不清楚为什么超时,数据小的时候超时,怀疑是getchar的问题?
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 1e7 + 5; int ID[maxn]; int main() { ios::sync_with_stdio(false); int N, cnt = 0, cnt_0 = 0; int m = 0, n = 0, num = 0; char ch; scanf("%d", &N); getchar(); while (N--) { while(1) { ch = getchar(); if (ch == '\n') { if(cnt_0 == 0) { ID[cnt++] = num; num = 0; } cnt_0 = 0; break; } else if(cnt_0 == 0 && ch == ' ') { cnt_0++; ID[cnt++] = num; num = 0; } else if(ch >= '0' && ch <= '9') { cnt_0 = 0; num = num * 10 + (ch - '0'); } } } sort(ID, ID + cnt); // for (int i = 0; i < cnt; i++) // { // if(i) cout << " "; // cout << ID[i]; // } // cout << endl; for (int i = 0; i < cnt; i++) { if (ID[i + 1] - ID[i] > 1) m = ID[i] + 1; if (ID[i + 1] - ID[i] == 0) n = ID[i]; if (m != 0 && n != 0) break; } printf("%d %d", m, n); system("pause"); return 0; }