QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#141461 | #6659. 외곽 순환 도로 2 | penguinman# | 0 | 32ms | 27212kb | C++17 | 4.1kb | 2023-08-17 14:05:56 | 2024-08-26 15:51:13 |
Judging History
answer
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define ln "\n"
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
constexpr ll inf = (1ll<<60);
struct union_find{
ll N;
vi par, sz;
union_find(ll n): N(n){
par.resize(N);
sz.resize(N,1);
rep(i,0,N) par[i] = i;
}
ll root(ll now){
if(par[now] == now) return now;
return par[now] = root(par[now]);
}
void merge(ll u, ll v){
u = root(u);
v = root(v);
if(u == v) return;
if(sz[u] > sz[v]) std::swap(u,v);
sz[v] += sz[u];
sz[u] = 0;
par[u] = v;
}
bool same(ll u, ll v){
return root(u) == root(v);
}
};
long long place_police(vector<int> P, vector<long long> C, vector<long long> W){
ll N = P.size()+1;
vii edge(N), weight(N);
vi par(N,-1), wpar(N,inf);
rep(i,1,N){
edge[P[i-1]].pb(i);
weight[P[i-1]].pb(C[i-1]);
par[i] = P[i-1];
wpar[i] = C[i-1];
}
vi ord;
vi left(N), right(N);
std::function<void(ll)> dfs = [&](ll now){
left[now] = ord.size();
ord.pb(now);
for(auto next: edge[now]){
dfs(next);
}
right[now] = ord.size();
ord.pb(now);
};
dfs(0);
ll K = W.size();
vi V;
rep(i,0,N){
if(left[i] == right[i]-1) V.pb(i);
}
assert(V.size() == K);
vi ml(K), mr(K), mm(K);
rep(i,0,K){
ml[i] = mr[i] = inf;
mm[i] = W[i];
ll dist = 0;
{
ll now = V[i];
ll idx = right[now];
while(true){
ml[i] = std::min(ml[i], wpar[now]);
dist++;
if(ord[idx+1] != par[now]) break;
idx++;
now = par[now];
}
}
{
ll now = V[(i+1)%K];
ll idx = left[now];
while(true){
dist++;
mr[i] = std::min(mr[i], wpar[now]);
if(ord[idx-1] != par[now]) break;
idx--;
now = par[now];
}
}
if(dist%2) mm[i] = 0;
//cout << ml[i] << " " << mr[i] << " " << mm[i] << ln;
}
ll ans = inf;
{
vii dp(K+1, vi(2,inf));
dp[0][0] = 0;
rep(i,0,K){
dp[i+1][0] = dp[i][0]+std::min({ml[i], mr[i]});
dp[i+1][1] = dp[i][1]+std::min({ml[i], mr[i], mm[i]});
dp[i+1][1] = std::min(dp[i+1][1], dp[i][0]+mm[i]);
if(i > 0){
dp[i+1][1] = std::min(dp[i+1][1], dp[i-1][1]+std::max(mr[i-1], ml[i]));
dp[i+1][0] = std::min(dp[i+1][0], dp[i-1][0]+std::max(mr[i-1], ml[i]));
}
}
ans = std::min(ans, dp[K][1]);
if(K%2 == 0) ans = std::min(ans, dp[K][0]);
}
{
{
vi ML(K), MR(K), MM(K);
rep(i,0,K){
ML[i] = ml[(i+K-1)%K];
MR[i] = mr[(i+K-1)%K];
MM[i] = mm[(i+K-1)%K];
}
ml = ML, mr = MR, mm = MM;
}
vii dp(K+1, vi(2,inf));
dp[0][0] = 0;
rep(i,0,K){
dp[i+1][0] = dp[i][0]+std::min({ml[i], mr[i]});
dp[i+1][1] = dp[i][1]+std::min({ml[i], mr[i], mm[i]});
dp[i+1][1] = std::min(dp[i+1][1], dp[i][0]+mm[i]);
if(i > 0){
dp[i+1][1] = std::min(dp[i+1][1], dp[i-1][1]+std::max(mr[i-1], ml[i]));
dp[i+1][0] = std::min(dp[i+1][0], dp[i-1][0]+std::max(mr[i-1], ml[i]));
}
}
ans = std::min(ans, dp[K][1]);
if(K%2 == 0) ans = std::min(ans, dp[K][0]);
}
return ans;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 6
Accepted
time: 0ms
memory: 3820kb
input:
5 0 452912 0 820899 0 79369 0 232463 1000000000000 1000000000000 1000000000000 1000000000000
output:
532281
result:
ok single line: '532281'
Test #2:
score: 6
Accepted
time: 0ms
memory: 3808kb
input:
6 0 581451 0 68556 0 918465 0 661406 0 41816 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000
output:
1000000110372
result:
ok single line: '1000000110372'
Test #3:
score: 6
Accepted
time: 0ms
memory: 3808kb
input:
4 0 0 0 0 0 0 0 0 0
output:
0
result:
ok single line: '0'
Test #4:
score: 6
Accepted
time: 0ms
memory: 3804kb
input:
5 0 938777585449 0 576051802364 0 418735407836 0 823692221300 233950071687 338912182863 866023804654 680391493800
output:
1333076973323
result:
ok single line: '1333076973323'
Test #5:
score: 6
Accepted
time: 0ms
memory: 4100kb
input:
6 0 938777585449 0 576051802364 0 418735407836 0 823692221300 0 233950071687 338912182863 866023804654 680391493800 876313612238 476765859230
output:
991597662386
result:
ok single line: '991597662386'
Test #6:
score: 6
Accepted
time: 11ms
memory: 26052kb
input:
99995 0 573954 1 101503 2 350026 3 832411 4 311022 5 583957 6 894954 7 223392 8 287704 9 259600 10 964702 11 24863 12 831166 13 754666 14 96743 15 606341 16 198920 17 262280 18 610409 19 193417 20 192417 21 194438 22 244016 23 680809 24 106449 25 249873 26 41805 27 375383 28 927874 29 148386 30 1354...
output:
3
result:
ok single line: '3'
Test #7:
score: 6
Accepted
time: 18ms
memory: 24236kb
input:
99995 0 573954 1 101503 2 350026 3 832411 4 311022 5 583957 6 894954 7 223392 8 287704 9 259600 10 964702 11 24863 12 831166 13 754666 14 96743 15 606341 16 198920 17 262280 18 610409 19 193417 20 192417 21 194438 22 244016 23 680809 24 106449 25 249873 26 41805 27 375383 28 927874 29 148386 30 1354...
output:
0
result:
ok single line: '0'
Test #8:
score: 6
Accepted
time: 13ms
memory: 25948kb
input:
99995 0 573954 1 101503 2 350026 3 832411 4 311022 5 583957 6 894954 7 223392 8 287704 9 259600 10 964702 11 24863 12 831166 13 754666 14 96743 15 606341 16 198920 17 262280 18 610409 19 193417 20 192417 21 194438 22 244016 23 680809 24 106449 25 249873 26 41805 27 375383 28 927874 29 148386 30 1354...
output:
3
result:
ok single line: '3'
Test #9:
score: 0
Wrong Answer
time: 18ms
memory: 24064kb
input:
99995 0 573954 1 101503 2 350026 3 832411 4 311022 5 583957 6 894954 7 223392 8 287704 9 259600 10 964702 11 24863 12 831166 13 754666 14 96743 15 606341 16 198920 17 262280 18 610409 19 193417 20 192417 21 194438 22 244016 23 680809 24 106449 25 249873 26 41805 27 375383 28 927874 29 148386 30 1354...
output:
38
result:
wrong answer 1st lines differ - expected: '50', found: '38'
Subtask #2:
score: 0
Wrong Answer
Test #28:
score: 0
Wrong Answer
time: 32ms
memory: 27212kb
input:
99997 0 122727 0 267270 0 846212 0 454122 0 805668 0 614161 0 7805 0 173284 0 684707 0 269129 0 930945 0 1101 0 992427 0 297412 0 759787 0 227130 0 120418 0 90914 0 333684 0 46144 0 519912 0 171490 0 823586 0 121787 0 674177 0 560254 0 753090 0 853359 0 465464 0 655527 0 631303 0 919012 0 597126 0 1...
output:
20632254883
result:
wrong answer 1st lines differ - expected: '24980330181', found: '20632254883'
Subtask #3:
score: 0
Wrong Answer
Test #36:
score: 5
Accepted
time: 0ms
memory: 3780kb
input:
11 0 9 0 8 2 0 3 7 3 1 2 6 0 0 7 7 7 1 9 6 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000
output:
1
result:
ok single line: '1'
Test #37:
score: 0
Wrong Answer
time: 13ms
memory: 12860kb
input:
50311 0 630582 1 458618 2 300543 3 566041 4 306718 5 134260 6 736322 7 458543 8 609374 9 355623 10 706939 11 48588 12 455725 13 105118 14 71071 15 528699 16 423538 17 471781 18 98063 19 169099 20 657181 21 295537 22 49937 23 306612 24 186582 25 505763 26 831500 27 406268 28 294626 29 128111 30 42115...
output:
427455
result:
wrong answer 1st lines differ - expected: '813491', found: '427455'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #77:
score: 0
Wrong Answer
time: 10ms
memory: 12676kb
input:
50311 0 962897543825 1 887020369743 2 363658802934 3 481009844166 4 1099712574 5 858320882162 6 521927434762 7 379344260539 8 73024776148 9 634183458545 10 869560347910 11 81581323331 12 750044298516 13 307013017409 14 306226274039 15 423923546601 16 482114694167 17 849292461119 18 299993045938 19 7...
output:
619471179450
result:
wrong answer 1st lines differ - expected: '939418184213', found: '619471179450'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%