Note:
It tells you that N numbers allow you to keep one repeated number and sort the output.
Analysis:
Store a number every time it is read.
There were countless RESS yesterday because I first output the first one and then output each one in sequence.
The problem is that if only one element needs to access the first element
Code:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 5000007; 8 9 struct Node {10 int d;11 Node *next;12 };13 Node *head[maxn + 10];14 Node num[maxn + 10];15 int ans[maxn];16 17 int main() {18 int n;19 int d;20 while(EOF != scanf("%d",&n) ) {21 memset(head, 0, sizeof(head));22 int tot = 1;23 int cnt = 0;24 for(int i = 1; i <= n; i++) {25 scanf("%d",&d);26 bool flag = true;27 int c = d % maxn;28 Node *pt = head[c];29 while(pt) {30 if(pt -> d == d) {31 flag = false;32 break;33 }34 pt = pt -> next;35 }36 if(flag) {37 ans[cnt++] = d;38 num[tot].d = d;39 num[tot].next = head[c];40 head[c] = &num[tot++];41 }42 }43 sort(ans, ans + cnt);44 printf("%d\n", cnt);45 printf("%d", ans[0]46 for(int i = 0; i < cnt; i++) {47 printf(i == 0 ? "%d" : " %d", ans[i]);48 }49 puts("");50 }51 return 0;52 }
View code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 5000007; 8 9 struct Node {10 int to;11 int next;12 }e[maxn + 10];13 14 int head[maxn + 10];15 int tot;16 17 bool Find(int u, int v) {18 for(int i = head[u]; i; i = e[i].next) {19 if(e[i].to == v) {20 return true;21 }22 }23 e[tot].to = v;24 e[tot].next = head[u];25 head[u] = tot++;26 return false;27 }28 29 int ans[maxn];30 31 int main() {32 int n;33 int d;34 while(EOF != scanf("%d",&n) ) {35 int cnt = 0;36 memset(head, 0, sizeof(head));37 tot = 1;38 for(int i = 1; i <= n; i++) {39 scanf("%d",&d);40 int c = d % maxn;41 if(!Find(c, d)) {42 ans[cnt++] = d;43 }44 }45 sort(ans, ans + cnt);46 printf("%d\n",cnt);47 if(cnt != 0) printf("%d", ans[0]);48 for(int i = 1; i < cnt; i++) {49 printf(" %d",ans[i]);50 }51 puts("");52 }53 return 0;54 }
Array Simulation
Hlg1287 number deduplication and sorting II [hash]