QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#708002#8056. Travel 2WolamTL 0ms0kbC++172.6kb2024-11-03 18:49:242024-11-03 18:49:31

Judging History

你现在查看的是最新测评结果

  • [2024-11-03 18:49:31]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-11-03 18:49:24]
  • 提交

answer

#include<bits/stdc++.h>
#define endl "\n"
#define int long long
using namespace std;
using ll = long long;
const int maxn = 5e5 + 10;
ll cnt1[maxn],cnt2[maxn],vis[maxn];
void solve(void)
{
    bool carry=false;
    int m,mn1=0x3f3f3f3f,mn2=0x3f3f3f3f,mx1=-0x3f3f3f3f,mx2=-0x3f3f3f3f;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        cin>>cnt1[i];
        vis[i]=cnt1[i]>0;
    }
    for(int i=0;i<m;i++)
    {
        cin>>cnt2[i];
    }
    for(int i=m-2;i>=0;i--)
    {
        vis[i]|=vis[i+1];
    }
    for(int i=1;i<m;i++)
    {
        if(vis[m-i]&&cnt2[i])
        {
            carry=true;
        }
    }
    if(!carry)
    {
        cout<<0<<endl;
        return;
    }
    queue<int> q;
    for(int i=0;i<m;i++)
    {
        q.push(i);
    }
    ll ans=0;
    bool valid=false;
    for(int i=m-2;i>=0;i--)
    {
        while(!q.empty()&&q.front()+i<m-1)
        {
            q.pop();
        }
        while(cnt1[i]&&!q.empty())
        {
            int res=min(cnt2[q.front()],cnt1[i]);
            if(cnt1[i]&&res)
            {
                mn1=min(mn1,i);
            }
            if(cnt2[q.front()]&&res)
            {
                mn2=min(mn2,q.front());
            }
            cnt1[i]-=res;
            cnt2[q.front()]-=res;
            ans+=res;
            if(i+q.front()>=m&&res)
            {
                valid=true;
            }
            if(cnt2[q.front()]==0)
            {
                q.pop();
            }
        }
    }
    for(int i=0;i<m&&cnt1[m-1];i++)
    {
        if(cnt2[i])
        {
            int res=min(cnt1[m-1],cnt2[i]);
            cnt1[m-1]-=res;
            cnt2[i]-=res;
            ans+=res;
            valid|=(i+m-1>=m);
            mn1=min(mn1,m-1);
            mn2=min(mn2,i);
        }
    }
    if(valid)
    {
        ans+=cnt1[m-1];
        ans+=cnt2[m-1];
        cout<<ans<<endl;
    }
    else
    {
        ans+=cnt1[m-1];
        ans+=cnt2[m-1];
        cnt1[m-1]=cnt2[m-1]=0;
        for(int i=m-1;i>=0;i--)
        {
            if(cnt1[i])
            {
                mx1=max(mx1,i);
            }
            if(cnt2[i])
            {
                mx2=max(mx2,i);
            }
        }
        if(mn1>=mx1&&mn2>=mx2)
        {
            ans--;
        }
        ans=max(0ll,ans);
        cout<<ans<<endl;
    }
}
signed main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
/*
1
10
0 0 3 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 3 3
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

2
1 1

output:


result: