Submission #1265414

#TimeUsernameProblemLanguageResultExecution timeMemory
1265414k12_khoiRemittance (JOI19_remittance)C++17
100 / 100
141 ms10416 KiB
#include <bits/stdc++.h>
using namespace std;

const int N=1e6+5;

int n,u,v,x,a[N];
bool CanBeNeg[N],InQueue[N];
queue <int> q;

bool check(int v)
{
    if (CanBeNeg[v]) return (a[v]>0);

    return (a[v]>1);
}

int main()
{
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL); cout.tie(NULL);

    cin >> n;
    for (int i=1;i<=n;i++)
    {
        cin >> a[i] >> x;
        a[i]-=x;
        CanBeNeg[i]=(x!=0);


        if (a[i]>0)
        {
            q.push(i);
            InQueue[i]=true;
        }

    }

    if (n==1)
    {
        if (a[1]==0) cout << "Yes";
        else cout << "No";

        return 0;
    }


    while (!q.empty())
    {
        u=q.front();
        q.pop();

        InQueue[u]=false;

        if (u==n) v=1;
        else v=u+1;


        if (CanBeNeg[u]) x=(a[u]+1)/2;
        else x=a[u]/2;

        a[u]-=x*2;
        a[v]+=x;

        if (!InQueue[v] and check(v))
        {
            q.push(v);
            InQueue[v]=true;
        }
    }

    for (int i=1;i<=n;i++)
    if (a[i]!=0)
    {
        cout << "No";
        return 0;
    }

    cout << "Yes";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...