CF1474D - Cleaning

I thought this question was complicated at first.

Make a difference p from front to back, and make a difference s from back to front. If the previous time is illegal, this time will also be marked.

Exchange adjacent a[i], a[i+1], only affect p[i-1], a[i], a[i+1], s[i+2].

Judge whether p[i-1], a[i+1], a[i], s[i+2] can satisfy the situation.

I don't know why std is so ugly. . .

 

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define lep(i,a,b) for(int i=(a);i>=(b);i--) 
#define pii pair<int,int>
#define pll pair<long long,long long>
#define mp make_pair
#define All(x) x.begin(),x.end() 
#define ms(a,b) memset(a,b,sizeof(a)) 
#define INF 0x3f3f3f3f
#define INFF 0x3f3f3f3f3f3f3f3f
#define multi int T;scanf("%d",&T);while(T--) 
using namespace std;
typedef long long ll;
typedef double db;
const int N=2e5+5;
const int mod=1e9+7;
const db eps=1e-6;                                                                            
const db pi=acos(-1.0);
int n,m;
ll a[N],p[N],s[N],b[N];
int check(ll* a,int len){
    a[0]=0;
    rep(i,1,len){
        a[i]-=a[i-1];
        if(a[i]<0) return 0;
    }
    if(a[len]==0) return 1;
    return 0;
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("D:\\work\\data.in","r",stdin);
    #endif
    multi{
        cin>>n;
        rep(i,1,n){
            cin>>a[i];
            b[i]=a[i];
            if(p[i-1]==-1) p[i]=-1;
            else{
                p[i]=a[i]-p[i-1];
                if(p[i]<0) p[i]=-1;
            }
        }
        s[n+1]=0;
        lep(i,n,1){
            if(s[i+1]==-1) s[i]=-1;
            else{
                s[i]=a[i]-s[i+1];
                if(s[i]<0) s[i]=-1;
            }
        }
        int flag=(check(b,n)?1:0);
        rep(i,1,n-1){
            if(flag) break;
            ll c[5]={0,p[i-1],a[i+1],a[i],s[i+2]};
            if(p[i-1]==-1||s[i+2]==-1) continue;
            if(check(c,4)) flag=1;
        }
        cout<<(flag?"YES":"NO")<<endl;
    }
}

 

Guess you like

Origin blog.csdn.net/Luowaterbi/article/details/112912406