Submission #1222823

#TimeUsernameProblemLanguageResultExecution timeMemory
1222823thelegendary08Closing Time (IOI23_closing)C++17
43 / 100
113 ms33212 KiB
#include "closing.h"
#include<bits/stdc++.h>
#define f0r(i,n) for(ll i = 0;i<n;i++)
#define FOR(i, k, n) for(int i = k;i<n;i++)
#define pb push_back
#define vi vector<long long int>
#define ll long long int
#define mp make_pair
using namespace std;

int max_score(int N, int X, int Y, long long K,
              std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
    vector<pair<ll,ll>> adj[N];
    f0r(i, N-1){
    	adj[U[i]].pb({V[i], W[i]});
    	adj[V[i]].pb({U[i], W[i]});
    }
    vector<vector<ll>>dist(N, vector<ll>(2, 4e18));
    vector<bool>vis(N, 0);
    queue<int>q;
    q.push(X);
    vis[X] = 1;
    dist[X][0] = 0;
    while(!q.empty()){
    	int c = q.front();
    	q.pop();
    	for(auto u : adj[c]){
    		if(vis[u.first])continue;
    		vis[u.first] = 1;
    		dist[u.first][0] = min(dist[u.first][0], dist[c][0] + u.second);
    		q.push(u.first);
    	}
    }
    f0r(i,N)vis[i] = 0;
    vis[Y] = 1;
    dist[Y][1] = 0;
    q.push(Y);
    while(!q.empty()){
    	int c = q.front();
    	q.pop();
    	for(auto u : adj[c]){
    		if(vis[u.first])continue;
    		vis[u.first] = 1;
    		dist[u.first][1] = min(dist[u.first][1], dist[c][1] + u.second);
    		q.push(u.first);
    	}
    }
    if(dist[Y][0] > 2 * K){
    	vector<ll> dists;
	    f0r(i, N){
	    	dists.pb(min(dist[i][0], dist[i][1]));
	    }
	    sort(dists.begin(), dists.end());
		int ans = 0;
		ll s = 0;
		f0r(i, N){
			if(s + dists[i] > K)break;
			ans++;
			s += dists[i];
		}
		return ans;
    }
    else{
    	vector<vector<ll>>cost(N, vi(2));
	    f0r(i, N){
	    	cost[i][0] = min(dist[i][0], dist[i][1]);
	    	cost[i][1] = max(dist[i][0], dist[i][1]);
	    }
	    vector<pair<pair<ll,ll>,ll>> l2s;
	    f0r(i, N){
	    	l2s.pb(mp(mp(max(dist[i][0], dist[i][1]) - min(dist[i][0], dist[i][1]), max(dist[i][0], dist[i][1])), i));
	    }
	    sort(l2s.begin(), l2s.end());
	    vector<ll> dists;
	    f0r(i, N){
	    	dists.pb(min(dist[i][0], dist[i][1]));
	    }
	    sort(dists.begin(), dists.end());
		ll ans = 0;
		ll s = 0;
		f0r(i, N){
			if(s + dists[i] <= K){
				ans++; s += dists[i];
			}
		}
		ll mx = -4e18;
		ll mn = 4e18;
		ll twosum = 0;
		f0r(i, N){
			// cout<<l2s[i].first<<' '<<l2s[i].second<<'\n';
			mx = max(mx, l2s[i].second);
			mn = min(mn, l2s[i].second);
			twosum += max(dist[l2s[i].second][0], dist[l2s[i].second][1]);
			ll cur = twosum;
			ll curans = (mx - mn + 1) * 2; 
			if(mn > X){
				for(int j = X; j<mn; j++){
					cur += dist[j][0];
					curans++;
				}
			}
			if(mx < Y){
				for(int j = mx + 1; j<=Y; j++){
					cur += dist[j][1];
					curans++;
				}
			}
			// cout<<twosum<<' '<<cur<<'\n';
			if(cur > K)continue;
			vi bonk; 
			for(int j = min((ll)X-1, mn-1); j>=0; j--){
				bonk.pb(dist[j][0]);
			}
			for(int j = max((ll)Y+1, mx+1); j<N; j++){
				bonk.pb(dist[j][1]);
			}
			sort(bonk.begin(), bonk.end());
			int ptr = 0;
			while(ptr < bonk.size() && bonk[ptr] + cur <= K){
				curans++; cur += bonk[ptr]; ptr++;
			}
			// cout<<i<<' '<<curans<<'\n';
			// cout<<'\n';
			ans = max(ans, curans);
		}
    	
    	return ans;
    }
    
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...