#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
#define pb push_back
#define rsz resize
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define lb lower_bound
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
namespace output {
void pr(int x) {
cout << x;
}
void pr(long x) {
cout << x;
}
void pr(ll x) {
cout << x;
}
void pr(unsigned x) {
cout << x;
}
void pr(unsigned long x) {
cout << x;
}
void pr(unsigned long long x) {
cout << x;
}
void pr(float x) {
cout << x;
}
void pr(double x) {
cout << x;
}
void pr(long double x) {
cout << x;
}
void pr(char x) {
cout << x;
}
void pr(const char * x) {
cout << x;
}
void pr(const string & x) {
cout << x;
}
void pr(bool x) {
pr(x ? "true" : "false");
}
template < class T1, class T2 > void pr(const pair < T1, T2 > & x);
template < class T > void pr(const T & x);
template < class T, class...Ts > void pr(const T & t,
const Ts & ...ts) {
pr(t);
pr(ts...);
}
template < class T1, class T2 > void pr(const pair < T1, T2 > & x) {
pr("{", x.f, ", ", x.s, "}");
}
template < class T > void pr(const T & x) {
pr("{"); // const iterator needed for vector<bool>
bool fst = 1;
for (const auto & a: x) pr(!fst ? ", " : "", a), fst = 0;
pr("}");
}
void ps() {
pr("\n");
} // print w/ spaces
template < class T, class...Ts > void ps(const T & t,
const Ts & ...ts) {
pr(t);
if (sizeof...(ts)) pr(" ");
ps(ts...);
}
void pc() {
cout << "]" << endl;
} // debug w/ commas
template < class T, class...Ts > void pc(const T & t,
const Ts & ...ts) {
pr(t);
if (sizeof...(ts)) pr(", ");
pc(ts...);
}
#define dbg(x...) pr("[", #x, "] = ["), pc(x);
}
#ifndef ONLINE_JUDGE
using namespace output;
#else
using namespace output;
#define dbg(x...)
#endif
const int MX = 500005;
const int MOD = (int) (1e9 + 7);
const ll INF = (ll) 1e18;
template < int SZ > struct DSU {
int p[SZ], en[SZ];
void init() {
for (int i = 0; i < SZ; i++) {
p[i] = i;
en[i] = i;
}
}
int find(int x) {
return p[x] = (p[x] == x ? x : find(p[x]));
}
void merge(int u, int v) {
int a = find(u);
int b = find(v);
if (a != b) {
p[b] = a;
en[a] = en[b];
}
}
};
bool marked[MX];
DSU<MX> dsu;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<vi> a(n);
vi sx(n), sy(n);
for (int i = 0; i < n; i++) {
int x; cin >> x;
a[x - 1].pb(i);
}
for (int i = 0; i < n; i++) cin >> sx[i];
for (int i = 0; i < n; i++) cin >> sy[i];
dsu.init();
set<int> vals;
auto mark = [&] (int i) {
assert(!marked[i]);
marked[i] = true;
int prev = (i - 1 + n) % n;
int nxt = (i + 1) % n;
if (marked[prev]) {
dsu.merge(prev, i);
}
if (marked[nxt]) {
dsu.merge(i, nxt);
}
};
for (int i = 0; i < n; i++) {
for (int j = 0; j < sz(a[i]); j++) {
if (!marked[i]) {
mark(i);
} else {
int lst = dsu.en[dsu.find(i)];
mark((lst + 1) % n);
}
}
}
int st = (dsu.en[dsu.find(0)] + 1) % n;
rotate(a.begin(), a.begin() + st, a.end());
rotate(sx.begin(), sx.begin() + st, sx.end());
set<int> dudes;
int ans = 0;
for (int i = 0; i < n; i++) {
// dbg(i, a[i]);
for (auto x : a[i]) dudes.insert(sy[x]);
assert(!dudes.empty());
auto it = dudes.lb(sx[i]);
if (it == dudes.end()) {
dudes.erase(dudes.begin());
} else {
++ans;
dudes.erase(it);
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
778 ms |
37612 KB |
Output is correct |
2 |
Correct |
483 ms |
36848 KB |
Output is correct |
3 |
Correct |
600 ms |
44776 KB |
Output is correct |
4 |
Correct |
597 ms |
45960 KB |
Output is correct |
5 |
Correct |
394 ms |
22264 KB |
Output is correct |
6 |
Correct |
339 ms |
22520 KB |
Output is correct |
7 |
Correct |
459 ms |
25724 KB |
Output is correct |
8 |
Correct |
373 ms |
24440 KB |
Output is correct |
9 |
Correct |
479 ms |
27000 KB |
Output is correct |
10 |
Correct |
388 ms |
24312 KB |
Output is correct |