제출 #1341695

#제출 시각아이디문제언어결과실행 시간메모리
1341695doantaolaaidiKralj (COCI16_kralj)C++20
140 / 140
360 ms86752 KiB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define AC "test"
#define foru(i, l, r) for (int i = (l); i <= (r); i++)
#define ford(i, l, r) for (int i = (l); i >= (r); i--)
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vii;
typedef vector<ll> vll;

const ll inf = 1e9 + 7;
const ll linf = 1e18 + 7;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 7;
const int base = 31;

void fastIO(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
}

ll mul(ll a, ll b){
    a%=mod;
    ll res = 0;
    while (b){
        if (b%2) res = (res + a)%mod;
        a = (a + a)%mod;
        b/=2;
    }
    return res;
}

ll Pow(ll a, ll b){
    ll ans = 1;
    while (b){
        if (b % 2) ans = mul(ans, a);
        a = mul(a, a);
        b/=2;
    }
    return ans;
}

set<int> s, sac[maxn];
deque<int> dq;
int par[maxn], n, st, a[maxn], val, v[maxn], p[maxn], res, cnt[maxn], pre[maxn], quali[maxn], mn[maxn];

namespace sub1{
    void solve(){
        foru(i, 1, n){
            s.insert(v[i]);
        }
        res = 0;
        foru(i, 1, n){
            auto it = s.lower_bound(p[i]);
            if (it != s.end()){
                res++;
                s.erase(it);
            }
            else{
                s.erase(s.begin());
            }
        }
        cout << res;
    }
}

namespace subAC{

    int get(int top){
        return (top == par[top] ? top: par[top] = get(par[top]));
    }

    void uni(int u, int v){
        u = get(u);
        v = get(v);
        if (sac[u].size() < sac[v].size()) swap(u, v);
        par[v] = u;
        for (int tmp : sac[v]){
            sac[u].insert(tmp);
        }
    }

    void solve(){
        foru(i, 1, n){
            cnt[a[i]]++;
            par[i] = i;
            sac[a[i]].insert(v[i]);
        }
        foru(i, 1, n){
            cnt[i]--;
            pre[i] = pre[i - 1] + cnt[i];
        }
        foru(i, n + 1, 2*n - 1){
            pre[i] = pre[i - 1] + cnt[i - n];
        }
        foru(i, 1 ,2*n - 1){
            while (!dq.empty() && pre[dq.back()] > pre[i]) dq.pop_back();
            dq.pb(i);
            while (!dq.empty() && dq.front() <= i - n) dq.pop_front();
            if (i >= n) mn[i - n + 1] = pre[dq.front()];
        }
        foru(i, 1, n){
            if (mn[i] >= pre[i - 1]) quali[i] = 1;
        }
        foru(i, 1, n){
            if (quali[i]){
                st = i;
                break;
            }
        }
        res = 0;
        foru(i, st, n){
            if (i > st){
                uni(i, i - 1);
            }
            int pa = get(st);
            auto it = sac[pa].lower_bound(p[i]);
            if (it != sac[pa].end()){
                res++;
                sac[pa].erase(it);
            }
            else{
                sac[pa].erase(sac[pa].begin());
            }
        }
        if (st > 1){
            uni(n, 1);
            foru(i, 1, st - 1){
                if (i > 1){
                    uni(i, i - 1);
                }
                int pa = get(st);
                auto it = sac[pa].lower_bound(p[i]);
                if (it != sac[pa].end()){
                    res++;
                    sac[pa].erase(it);
                }
                else{
                    sac[pa].erase(sac[pa].begin());
                }
            }
        }
        cout << res;
    }
}

void inp(){
    cin >> n;
    val = 1;
    foru(i, 1, n){
        cin >> a[i];
        val = max(val, a[i]);
    }
    foru(i, 1, n){
        cin >> p[i];
    }
    foru(i, 1, n){
        cin >> v[i];
    }

}

int main(){
    fastIO();
    if (fopen(AC".inp", "r")){
        freopen(AC".inp", "r", stdin);
        freopen(AC".out", "w", stdout);
    }
    inp();
    if (val == 1) sub1::solve();
    else subAC::solve();
}

컴파일 시 표준 에러 (stderr) 메시지

kralj.cpp: In function 'int main()':
kralj.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen(AC".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
kralj.cpp:172:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  172 |         freopen(AC".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...