Submission #1012644

# Submission time Handle Problem Language Result Execution time Memory
1012644 2024-07-02T12:49:42 Z JakobZorz Closing Time (IOI23_closing) C++17
8 / 100
91 ms 36052 KB
#include"closing.h"
#include<vector>
#include<iostream>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;

int n,x,y;
ll k;
vector<pair<int,int>>nodes[200000];
ll distx[200000];
ll disty[200000];

void dfs1(int node,int par,ll*dists){
    for(auto[ne,w]:nodes[node]){
        if(ne==par)
            continue;
        dists[ne]=dists[node]+w;
        dfs1(ne,node,dists);
    }
}

int lvl1(){
    vector<bool>vis(n);
    int res=0;
    ll kr=k;
    priority_queue<pair<ll,int>>q;
    q.push({0,x});
    q.push({0,y});
    vis[x]=true;
    vis[y]=true;
    while(q.size()){
        int node=q.top().second;
        ll cost=-q.top().first;
        q.pop();
        if(cost>kr)
            break;
        res++;
        kr-=cost;
        for(auto[ne,w]:nodes[node]){
            if(vis[ne])
                continue;
            vis[ne]=true;
            q.push({-min(distx[ne],disty[ne]),ne});
        }
    }
    return res;
}

struct Structure{
    multiset<ll>s;
    vector<ll>vec;
    vector<ll>tree;
    vector<bool>pres;
    map<ll,int>mp;
    
    void reg(ll a){
        vec.push_back(a);
    }
    
    void init(){
        sort(vec.begin(),vec.end());
        tree=vector<ll>(vec.size());
        pres=vector<bool>(vec.size());
        for(int i=(int)vec.size()-1;i>=0;i--)
            mp[vec[i]]=i;
    }
    
    int get(ll money){
        int res=0;
        for(int i=0;i<(int)vec.size();i++){
            if(tree[i]>money)
                break;
            money-=tree[i];
            res+=pres[i];
        }
        return res;
    }
    
    void insert(ll a){
        tree[mp[a]]=vec[mp[a]];
        pres[mp[a]]=true;
        mp[a]++;
    }
    
    void remove(ll a){
        tree[mp[a]]=0;
        pres[mp[a]]=false;
        mp[a]--;
    }
};

int cmp(pair<ll,ll>a,pair<ll,ll>b){
    return a.first+a.second<b.first+b.second;
}

int solve(vector<pair<ll,ll>>opts,ll money){
    sort(opts.begin(),opts.end(),cmp);
    Structure s;
    
    for(auto[i,j]:opts){
        s.reg(i);
        s.reg(j);
    }
    
    s.init();
    
    for(auto[i,j]:opts)
        s.insert(i);
    
    int res=s.get(money);
    int res2=0;
    for(auto[i,j]:opts){
        money-=i;
        s.remove(i);
        s.insert(j);
        res2++;
        if(money<0)
            break;
        res=max(res,res2+s.get(money));
    }
    return res;
}

int lvl2(){
    int cres=0;
    ll money=k;
    vector<pair<ll,ll>>opts;
    for(int i=0;i<n;i++){
        if(distx[i]+disty[i]==distx[y]){ // is on the path from x to y
            cres++;
            money-=min(distx[i],disty[i]);
            opts.emplace_back(max(distx[i],disty[i])-min(distx[i],disty[i]),1e18);
        }else{
            opts.emplace_back(min(distx[i],disty[i]),max(distx[i],disty[i])-min(distx[i],disty[i]));
        }
    }
    if(money<0)
        return 0;
    
    return cres+solve(opts,money);
}

int max_score(int N,int X,int Y,ll K,vector<int>U,vector<int>V,vector<int>W){
    n=N;
    x=X;
    y=Y;
    k=K;
    for(int i=0;i<n;i++){
        distx[i]=0;
        disty[i]=0;
        nodes[i].clear();
    }
    for(int i=0;i<n-1;i++){
        nodes[U[i]].emplace_back(V[i],W[i]);
        nodes[V[i]].emplace_back(U[i],W[i]);
    }
    dfs1(x,x,distx);
    dfs1(y,y,disty);
    
    return max(lvl1(),lvl2());
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 31136 KB Output is correct
2 Correct 91 ms 36052 KB Output is correct
3 Correct 43 ms 11996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8104 KB Output is correct
2 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8104 KB Output is correct
2 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8104 KB Output is correct
2 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8024 KB Output is correct
2 Correct 2 ms 8104 KB Output is correct
3 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8024 KB Output is correct
2 Correct 2 ms 8104 KB Output is correct
3 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8024 KB Output is correct
2 Correct 2 ms 8104 KB Output is correct
3 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8024 KB Output is correct
2 Correct 2 ms 8104 KB Output is correct
3 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 8024 KB Output is correct
2 Correct 2 ms 8104 KB Output is correct
3 Incorrect 2 ms 8028 KB 1st lines differ - on the 1st token, expected: '30', found: '31'
4 Halted 0 ms 0 KB -