A:Zero Array
题意:两种操作, 1 p v 将第p个位置的值改成v 2 查询最少的操作数使得所有数都变为0 操作为可以从原序列中选一个非0的数使得所有非0的数减去它,并且所有数不能变为负数
思路:考虑第二种操作,显然,最少的操作数肯定是不同数的个数 用map 记录,特殊注意0的存在
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define N 100010 6 7 unordered_map <int, int> mp; 8 9 int t, n, q; 10 11 int arr[N]; 12 13 int main() 14 { 15 cin.tie(0); 16 cout.tie(0); 17 ios::sync_with_stdio(false); 18 cin >> t; 19 while (t--) 20 { 21 cin >> n >> q; 22 mp.clear(); 23 for (int i = 1; i <= n; ++i) 24 { 25 cin >> arr[i]; 26 mp[arr[i]]++; 27 } 28 int op, p, v; 29 while (q--) 30 { 31 cin >> op; 32 if (op == 1) 33 { 34 cin >> p >> v; 35 if (mp[arr[p]] == 1) 36 { 37 mp.erase(arr[p]); 38 } 39 else 40 { 41 mp[arr[p]]--; 42 } 43 mp[v]++; 44 arr[p] = v; 45 } 46 else 47 { 48 mp[0]++; 49 cout << mp.size() - 1 << endl; 50 } 51 } 52 } 53 return 0; 54 }