QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#129158 | #6406. Stage Clear | Energy_is_not_over | TL | 3898ms | 138696kb | C++17 | 10.4kb | 2023-07-22 01:57:45 | 2023-07-22 01:57:49 |
Judging History
answer
//
// Created by Barichek on 21.07.2023 09:32:09
//
#include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define all(a) a.begin(), a.end()
#define len(a) (int) (a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
#ifdef Energy_is_not_over
#define DEBUG for (bool ____DEBUG = true; ____DEBUG; ____DEBUG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template<class ...Ts>
auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif
const int max_n = -1, inf = 1000111222;
const ll infll = 1e18;
inline bool get_bit(ll mask,int bit)
{
return (mask>>bit)&1;
}
const int N=36;
const int K=24;
const int max_E_non_spanning_tree=72-((K+1)-K);
ll sum_d_left[1ll<<(N-N/2)];
ll sum_d_right[1ll<<(N/2)];
ll dp[1ll<<K];
int n,m;
ll e[N];
ll rev_e[N];
ll a[N];
ll d[N];
ll d_plus_a[N];
int non_spanning_tree_u[max_E_non_spanning_tree];
int non_spanning_tree_v[max_E_non_spanning_tree];
int cnt_non_spanning_tree;
int rem_p[N];
int p[N];
vector<int> reb[N];
pair<ll,int> greedy_down[N];
//pair<ll,int> greedy_up[N];
void dfs_greedy_down(int start,int now,ll A,ll D)
{
if (D>=0){
greedy_down[start]=min(greedy_down[start],mp(A,now));
}
for (auto wh:reb[now]){
dfs_greedy_down(start,wh,max(A,a[wh]-D),D+d[wh]);
}
}
//void dfs_greedy_up(int start,int now,ll A,ll D)
//{
// if (D>=0){
// greedy_up[start]=min(greedy_up[start],mp(A,now));
// }
// if (now!=0){
// dfs_greedy_up(start,p[now],max(A,d[p[now]]+a[p[now]]-D),D-d[p[now]]);
// }
//}
void dfs_greedy(int now)
{
greedy_down[now]=mp(infll,-1);
dfs_greedy_down(now,now,a[now],d[now]);
// greedy_up[now]=mp(infll,-1);
// dfs_greedy_up(now,now,d[now]+a[now],-d[now]);
for (auto wh:reb[now]){
dfs_greedy(wh);
}
}
struct elem
{
ll a,b;
};
elem merge(elem lhs,elem rhs)
{
ll a=max(lhs.a,lhs.a-lhs.b+rhs.a);
return elem{a,lhs.b+rhs.b-lhs.a-rhs.a+a};
}
bool operator<(const elem& lhs,const elem& rhs)
{
bool negl = lhs.a > lhs.b;
bool negr = rhs.a > rhs.b;
if (negl != negr) return negl < negr;
else if (negl == 0) return lhs.a < rhs.a;
else return lhs.b > rhs.b;
}
elem elems[N];
set<pair<elem,int>> setik;
int ppp[N];
int f_ppp(int v)
{
return ppp[v]==v?v:ppp[v]=f_ppp(ppp[v]);
}
ll get_ans_from_p(ll known_ans)
{
DEBUG{
cerr<<"p :: ";
for (int i=0;i<n;i++){
cerr<<i<<","<<p[i]<<" ";
}
cerr<<"\n";
};
ppp[0]=0;
elems[0]=elem{0,0};
setik.clear();
for (int i=1;i<n;i++){
ppp[i]=i;
elems[i]=elem{a[i],d[i]+a[i]};
setik.insert(mp(elems[i],i));
}
while (!setik.empty()){
DEBUG{
cerr<<"setik :: ";
for (auto i:setik){
cerr<<i.fir.a<<","<<i.fir.b<<" ";
}
cerr<<"\n";
}
auto now=*setik.begin();
LOG(now.sec);
setik.erase(setik.begin());
ppp[now.sec]=p[now.sec];
int u=f_ppp(now.sec);
if (u==0){
elems[0]=merge(elems[0],now.fir);
LOG("to elems[0]",now.fir.a,now.fir.b);
}
else{
LOG("to elems[u]",u,now.fir.a,now.fir.b);
setik.erase(mp(elems[u],u));
elems[u]=merge(elems[u],now.fir);
setik.insert(mp(elems[u],u));
}
}
return elems[0].a;
// for (int i=0;i<n;i++){
// reb[i].clear();
// }
// for (int i=1;i<n;i++){
// reb[p[i]].pb(i);
// }
// dfs_greedy(0);
//
// ll l=0,r=min(ll(N*1e15+10),(-known_ans));
// if (!check_using_p(r)){
// return known_ans;
// }
// while (r-l>0){
// ll m=(l+r)/2;
// if (check_using_p(m)){
// r=m;
// }
// else{
// l=m+1;
// }
// }
//// LOG("get_ans_from_p answer is",-l);
// return -l;
}
// 255214703962898
const int gen_test=0;
const bool repeat=0;
const bool not_goto_start=1;
mt19937_64 gen64(123);
/*
5 5
2 1
1 1
2 1
2 2
1 2
2 3
3 4
4 5
1 4
*/
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
LOG(elem{2,-1}<elem{2,0});
LOG(elem{2,0}<elem{2,-1});
start_of_program:;
for (int i=0;i<N;i++){
e[i]=0;
rev_e[i]=0;
}
if (gen_test==1){
static int test_id=0;
const ll M=2;
n=5;
m=5;
cout<<"test_id :: "<<(test_id++)<<"\n";
cout<<"new test"<<"\n";
cout<<n<<" "<<m<<"\n";
for (int i=1;i<n;i++){
ll aa,bb;
aa=gen64()%M+1;
bb=gen64()%M+1;
cout<<aa<<" "<<bb<<"\n";
a[i]=aa;
d[i]=bb-aa;
d_plus_a[i]=d[i]+a[i];
}
for (int i=0;i<m;i++){
int u,v;
if (i<n-1){
u=i;
v=i+1;
}
else{
do{
u=gen64()%n;
v=gen64()%n;
}
while (u>=v);
}
cout<<u+1<<" "<<v+1<<"\n";
e[u]|=(1ll<<v);
rev_e[v]|=(1ll<<u);
}
}
else{
cin>>n>>m;
for (int i=1;i<n;i++){
ll aa,bb;
cin>>aa>>bb;
a[i]=aa;
d[i]=bb-aa;
d_plus_a[i]=d[i]+a[i];
}
for (int i=0;i<m;i++){
int u,v;
cin>>u>>v;
u--;
v--;
e[u]|=(1ll<<v);
rev_e[v]|=(1ll<<u);
}
}
assert(n<=N);
// DEBUG{
// for (int i=1;i<n;i++){
// LOG(i,a[i],d[i],d_plus_a[i]);
// }
// };
// for (int i=0;i<n;i++){
// sum_all_d+=d[i];
// }
// for (int i=0;i<n;i++){
// sorted_list_of_d_plus_a[i]=(d_plus_a[i]<<8)+i;
// }
// sort(sorted_list_of_d_plus_a,sorted_list_of_d_plus_a+n);
// for (int i=0;i<n;i++){
// if (i!=0){
// mask_on_pref_of_sorted_list_of_d_plus_a[i]=mask_on_pref_of_sorted_list_of_d_plus_a[i-1];
// }
// mask_on_pref_of_sorted_list_of_d_plus_a[i]|=(1ll<<(sorted_list_of_d_plus_a[i]&((1ll<<8)-1)));
// sorted_list_of_d_plus_a[i]>>=8;
// }
ll ans_for_repeat=-1;
if (repeat || n<=K){
for (int mask=0;mask<(1ll<<(N/2));mask++){
for (int i=0;i<(N/2);i++){
if (get_bit(mask,i)){
sum_d_right[mask]=sum_d_right[mask^(1ll<<i)]+d[i];
break;
}
}
}
for (int mask=0;mask<(1ll<<(N-N/2));mask++){
for (int i=0;i<(N-N/2);i++){
if (get_bit(mask,i)){
sum_d_left[mask]=sum_d_left[mask^(1ll<<i)]+d[N/2+i];
break;
}
}
}
for (int mask=0;mask<(1ll<<n);mask++){
dp[mask]=-infll;
}
dp[(1ll<<0)]=0;
for (int mask=0;mask<(1ll<<n);mask++){
ll cur_sum_d = sum_d_right[mask&((1ll<<(N/2))-1)] + (n<=(N/2)?0:sum_d_left[(mask&(((1ll<<n)-1)-((1ll<<(N/2))-1)))>>(N/2)]);
for (int i=0;i<n;i++){
if (!get_bit(mask,i) && (rev_e[i]&mask)!=0){
// LOG("upd",bitset<10>(mask),i,dp[mask],cur_sum_d-a[i]);
dp[mask^(1ll<<i)]=max(dp[mask^(1ll<<i)],min(dp[mask],cur_sum_d-a[i]));
}
}
}
cout<<-dp[(1ll<<n)-1]<<"\n";
ans_for_repeat=-dp[(1ll<<n)-1];
}
if (repeat || n>K){
// min_d_right[0]=infll;
// for (int mask=1;mask<(1ll<<(N/2));mask++){
// for (int i=0;i<(N/2);i++){
// if (get_bit(mask,i)){
// min_d_right[mask]=min(min_d_right[mask^(1ll<<i)],(d[i]<<8)+i);
// break;
// }
// }
// }
// min_d_left[0]=infll;
// for (int mask=0;mask<(1ll<<(N-N/2));mask++){
// for (int i=0;i<(N-N/2);i++){
// if (get_bit(mask,i)){
// min_d_left[mask]=min(min_d_left[mask^(1ll<<i)],(d[N/2+i]<<8)+(N/2+i));
// break;
// }
// }
// }
assert(m-(n-1)<=max_E_non_spanning_tree);
cnt_non_spanning_tree=0;
for (int i=1;i<n;i++){
for (int j=0;j<n;j++){
if (get_bit(rev_e[i],j)){
if ((rev_e[i]>>(j+1))==0){
rem_p[i]=j;
// LOG("rem_p",i,j);
break;
}
else{
non_spanning_tree_u[cnt_non_spanning_tree]=j;
non_spanning_tree_v[cnt_non_spanning_tree]=i;
cnt_non_spanning_tree++;
}
}
}
}
assert(cnt_non_spanning_tree<=max_E_non_spanning_tree);
ll ans=infll;
for (int mask=0;mask<(1ll<<cnt_non_spanning_tree);mask++){
vector<bool> changed(n,0);
bool ok=1;
// LOG("new mask of cnt_non_spanning_tree");
for (int i=0;i<n;i++){
p[i]=rem_p[i];
}
for (int i=0;i<cnt_non_spanning_tree;i++){
if (get_bit(mask,i)){
// LOG("changed p",non_spanning_tree_v[i],non_spanning_tree_u[i]);
if (changed[non_spanning_tree_v[i]]){
ok=0;
}
changed[non_spanning_tree_v[i]]=1;
p[non_spanning_tree_v[i]]=non_spanning_tree_u[i];
}
}
if (!ok){
continue;
}
ans=min(ans,get_ans_from_p(ans));
}
cout<<ans<<"\n";
if (repeat && ans!=ans_for_repeat){
cerr<<ans<<" "<<ans_for_repeat<<"\n";
exit(123);
}
}
if (repeat && !not_goto_start){
goto start_of_program;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 7572kb
input:
4 4 4 2 5 3 2 6 1 2 1 3 2 4 3 4
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 4ms
memory: 7944kb
input:
15 14 254040392438309 117083115436273 500005748229691 557255157630172 821034233718230 865199673774998 659892147898798 987564141425694 81172575487567 811635577877255 751768357864605 341103322647288 454926350150218 140191090713900 921608121471585 659295670987251 223751724062143 505619245326640 8907765...
output:
1665396301509143
result:
ok 1 number(s): "1665396301509143"
Test #3:
score: 0
Accepted
time: 14ms
memory: 11216kb
input:
18 17 636830992776530 847574431876821 330869946457865 78274534165482 450581372553540 11565219334965 8736347226844 17186323694285 870805093198860 559070167736042 674369178493171 930151818400874 641605209598997 222521062460239 450936030349531 469197172169023 831295459816974 626096008793091 53095460351...
output:
2375957544280218
result:
ok 1 number(s): "2375957544280218"
Test #4:
score: 0
Accepted
time: 63ms
memory: 17016kb
input:
20 19 539893468691183 767805205447882 240338186903141 960937349402327 942645580569365 896509929612645 542601575005817 191461109090531 540992546866047 765080044816119 904535155855114 858111921213175 452499200048240 115895143306864 983856946412026 838504718536099 586421298181479 265212699386882 677124...
output:
800919806038419
result:
ok 1 number(s): "800919806038419"
Test #5:
score: 0
Accepted
time: 1104ms
memory: 138696kb
input:
24 23 114281007218527 308690671179962 145951034437731 718976086594208 709172151907814 926071954787084 224496444610281 498657753059525 874422017133378 857676356343078 532175866197017 818525693672607 303837639402605 374469705563954 512244364294540 952911486867703 748959419417502 249992707230361 512696...
output:
114281007218527
result:
ok 1 number(s): "114281007218527"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3512kb
input:
36 35 389328367777319 678636570542258 32216944647452 612585362150577 891592845704885 596030605892036 688825276167602 461516360471825 916552899998310 106733202183953 400050408958777 670724326933521 995792861502757 894514508573875 14511185222713 612305257166443 175168368096281 508263855969282 85578802...
output:
171942144116875
result:
ok 1 number(s): "171942144116875"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3488kb
input:
36 35 759037289890767 849577210686635 16379509883489 441829377955433 589378488455351 990818352083122 871208015900506 727359003875494 207852561142533 28766987248469 81321183353129 892618157632070 198487099788393 519364502513651 83942803274015 988821788304459 868185445880277 269956013388079 3834515054...
output:
759037289890767
result:
ok 1 number(s): "759037289890767"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3528kb
input:
36 35 100792831728257 823656493168793 866936535786311 187861146327778 132998929717538 605906559206892 3319598846477 393401056223733 964444786730964 932398059281618 925176496607384 148825907337833 985037559482190 646827297289525 469876125353024 641923164294854 453796287874442 291205025001534 72806942...
output:
1397699717661157
result:
ok 1 number(s): "1397699717661157"
Test #9:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
36 36 245996406159980 462974248377488 839352152971124 40282565369163 572695144110271 507726167903341 671102350267895 18090181781241 643724978558334 551787913319524 936340565446887 517649577919257 158127116487034 175750969611510 396852573858996 670814068366285 534702788102341 124550558279140 69441153...
output:
2508008255775889
result:
ok 1 number(s): "2508008255775889"
Test #10:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
34 38 656738239290510 319959252044415 760511943177376 828562698756504 470087249708484 441916827764162 105399930988058 761192720347117 81742549616394 195819875734286 782982110569406 72384154665629 647269989285797 720280547207448 531182311814386 160821851115134 292963780645658 871789628567253 74499577...
output:
656738239290510
result:
ok 1 number(s): "656738239290510"
Test #11:
score: 0
Accepted
time: 2ms
memory: 3504kb
input:
32 40 818105834607446 689904077664886 717146597564762 686987602224041 538827104521875 147060924732538 604913134601443 802546720879673 45376965619246 480061093729529 686039951678173 889398415870480 374408509732957 354006189233817 103818950629279 863526642478066 719174876808085 130061851080766 9744074...
output:
2289520618562758
result:
ok 1 number(s): "2289520618562758"
Test #12:
score: 0
Accepted
time: 19ms
memory: 3484kb
input:
30 42 730678486091139 762413502107242 564137648972911 492217680357057 677122869459914 634406715345550 766223620461328 750896882727596 34139073751269 875301336250330 948602995486093 589201509496124 333847023521138 673322700954330 774661538057122 360743409997856 301647343463502 78371781314140 44979585...
output:
2296677982487339
result:
ok 1 number(s): "2296677982487339"
Test #13:
score: 0
Accepted
time: 288ms
memory: 3596kb
input:
28 44 996216744822715 15265122654307 591377215147374 392892022614182 134817686923570 666778840251745 603108267679560 939679039946396 792878600465606 943254465658609 705582931165204 626247204621328 833947774992752 802610518921019 60510220659563 935537906466250 900509663884138 957082020010408 38517385...
output:
1021065751521024
result:
ok 1 number(s): "1021065751521024"
Test #14:
score: 0
Accepted
time: 1012ms
memory: 3468kb
input:
27 45 271265179156100 385209948242010 548010795825703 286502371912374 203557541769729 336737491323929 32253800857105 902537647325928 835008409588714 227495683621084 573457473959732 478446911624066 447407603972649 401150715116732 597962487418392 594931676764990 326718612562917 293848561935121 6497688...
output:
271265179156100
result:
ok 1 number(s): "271265179156100"
Test #15:
score: 0
Accepted
time: 3898ms
memory: 3532kb
input:
26 46 511128167626061 755154773895250 469460004382432 144928349121735 272299544034000 41881588292305 453271611317466 830211882616629 877138218711823 441367083696839 476515315035731 252150151731957 174547198161633 921197665643069 56919360991429 297636468095153 717743189152864 552120784448634 95767590...
output:
511128167626061
result:
ok 1 number(s): "511128167626061"
Test #16:
score: -100
Time Limit Exceeded
input:
25 47 483175861091928 628662160345159 414348784525954 991346283769736 118134342611258 254055400216860 367817156249062 195226919472367 228751017881407 501458690109441 595787759089619 364958390117603 758404493344385 423811540220990 373421064986368 503851495028044 645521325517401 846860937023068 696132...