QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#129137#6406. Stage ClearEnergy_is_not_overWA 1027ms138828kbC++1710.8kb2023-07-21 23:15:062023-07-21 23:15:09

Judging History

This is the latest submission verdict.

  • [2024-08-15 21:05:17]
  • hack成功,自动添加数据
  • (/hack/778)
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-21 23:15:09]
  • Judged
  • Verdict: WA
  • Time: 1027ms
  • Memory: 138828kb
  • [2023-07-21 23:15:06]
  • Submitted

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 qEnergy_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=26;
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);
    }
}

bool in_q[N];
int cur_cnt_down[N];
bool mark[N];

bool check_using_p(ll ANS)
{
    for (int i=0;i<n;i++){
        mark[i]=0;
    }
    {
        for (int i=0;i<n;i++){
            in_q[i]=0;
        }
        in_q[0]=1;
        ll D=0;
        while (1){
            bool ok=0;
            for (int i=0;i<n;i++){
                if (in_q[i]){
                    if (D-greedy_down[i].fir>=-ANS){
                        int cur=greedy_down[i].sec;
                        int nxt_cur=-1;
                        while (1){
                            mark[cur]=1;
                            D+=d[cur];
                            for (auto wh:reb[cur]){
                                if (wh!=nxt_cur){
                                    in_q[wh]=1;
                                }
                            }
                            if (cur==i){
                                break;
                            }
                            nxt_cur=cur;
                            cur=p[cur];
                        }
                        in_q[i]=0;
                        ok=1;
                        break;
                    }
                }
            }
            if (!ok){
                break;
            }
        }
    }

    {
        for (int i=0;i<n;i++){
            cur_cnt_down[i]=0;
        }
        for (int i=1;i<n;i++){
            cur_cnt_down[p[i]]++;
        }
        ll D=0;
        for (int i=0;i<n;i++){
            D+=d[i];
        }
        while (1){
            bool ok=0;
            for (int i=0;i<n;i++){
                if (cur_cnt_down[i]==0){
                    ll cur_D=-d[i];
                    ll cur_A=d[i]+a[i];
                    int cur=i;
                    while (cur_cnt_down[cur]<=1){
                        if (cur_D>=0 && D-cur_A>=-ANS){
                             int ff=i;
                             while (1){
                                 cur_cnt_down[ff]--;
                                 D-=d[ff];
                                 mark[ff]=1;
                                 if (ff==cur){
                                     if (ff!=0){
                                         cur_cnt_down[p[ff]]--;
                                     }
                                     break;
                                 }
                                 ff=p[ff];
                             }
                        }
                        if (cur==0){
                            break;
                        }
                        cur=p[cur];
                        cur_A=max(cur_A,d[cur]+a[cur]-cur_D);
                        cur_D-=d[cur];
                    }
                }
            }
            if (!ok){
                break;
            }
        }
    }

    for (int i=0;i<n;i++){
        if (!mark[i]){
            return false;
        }
    }
    return true;
}

ll get_ans_from_p(ll known_ans)
{
    DEBUG{
        cerr<<"p :: ";
        for (int i=0;i<n;i++){
            cerr<<i<<","<<p[i]<<" ";
        }
        cerr<<"\n";
    };

    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;

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    if (gen_test==1){
        const ll M=1e15;
        mt19937_64 gen64(474747);
        n=K;
        m=72-K;
        for (int i=1;i<n;i++){
            ll aa,bb;
            aa=gen64()%M+1;
            bb=gen64()%M+1;
            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);
            }
            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;
//    }

    if (1 && 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";
    }
    else{
//        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);
        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++){
            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]);
                    p[non_spanning_tree_v[i]]=non_spanning_tree_u[i];
                }
            }
            ans=max(ans,get_ans_from_p(ans));
        }
        cout<<-ans<<"\n";
    }
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 7692kb

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: 1ms
memory: 7792kb

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: 13ms
memory: 10596kb

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: 62ms
memory: 16256kb

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: 1027ms
memory: 138828kb

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: -100
Wrong Answer
time: 0ms
memory: 3440kb

input:

36 35
389328367777319 678636570542258
32216944647452 612585362150577
891592845704885 596030605892036
688825276167602 461516360471825
916552899998310 106733202183953
400050408958777 670724326933521
995792861502757 894514508573875
14511185222713 612305257166443
175168368096281 508263855969282
85578802...

output:

699055300039387

result:

wrong answer 1st numbers differ - expected: '171942144116875', found: '699055300039387'