445 B. Vlad and Cafes
http://codeforces.com/contest/890/problem/B
Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.
First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe.
In first line there is one integer n (1 ≤ n ≤ 2·105) — number of cafes indices written by Vlad.
In second line, n numbers a1, a2, ..., an (0 ≤ ai ≤ 2·105) are written — indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted.
Print one integer — index of the cafe that Vlad hasn't visited for as long as possible.
5
1 3 2 1 2
3
6
2 1 2 2 4 1
2
In first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer.
In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes.
set写
// 去吧!皮卡丘! 把AC带回来! // へ /| // /\7 ∠_/ // / │ / / // │ Z _,< / /`ヽ // │ ヽ / 〉 // Y ` / / // イ● 、 ● ⊂⊃〈 / // () へ | \〈 // >ー 、_ ィ │ // // / へ / ノ<| \\ // ヽ_ノ (_/ │// // 7 |/ // >―r ̄ ̄`ー―_ //************************************** #pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 2147483647 const ll INF = 0x3f3f3f3f3f3f3f3fll; #define ri register int template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } #define scanf1(x) scanf("%d", &x) #define scanf2(x, y) scanf("%d%d", &x, &y) #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X) #define pi acos(-1) #define me(x, y) memset(x, y, sizeof(x)); #define For(i, a, b) for (int i = a; i <= b; i++) #define FFor(i, a, b) for (int i = a; i >= b; i--) #define bug printf("***********\n"); #define mp make_pair #define pb push_back const int maxn = 3e5 + 10; const int maxx = 1e6 + 10; // name******************************* int n; int a[maxn]; set<int> S; int x; // function****************************** //*************************************** int main() { // ios::sync_with_stdio(0); cin.tie(0); // freopen("test.txt", "r", stdin); // freopen("outout.txt","w",stdout); cin >> n; For(i, 1, n) { cin >> a[i]; S.insert(a[i]); } FFor(i, n, 1) { if (S.size() == 1) break; if (S.find(a[i]) != S.end()) { S.erase(a[i]); } } cout << *S.begin(); return 0; }
手写
// 去吧!皮卡丘! 把AC带回来! // へ /| // /\7 ∠_/ // / │ / / // │ Z _,< / /`ヽ // │ ヽ / 〉 // Y ` / / // イ● 、 ● ⊂⊃〈 / // () へ | \〈 // >ー 、_ ィ │ // // / へ / ノ<| \\ // ヽ_ノ (_/ │// // 7 |/ // >―r ̄ ̄`ー―_ //************************************** #pragma comment(linker, "/STACK:1024000000,1024000000") #include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 2147483647 const ll INF = 0x3f3f3f3f3f3f3f3fll; #define ri register int template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } #define scanf1(x) scanf("%d", &x) #define scanf2(x, y) scanf("%d%d", &x, &y) #define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z) #define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X) #define pi acos(-1) #define me(x, y) memset(x, y, sizeof(x)); #define For(i, a, b) for (int i = a; i <= b; i++) #define FFor(i, a, b) for (int i = a; i >= b; i--) #define bug printf("***********\n"); #define mp make_pair #define pb push_back const int maxn = 2e5 + 10; const int maxx = 1e6 + 10; // name******************************* int n; int used[maxn]; int a[maxn]; int cnt = 0; int p; // function****************************** //*************************************** int main() { // ios::sync_with_stdio(0); cin.tie(0); // freopen("test.txt", "r", stdin); // freopen("outout.txt","w",stdout); cin >> n; For(i, 1, n) { cin >> a[i]; if (!used[a[i]]) { cnt++; used[a[i]] = 1; } } p = cnt; FFor(i, n, 1) { if (p == 1 && used[a[i]]) { cout << a[i]; exit(0); } if (used[a[i]]) { used[a[i]] = 0; p--; } } return 0; }