MT30. 序列操作
描述
输入描述
输出描述
从前往后输出序列中的每个元素,每个元素占一行。示例1
输入:
5 3 4 2 5
输出:
5 2 4 1 3
说明:
初始时序列为 1,2,3,4,5,经过第一次操作后序列为 4,1,2,3,5,经过第二次操作后序列为C++ 解法, 执行用时: 17ms, 内存消耗: 1400KB, 提交时间: 2020-12-22
#include<cstdio> #include<cstring> bool v[200010]; int a[100010]; int main() { memset(v,0,sizeof(v)); int n,m;scanf("%d %d",&n,&m); for(int i=1;i<=m;i++) { scanf("%d",&a[i]); } for(int i=m;i>0;i--) if(!v[a[i]])printf("%d\n",a[i]),v[a[i]]=1; for(int i=1;i<=n;i++) if(!v[i]){printf("%d\n",i);v[i]=1;} return 0; }
C++ 解法, 执行用时: 17ms, 内存消耗: 1548KB, 提交时间: 2022-04-23
#define _CRT_SECURE_NO_WARNINGS #define DEBUG123 #ifdef DEBUG #include "goodhead.h" #endif // DEBUG #ifndef DEBUG #include <bits/stdc++.h> #endif // !DEBUG using namespace std; ///Quoted from 912SCL template #define MAXN 2000005 #define INF 1000000007 #define _for(i, a, b) for (int i = (a); i < (b); ++i) #define _rep(i, a, b) for (int i = (a); i <= (b); ++i) typedef long long ll; typedef pair<string, string> pss; typedef pair<int, int> pii; typedef pair<string, int> psi; const double PI = acos(-1.0); const double eps = 1e-6; const int MOD = 1e7 + 7; inline int read() { int x = 0, w = 1; char ch = 0; while (ch < '0' || ch > '9') { // ch 不是数字时 if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getchar(); } return x * w; } inline void write(int x) { static int sta[35]; int top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); while (top) putchar(sta[--top] + 48); // 48 是 '0' } //end of quote bool e[100005]; bool done[100005]; int main() { #ifdef DEBUG freopen("F:/Code/Algorithm/CppAlgorithm/PlayGround/in.txt", "r", stdin); //freopen("F:/Code/Algorithm/CppAlgorithm/PlayGround/out.txt", "w", stdout); #endif int n , m; memset(e, false, sizeof(e)); memset(done, false, sizeof(done)); stack<int> q; while (~scanf("%d %d", &n, &m)) { for (int i = 0; i < m; i++) { int move = read(); e[move] == true; q.push(move); } for (int i = 0; i <= n; i++) { while (!q.empty()) { int temp = q.top(); q.pop(); if (done[temp] == false) { done[temp] = true; printf("%d \n", temp); } } } for (int i = 1; i <= n; i++) { if (done[i] == false) { printf("%d \n", i); } } } return 0; }