#include <bits/stdc++.h>
using namespace std;
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define F0R(x, z) FOR(x, 0, z)
const int mxN = 5e5+10;
int n;
int a[mxN];
int st[mxN];
int par[mxN];
vector<int> v[mxN];
int nums[mxN];
int ans = 0;
int p(int q){
if(par[q]==q) return par[q];
return par[q]=p(par[q]);
}
void comb(int e1, int e2){
e1 = p(e1); e2 = p(e2);
par[e2] = e1;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
F0R(i, n){
par[i] = i;
cin >> a[i]; a[i]--;
}
F0R(i, n){
cin >> st[i];
}
F0R(i, n){
int x; cin >> x;
v[a[i]].push_back(x);
}
int q = 0;
F0R(i, 2*n){
bool good = 1;
if(q>0){
comb((i-1)%n, i%n);
q--;
good = 0;
}
q += max((int)v[i%n].size()-good,0);
}
F0R(i, n){
nums[par[i]]++;
}
F0R(i, n){
set<int> al;
FOR(j, i, i+nums[i]){
for(auto&x:v[j%n]){
al.insert(x);
}
auto it = al.lower_bound(st[j%n]);
if(it==al.end()) al.erase(al.begin());
else{
ans++;
al.erase(it);
}
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
788 ms |
37220 KB |
Output is correct |
2 |
Correct |
472 ms |
36708 KB |
Output is correct |
3 |
Correct |
617 ms |
42724 KB |
Output is correct |
4 |
Correct |
588 ms |
43364 KB |
Output is correct |
5 |
Correct |
401 ms |
20844 KB |
Output is correct |
6 |
Correct |
328 ms |
21376 KB |
Output is correct |
7 |
Correct |
434 ms |
23916 KB |
Output is correct |
8 |
Correct |
359 ms |
23660 KB |
Output is correct |
9 |
Correct |
454 ms |
24428 KB |
Output is correct |
10 |
Correct |
379 ms |
21868 KB |
Output is correct |