#include "closing.h"
#include <vector>
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll big = 2e18;
struct line{
ll i,w;
};
int max_score(int N, int X, int Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W){
ll n = N;
ll x = X;
ll y = Y;
ll k = K;
vector<vector<line>> con(n);
ll is_line = 1;
for(ll i = 0;i<n-1;i++){
con[U[i]].push_back({V[i],W[i]});
con[V[i]].push_back({U[i],W[i]});
if(U[i]!=i||V[i]!=i+1) is_line = 0;
}
if (is_line){
auto &w = W;
vector<ll> pw(n,0);
for(ll i = 0;i<n-1;i++) pw[i+1] = pw[i]+w[i];/*
vector<ll> v,cc(n,0);
for(ll i = 0;i<x;i++){
v.push_back(cc[i] = pw[x]-pw[i]);
}
for(ll i = y+1;i<n;i++){
v.push_back(cc[i] = pw[i]-pw[y]);
}
vector<ll> pc(n+1,0);
for(ll i = 0;i<n;i++) pc[i+1] = cc[i]+pc[i];
sort(v.begin(),v.end());
for(ll i = 1;i<v.size();i++) v[i]+=v[i-1];
ll ans = 0;
vector<ll> h(n,0),h1(n,0);
ll lv1 = 0;
for(ll i = x;i<n;i++){
h[i] = pw[i]-pw[x];
h1 = h;
lv1 += h[i];
ll lv2 = lv1;
for(ll j = y;j>=0;j--){
h1[j] = max(h[j],pw[y]-pw[j]);
lv2 += max(pw[y]-pw[j]-h[j],0ll);
if (lv2>k) break;
ll g = 2+i-x+y-j;
vector<ll> v;
for(ll p = 0;p<x;p++){
v.push_back(max(0ll,pw[x]-pw[p]-h[p]));
}
for(ll p = y+1;p<n;p++){
v.push_back(max(0ll,pw[p]-pw[y]-h[p]));
}
sort(v.begin(),v.end());
ll cur = lv2;
for(ll i : v) if(cur+i<=k){
cur += i;
g++;
}
ans = max(ans,g);
}
}*/
ll ans = 0;
ll v1 = 0;
for(ll a = x;a>=0;a--){
v1 += abs(pw[a]-pw[x]);
ll v2 = v1;
for(ll b = x;b<n;b++){
v2 += abs(pw[b]-pw[x]);
ll v3 = v2;
for(ll c = y;c>=0;c--){
v3 += max(0ll,abs(pw[c]-pw[y])-(c>=a&&c<=b?abs(pw[c]-pw[x]):0));
ll v4 = v3;
for(ll d = y;d<n;d++){
v4 += max(0ll,abs(pw[d]-pw[y])-(d>=a&&d<=b?abs(pw[d]-pw[x]):0));
if(v4<=k){
ans = max(ans,b-a+d-c+2);
}
}
}
}
}
return ans;
}
vector<ll> dis1(n,big),dis2(n,big);
{
function<void(ll,ll)> dfs;
dfs = [&](ll i, ll v){
if(dis1[i]>v){
dis1[i] = v;
for(auto [j,w] : con[i]){
dfs(j,v+w);
}
}
};
dfs(x,0);
}
{
function<void(ll,ll)> dfs;
dfs = [&](ll i, ll v){
if(dis2[i]>v){
dis2[i] = v;
for(auto [j,w] : con[i]){
dfs(j,v+w);
}
}
};
dfs(y,0);
}
ll l = 0, r = big;
while(l<r){
ll m = l+r+1>>1;
ll sm = 0;
for(ll i = 0;i<n;i++){
ll mi = min(dis1[i],dis2[i]);
ll mx = max(dis1[i],dis2[i]);
if(m>=mx) sm+=mx;
else if(m>=mi) sm+=mi;
}
if(sm>k) r = m-1;
else l = m;
}
ll cur = 0, ans = 0;
vector<ll> v;
for(ll i = 0;i<n;i++){
ll mi = min(dis1[i],dis2[i]);
ll mx = max(dis1[i],dis2[i]);
if (mx<=l){
cur += mx;
ans += 2;
}else if (mi<=l){
cur += mi;
ans += 1;
if (mx==l+1){
v.push_back(mx-mi);
}
}else if(mi==l+1){
v.push_back(mi);
}
}
sort(v.begin(),v.end());
for(ll i : v){
if(cur+i<=k){
ans += 1;
cur += i;
}
}
return ans;
}
Compilation message
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:117:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
117 | ll m = l+r+1>>1;
| ~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
28644 KB |
Output is correct |
2 |
Correct |
159 ms |
34712 KB |
Output is correct |
3 |
Correct |
66 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
440 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Execution timed out |
1097 ms |
348 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
3 ms |
440 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Execution timed out |
1097 ms |
348 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
1097 ms |
348 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
1097 ms |
348 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
1097 ms |
348 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |