Submission #1032066

# Submission time Handle Problem Language Result Execution time Memory
1032066 2024-07-23T10:45:44 Z Unforgettablepl Security Guard (JOI23_guard) C++17
100 / 100
351 ms 108716 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long

struct UFDSsimple{
	vector<int> p,siz,mini;
	UFDSsimple(int n):p(n+1),siz(n+1,1){iota(p.begin(),p.end(),0);}
	int findRoot(int x){
		return p[x]==x ? x : p[x]=findRoot(p[x]);
	}
	bool unite(int a,int b){
		a = findRoot(a);
		b = findRoot(b);
		if(a==b)return false;
		if(siz[b]>siz[a])swap(a,b);
		siz[a]+=siz[b];
		p[b]=a;
		return true;
	}
};

struct UFDS{
	vector<int> p,siz,mini,lazy,decomissioner;
	vector<priority_queue<pair<int,int>>> pq;
	priority_queue<pair<int,int>> globalpq;
	UFDS(int n,vector<int> arr,vector<pair<int,int>> edges):p(n+1),siz(n+1,1),mini(n+1),lazy(n+1),decomissioner(n+1),pq(n+1){
		iota(p.begin(),p.end(),0);
		mini=arr;
		int globalmini = *min_element(arr.begin()+1,arr.end());
		for(int i=0;i<n-1;i++){
			auto [u,v] = edges[i];
			pq[u].emplace(globalmini-arr[v],v);
			pq[v].emplace(globalmini-arr[u],u);
		}
		for(int i=1;i<=n;i++)globalpq.emplace(pq[i].top().first,i);
	}
	int findRoot(int x){
		return p[x]==x ? x : p[x]=findRoot(p[x]);
	}
	void unite(int a,int b){
		a = findRoot(a);
		b = findRoot(b);
		if(a==b)assert(false);
		if(siz[b]>siz[a])swap(a,b);
		int newmin = min(mini[a],mini[b]);
		decomissioner[a]++;
		decomissioner[b]++;
		lazy[a]+=newmin-mini[a];
		lazy[b]+=newmin-mini[b];
		if(pq[b].size()>pq[a].size()){
			swap(pq[a],pq[b]);
			swap(lazy[a],lazy[b]);
		}
		while(!pq[b].empty()){
			auto [cost,v] = pq[b].top();pq[b].pop();
			pq[a].emplace(cost+lazy[b]-lazy[a],v);
		}
		if(!pq[a].empty())globalpq.emplace(pq[a].top().first+lazy[a],a);
		mini[a]=newmin;
		siz[a]+=siz[b];
		p[b]=a;
	}
	int get_best(){
		if(globalpq.empty())assert(false);
		auto [cost,curra] = globalpq.top();globalpq.pop();
		if(decomissioner[curra]){
			decomissioner[curra]--;
			return get_best();
		}
		auto [temp,currb] = pq[curra].top();pq[curra].pop();
		curra = findRoot(curra);
		currb = findRoot(currb);
		if(curra==currb){
			globalpq.emplace(pq[curra].top().first+lazy[curra],curra);
			return get_best();
		}
		decomissioner[curra]--;
		unite(curra,currb);
		return cost;
	}
};


int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	int n,m,q;
	cin >> n >> m >> q;
	vector<int> arr(n+1);
	for(int i=1;i<=n;i++)cin>>arr[i];
	vector<tuple<int,int,int>> edges;
	for(int i=1;i<=m;i++){
		int a,b;
		cin >> a >> b;
		edges.emplace_back(arr[a]+arr[b],a,b);
	}
	sort(edges.begin(),edges.end());
	int baseans = (*max_element(arr.begin(),arr.end()));
	baseans += (n-2ll)*(*min_element(arr.begin()+1,arr.end()));
	UFDSsimple dsusim(n);
	vector<pair<int,int>> gudedges;
	for(int i=0;i<m;i++){
		auto [cost,a,b] = edges[i];
		if(!dsusim.unite(a,b))continue;
		gudedges.emplace_back(a,b);
	} 
	vector<int> ans(n);
	ans[0] = baseans;
	UFDS dsu(n,arr,gudedges);
	for(int i=1;i<n;i++){
		ans[i]=ans[i-1]-dsu.get_best();
	}
	for(int i=0;i<=q;i++){
		cout << ans[max(n-1-i,0ll)] << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 103 ms 52860 KB Output is correct
3 Correct 97 ms 52668 KB Output is correct
4 Correct 139 ms 55304 KB Output is correct
5 Correct 156 ms 52920 KB Output is correct
6 Correct 129 ms 52920 KB Output is correct
7 Correct 142 ms 52924 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 103 ms 52860 KB Output is correct
3 Correct 97 ms 52668 KB Output is correct
4 Correct 139 ms 55304 KB Output is correct
5 Correct 156 ms 52920 KB Output is correct
6 Correct 129 ms 52920 KB Output is correct
7 Correct 142 ms 52924 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 245 ms 57052 KB Output is correct
11 Correct 105 ms 54456 KB Output is correct
12 Correct 117 ms 54576 KB Output is correct
13 Correct 114 ms 55736 KB Output is correct
14 Correct 252 ms 57268 KB Output is correct
15 Correct 275 ms 56384 KB Output is correct
16 Correct 262 ms 55096 KB Output is correct
17 Correct 236 ms 56936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 103 ms 52860 KB Output is correct
3 Correct 97 ms 52668 KB Output is correct
4 Correct 139 ms 55304 KB Output is correct
5 Correct 156 ms 52920 KB Output is correct
6 Correct 129 ms 52920 KB Output is correct
7 Correct 142 ms 52924 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 245 ms 57052 KB Output is correct
11 Correct 105 ms 54456 KB Output is correct
12 Correct 117 ms 54576 KB Output is correct
13 Correct 114 ms 55736 KB Output is correct
14 Correct 252 ms 57268 KB Output is correct
15 Correct 275 ms 56384 KB Output is correct
16 Correct 262 ms 55096 KB Output is correct
17 Correct 236 ms 56936 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 272 ms 55584 KB Output is correct
20 Correct 251 ms 56712 KB Output is correct
21 Correct 303 ms 55364 KB Output is correct
22 Correct 351 ms 56412 KB Output is correct
23 Correct 237 ms 54764 KB Output is correct
24 Correct 241 ms 55172 KB Output is correct
25 Correct 174 ms 64380 KB Output is correct
26 Correct 152 ms 70944 KB Output is correct
27 Correct 133 ms 75904 KB Output is correct
28 Correct 243 ms 55988 KB Output is correct
29 Correct 228 ms 57360 KB Output is correct
30 Correct 180 ms 62804 KB Output is correct
31 Correct 126 ms 62044 KB Output is correct
32 Correct 224 ms 55428 KB Output is correct
33 Correct 194 ms 56760 KB Output is correct
34 Correct 253 ms 96128 KB Output is correct
35 Correct 229 ms 92796 KB Output is correct
36 Correct 180 ms 77084 KB Output is correct
37 Correct 171 ms 77144 KB Output is correct
38 Correct 203 ms 63872 KB Output is correct
39 Correct 158 ms 61584 KB Output is correct
40 Correct 199 ms 76308 KB Output is correct
41 Correct 194 ms 53692 KB Output is correct
42 Correct 187 ms 66176 KB Output is correct
43 Correct 203 ms 56248 KB Output is correct
44 Correct 247 ms 55736 KB Output is correct
45 Correct 231 ms 55488 KB Output is correct
46 Correct 266 ms 55344 KB Output is correct
47 Correct 277 ms 56248 KB Output is correct
48 Correct 284 ms 56884 KB Output is correct
49 Correct 184 ms 56760 KB Output is correct
50 Correct 220 ms 55104 KB Output is correct
51 Correct 193 ms 57524 KB Output is correct
52 Correct 178 ms 53692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 103 ms 52860 KB Output is correct
3 Correct 97 ms 52668 KB Output is correct
4 Correct 139 ms 55304 KB Output is correct
5 Correct 156 ms 52920 KB Output is correct
6 Correct 129 ms 52920 KB Output is correct
7 Correct 142 ms 52924 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 245 ms 57052 KB Output is correct
11 Correct 105 ms 54456 KB Output is correct
12 Correct 117 ms 54576 KB Output is correct
13 Correct 114 ms 55736 KB Output is correct
14 Correct 252 ms 57268 KB Output is correct
15 Correct 275 ms 56384 KB Output is correct
16 Correct 262 ms 55096 KB Output is correct
17 Correct 236 ms 56936 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 272 ms 55584 KB Output is correct
20 Correct 251 ms 56712 KB Output is correct
21 Correct 303 ms 55364 KB Output is correct
22 Correct 351 ms 56412 KB Output is correct
23 Correct 237 ms 54764 KB Output is correct
24 Correct 241 ms 55172 KB Output is correct
25 Correct 174 ms 64380 KB Output is correct
26 Correct 152 ms 70944 KB Output is correct
27 Correct 133 ms 75904 KB Output is correct
28 Correct 243 ms 55988 KB Output is correct
29 Correct 228 ms 57360 KB Output is correct
30 Correct 180 ms 62804 KB Output is correct
31 Correct 126 ms 62044 KB Output is correct
32 Correct 224 ms 55428 KB Output is correct
33 Correct 194 ms 56760 KB Output is correct
34 Correct 253 ms 96128 KB Output is correct
35 Correct 229 ms 92796 KB Output is correct
36 Correct 180 ms 77084 KB Output is correct
37 Correct 171 ms 77144 KB Output is correct
38 Correct 203 ms 63872 KB Output is correct
39 Correct 158 ms 61584 KB Output is correct
40 Correct 199 ms 76308 KB Output is correct
41 Correct 194 ms 53692 KB Output is correct
42 Correct 187 ms 66176 KB Output is correct
43 Correct 203 ms 56248 KB Output is correct
44 Correct 247 ms 55736 KB Output is correct
45 Correct 231 ms 55488 KB Output is correct
46 Correct 266 ms 55344 KB Output is correct
47 Correct 277 ms 56248 KB Output is correct
48 Correct 284 ms 56884 KB Output is correct
49 Correct 184 ms 56760 KB Output is correct
50 Correct 220 ms 55104 KB Output is correct
51 Correct 193 ms 57524 KB Output is correct
52 Correct 178 ms 53692 KB Output is correct
53 Correct 1 ms 600 KB Output is correct
54 Correct 243 ms 55996 KB Output is correct
55 Correct 275 ms 56252 KB Output is correct
56 Correct 245 ms 58292 KB Output is correct
57 Correct 291 ms 62536 KB Output is correct
58 Correct 243 ms 63924 KB Output is correct
59 Correct 224 ms 70572 KB Output is correct
60 Correct 188 ms 81328 KB Output is correct
61 Correct 186 ms 82980 KB Output is correct
62 Correct 162 ms 69040 KB Output is correct
63 Correct 230 ms 56760 KB Output is correct
64 Correct 236 ms 64236 KB Output is correct
65 Correct 219 ms 72880 KB Output is correct
66 Correct 166 ms 81704 KB Output is correct
67 Correct 242 ms 67508 KB Output is correct
68 Correct 230 ms 56760 KB Output is correct
69 Correct 283 ms 100912 KB Output is correct
70 Correct 237 ms 92028 KB Output is correct
71 Correct 206 ms 79012 KB Output is correct
72 Correct 188 ms 74880 KB Output is correct
73 Correct 197 ms 64900 KB Output is correct
74 Correct 152 ms 62072 KB Output is correct
75 Correct 198 ms 76496 KB Output is correct
76 Correct 187 ms 54460 KB Output is correct
77 Correct 193 ms 66176 KB Output is correct
78 Correct 210 ms 56504 KB Output is correct
79 Correct 285 ms 62896 KB Output is correct
80 Correct 239 ms 57272 KB Output is correct
81 Correct 280 ms 61100 KB Output is correct
82 Correct 281 ms 62636 KB Output is correct
83 Correct 297 ms 62384 KB Output is correct
84 Correct 227 ms 64448 KB Output is correct
85 Correct 298 ms 55092 KB Output is correct
86 Correct 181 ms 55484 KB Output is correct
87 Correct 173 ms 53684 KB Output is correct
88 Correct 327 ms 64260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 428 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 12 ms 2640 KB Output is correct
9 Correct 11 ms 2652 KB Output is correct
10 Correct 11 ms 2396 KB Output is correct
11 Correct 10 ms 2648 KB Output is correct
12 Correct 11 ms 2652 KB Output is correct
13 Correct 10 ms 2652 KB Output is correct
14 Correct 10 ms 2652 KB Output is correct
15 Correct 10 ms 2652 KB Output is correct
16 Correct 11 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 428 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 12 ms 2640 KB Output is correct
9 Correct 11 ms 2652 KB Output is correct
10 Correct 11 ms 2396 KB Output is correct
11 Correct 10 ms 2648 KB Output is correct
12 Correct 11 ms 2652 KB Output is correct
13 Correct 10 ms 2652 KB Output is correct
14 Correct 10 ms 2652 KB Output is correct
15 Correct 10 ms 2652 KB Output is correct
16 Correct 11 ms 2652 KB Output is correct
17 Correct 16 ms 4044 KB Output is correct
18 Correct 18 ms 4148 KB Output is correct
19 Correct 28 ms 6860 KB Output is correct
20 Correct 43 ms 8888 KB Output is correct
21 Correct 48 ms 10420 KB Output is correct
22 Correct 42 ms 9148 KB Output is correct
23 Correct 78 ms 16804 KB Output is correct
24 Correct 52 ms 11200 KB Output is correct
25 Correct 12 ms 3672 KB Output is correct
26 Correct 38 ms 7676 KB Output is correct
27 Correct 22 ms 4808 KB Output is correct
28 Correct 12 ms 3608 KB Output is correct
29 Correct 11 ms 3420 KB Output is correct
30 Correct 12 ms 3864 KB Output is correct
31 Correct 10 ms 2136 KB Output is correct
32 Correct 12 ms 2392 KB Output is correct
33 Correct 15 ms 3612 KB Output is correct
34 Correct 17 ms 3668 KB Output is correct
35 Correct 30 ms 6604 KB Output is correct
36 Correct 82 ms 16556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 103 ms 52860 KB Output is correct
3 Correct 97 ms 52668 KB Output is correct
4 Correct 139 ms 55304 KB Output is correct
5 Correct 156 ms 52920 KB Output is correct
6 Correct 129 ms 52920 KB Output is correct
7 Correct 142 ms 52924 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 0 ms 600 KB Output is correct
10 Correct 245 ms 57052 KB Output is correct
11 Correct 105 ms 54456 KB Output is correct
12 Correct 117 ms 54576 KB Output is correct
13 Correct 114 ms 55736 KB Output is correct
14 Correct 252 ms 57268 KB Output is correct
15 Correct 275 ms 56384 KB Output is correct
16 Correct 262 ms 55096 KB Output is correct
17 Correct 236 ms 56936 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 272 ms 55584 KB Output is correct
20 Correct 251 ms 56712 KB Output is correct
21 Correct 303 ms 55364 KB Output is correct
22 Correct 351 ms 56412 KB Output is correct
23 Correct 237 ms 54764 KB Output is correct
24 Correct 241 ms 55172 KB Output is correct
25 Correct 174 ms 64380 KB Output is correct
26 Correct 152 ms 70944 KB Output is correct
27 Correct 133 ms 75904 KB Output is correct
28 Correct 243 ms 55988 KB Output is correct
29 Correct 228 ms 57360 KB Output is correct
30 Correct 180 ms 62804 KB Output is correct
31 Correct 126 ms 62044 KB Output is correct
32 Correct 224 ms 55428 KB Output is correct
33 Correct 194 ms 56760 KB Output is correct
34 Correct 253 ms 96128 KB Output is correct
35 Correct 229 ms 92796 KB Output is correct
36 Correct 180 ms 77084 KB Output is correct
37 Correct 171 ms 77144 KB Output is correct
38 Correct 203 ms 63872 KB Output is correct
39 Correct 158 ms 61584 KB Output is correct
40 Correct 199 ms 76308 KB Output is correct
41 Correct 194 ms 53692 KB Output is correct
42 Correct 187 ms 66176 KB Output is correct
43 Correct 203 ms 56248 KB Output is correct
44 Correct 247 ms 55736 KB Output is correct
45 Correct 231 ms 55488 KB Output is correct
46 Correct 266 ms 55344 KB Output is correct
47 Correct 277 ms 56248 KB Output is correct
48 Correct 284 ms 56884 KB Output is correct
49 Correct 184 ms 56760 KB Output is correct
50 Correct 220 ms 55104 KB Output is correct
51 Correct 193 ms 57524 KB Output is correct
52 Correct 178 ms 53692 KB Output is correct
53 Correct 1 ms 600 KB Output is correct
54 Correct 243 ms 55996 KB Output is correct
55 Correct 275 ms 56252 KB Output is correct
56 Correct 245 ms 58292 KB Output is correct
57 Correct 291 ms 62536 KB Output is correct
58 Correct 243 ms 63924 KB Output is correct
59 Correct 224 ms 70572 KB Output is correct
60 Correct 188 ms 81328 KB Output is correct
61 Correct 186 ms 82980 KB Output is correct
62 Correct 162 ms 69040 KB Output is correct
63 Correct 230 ms 56760 KB Output is correct
64 Correct 236 ms 64236 KB Output is correct
65 Correct 219 ms 72880 KB Output is correct
66 Correct 166 ms 81704 KB Output is correct
67 Correct 242 ms 67508 KB Output is correct
68 Correct 230 ms 56760 KB Output is correct
69 Correct 283 ms 100912 KB Output is correct
70 Correct 237 ms 92028 KB Output is correct
71 Correct 206 ms 79012 KB Output is correct
72 Correct 188 ms 74880 KB Output is correct
73 Correct 197 ms 64900 KB Output is correct
74 Correct 152 ms 62072 KB Output is correct
75 Correct 198 ms 76496 KB Output is correct
76 Correct 187 ms 54460 KB Output is correct
77 Correct 193 ms 66176 KB Output is correct
78 Correct 210 ms 56504 KB Output is correct
79 Correct 285 ms 62896 KB Output is correct
80 Correct 239 ms 57272 KB Output is correct
81 Correct 280 ms 61100 KB Output is correct
82 Correct 281 ms 62636 KB Output is correct
83 Correct 297 ms 62384 KB Output is correct
84 Correct 227 ms 64448 KB Output is correct
85 Correct 298 ms 55092 KB Output is correct
86 Correct 181 ms 55484 KB Output is correct
87 Correct 173 ms 53684 KB Output is correct
88 Correct 327 ms 64260 KB Output is correct
89 Correct 0 ms 348 KB Output is correct
90 Correct 0 ms 428 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 0 ms 348 KB Output is correct
93 Correct 0 ms 344 KB Output is correct
94 Correct 0 ms 348 KB Output is correct
95 Correct 0 ms 348 KB Output is correct
96 Correct 12 ms 2640 KB Output is correct
97 Correct 11 ms 2652 KB Output is correct
98 Correct 11 ms 2396 KB Output is correct
99 Correct 10 ms 2648 KB Output is correct
100 Correct 11 ms 2652 KB Output is correct
101 Correct 10 ms 2652 KB Output is correct
102 Correct 10 ms 2652 KB Output is correct
103 Correct 10 ms 2652 KB Output is correct
104 Correct 11 ms 2652 KB Output is correct
105 Correct 16 ms 4044 KB Output is correct
106 Correct 18 ms 4148 KB Output is correct
107 Correct 28 ms 6860 KB Output is correct
108 Correct 43 ms 8888 KB Output is correct
109 Correct 48 ms 10420 KB Output is correct
110 Correct 42 ms 9148 KB Output is correct
111 Correct 78 ms 16804 KB Output is correct
112 Correct 52 ms 11200 KB Output is correct
113 Correct 12 ms 3672 KB Output is correct
114 Correct 38 ms 7676 KB Output is correct
115 Correct 22 ms 4808 KB Output is correct
116 Correct 12 ms 3608 KB Output is correct
117 Correct 11 ms 3420 KB Output is correct
118 Correct 12 ms 3864 KB Output is correct
119 Correct 10 ms 2136 KB Output is correct
120 Correct 12 ms 2392 KB Output is correct
121 Correct 15 ms 3612 KB Output is correct
122 Correct 17 ms 3668 KB Output is correct
123 Correct 30 ms 6604 KB Output is correct
124 Correct 82 ms 16556 KB Output is correct
125 Correct 263 ms 56244 KB Output is correct
126 Correct 300 ms 55996 KB Output is correct
127 Correct 298 ms 57412 KB Output is correct
128 Correct 332 ms 65376 KB Output is correct
129 Correct 234 ms 64944 KB Output is correct
130 Correct 234 ms 68268 KB Output is correct
131 Correct 213 ms 73644 KB Output is correct
132 Correct 212 ms 84392 KB Output is correct
133 Correct 171 ms 72360 KB Output is correct
134 Correct 234 ms 58488 KB Output is correct
135 Correct 245 ms 62592 KB Output is correct
136 Correct 236 ms 75952 KB Output is correct
137 Correct 156 ms 67244 KB Output is correct
138 Correct 255 ms 69796 KB Output is correct
139 Correct 215 ms 57012 KB Output is correct
140 Correct 289 ms 108716 KB Output is correct
141 Correct 243 ms 98936 KB Output is correct
142 Correct 202 ms 83840 KB Output is correct
143 Correct 179 ms 76672 KB Output is correct
144 Correct 227 ms 70528 KB Output is correct
145 Correct 166 ms 63352 KB Output is correct
146 Correct 205 ms 76412 KB Output is correct
147 Correct 204 ms 53692 KB Output is correct
148 Correct 201 ms 68316 KB Output is correct
149 Correct 245 ms 55360 KB Output is correct
150 Correct 305 ms 65448 KB Output is correct
151 Correct 267 ms 57468 KB Output is correct
152 Correct 302 ms 65828 KB Output is correct
153 Correct 331 ms 66388 KB Output is correct
154 Correct 311 ms 65964 KB Output is correct
155 Correct 252 ms 65976 KB Output is correct
156 Correct 268 ms 55228 KB Output is correct
157 Correct 207 ms 56292 KB Output is correct
158 Correct 197 ms 53552 KB Output is correct
159 Correct 299 ms 66984 KB Output is correct