card game

describe

There are N cards in front of Xiao Hi, and each card has two sides, A and B. The A side of the i-th card has an integer A i written on it, and the B side has an integer B i written on it .  

All cards start with A face up. Now Hi has to choose a card and turn it over with face B facing up.  

Suppose the set of numbers on the upward side of N cards is S, then the score of the game is: the smallest positive integer that does not belong to S.  

For example S={1, 2, 4, 6, 7}, the score is 3.  

Can you figure out the maximum number of points Xiao Hi can get?

enter

The first line contains an integer N.

The second row contains N integers A i .  

The third row contains N integers B i .  

For 50% of the data, 1 ≤ N ≤ 1000  

For 100% of the data, 1 ≤ N ≤ 100000 1 ≤ A i , B i  ≤ 1000000

output

output max score

sample input
5  
1 2 3 5 6  
3 4 3 4 4
Sample output

6

#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
set<int> S;
const int maxn = 1e5 + 10;
int A[maxn], B[maxn];

int main(){
    int N, ans = 1;
    scanf("%d", &N);
    for(int i = 1; i <= N; ++i) scanf("%d", A + i), mp[A[i]]++;
    for(int i = 1; i <= N; ++i) scanf("%d", B + i);
    for(int i = 1; i <= N + 1; ++i) if(mp.find(i) == mp.end()) S.insert(i);//If the key and value are not equal, just put S
    for(int i = 1; i <= N; ++i){//traverse to find the maximum value
        mp [A [i]]-;
        if(mp[A[i]] == 0) S.insert(A[i]);
        mp [B [i]] ++;
        if(mp[B[i]] == 1 && S.find(B[i]) != S.end()) S.erase(B[i]);
        years = max(years, *S.begin());
        mp [B [i]]-;
        if(mp[B[i]] == 0) S.insert(B[i]);
        mp [A [i]] ++;
        if(mp[A[i]] == 1 && S.find(A[i]) != S.end()) S.erase(A[i]);
    }
    printf("%d\n", ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324646093&siteId=291194637