J - One of Each
AC_Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <ctime> 7 #include <vector> 8 #include <stack> 9 using namespace std; 10 const int maxn = 2e5+20; 11 int a[maxn]; 12 int num[maxn]; 13 bool vis[maxn]; 14 15 int main() 16 { 17 int n,k; 18 scanf("%d%d",&n,&k); 19 for(int i=0;i<n;i++) 20 { 21 scanf("%d",&a[i]); 22 num[a[i]]++; 23 } 24 stack<int>Q; 25 for(int i=0;i<n;i++) 26 { 27 if( vis[a[i]]) 28 { 29 num[a[i]]--; 30 continue; 31 } 32 while(!Q.empty() && a[i]<Q.top() && num[Q.top()]!=0 )///相对较大的数,且后面此数还有则往后放 33 { 34 vis[Q.top()] = 0; 35 Q.pop(); 36 } 37 vis[a[i]] = 1; 38 Q.push(a[i]); 39 num[a[i]]--; 40 } 41 int cnt=0; 42 while(!Q.empty()) 43 { 44 a[cnt++] = Q.top(); 45 Q.pop(); 46 } 47 for(int i=cnt-1;i>=0;i--) 48 { 49 if( i==0 ) printf("%d ",a[i]); 50 else printf("%d ",a[i]); 51 } 52 return 0; 53 }