[CF780A] Andryusha and Socks
題目敘述
Andryusha 有 $2n$ 隻襪子,他會從袋子裡依序拿襪子出來, 如果當前桌子上有相對應的襪子,就可以配成一對收起來。 請問把襪子全部都拿出來的過程中,桌子上最多有幾隻襪子?
題解
可以用一個 set
或 array
紀錄下哪些襪子出現過了。
然後每一次都問當前這個 array
有多少元素是 1。
參考程式碼
// by tmt514
#include <algorithm>
#include <cstdio>
using namespace std;
int a[100005];
int main(void) {
int n, ans = 0, cur = 0;
scanf("%d", &n);
for(int i=0;i<2*n;i++) {
int x;
scanf("%d", &x);
cur += (a[x] ^= 1)*2-1;
ans = max(ans, cur);
}
printf("%d\n", ans);
return 0;
}