Problem Link
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int main()
- {
- int t, n, m, res, victim;
- int c[401];
- int lastUse[401];
- int seenBefore[401];
- bool present[401];
- cin >> t;
- while(t--)
- {
- cin >> n >> m;
- for(int i = 0; i < m; i++)
- {
- lastUse[c[i]] = i;
- }
- int order = 0;
- res = 0;
- while((res < n || present[c[order]]) && order < m)
- {
- if(!present[c[order]])
- {
- res++;
- present[c[order]] = true;
- }
- order++;
- }
- while(order < m)
- {
- if(!present[c[order]])
- {
- victim = -1;
- for(int i = order - 1; i >= 0; i--)
- {
- if(present[c[i]] && lastUse[c[i]] <= order)
- {
- victim = c[i];
- break;
- }
- }
- if(victim == -1)
- {
- for(int i = order + 1; i < m; i++)
- {
- if(present[c[i]] && seenBefore[c[i]] != order)
- {
- victim = c[i];
- seenBefore[c[i]] = order;
- }
- }
- }
- present[victim] = false;
- present[c[order]] = true;
- res++;
- }
- order++;
- }
- cout << res << endl;
- }
- return 0;
- }
Comments
Post a Comment