답안 #370383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
370383 2021-02-23T23:05:18 Z jainbot27 Kralj (COCI16_kralj) C++17
140 / 140
828 ms 43528 KB
#pragma GCC optimize("Ofast")  
#pragma GCC target("avx,avx2,fma") 
#include <bits/stdc++.h>
using namespace std;

#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()

#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)

using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;

template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const char nl = '\n';
const int mxN = 5e5+10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;

int n;
int a[mxN];
int st[mxN];
int par[mxN];
vi 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]].pb(x);
    }
    int q = 0;
    F0R(i, 2*n){
        //cout << i << ' ' << q << endl;;
        bool good = 1;
        if(q>0){
            comb((i-1)%n, i%n);
            q--;
            good = 0;
        }
        //if(i < n){
            //if(q == 0){
                q += max(siz(v[i%n])-good,0);
            //}
            //else{
            //    q += siz(v[i%n]);
            //}
        //}
    }
    F0R(i, n){
        //cout << par[i] << ' ';
        nums[par[i]]++;
    }
    //cout << endl;
    F0R(i, n){
        set<int> al;
        FOR(j, i, i+nums[i]){
            trav(x, v[j%n]){
                al.insert(x);
            }
            assert(siz(al));
            auto it = al.lower_bound(st[j%n]);
            if(it==al.end()) al.erase(al.begin());
            else{
                ans++;
                al.erase(it);
            }
        }
    }
    cout << ans << nl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 828 ms 37416 KB Output is correct
2 Correct 488 ms 36588 KB Output is correct
3 Correct 618 ms 42544 KB Output is correct
4 Correct 623 ms 43528 KB Output is correct
5 Correct 374 ms 20716 KB Output is correct
6 Correct 332 ms 21100 KB Output is correct
7 Correct 430 ms 24044 KB Output is correct
8 Correct 367 ms 23532 KB Output is correct
9 Correct 447 ms 24684 KB Output is correct
10 Correct 381 ms 21996 KB Output is correct