codeforces 808d

Array Division

题意:给一个序列,问将一个数调换顺序能否使得序列分为连续的2段后,2段的和相同;

思路:模拟,注意一点,可能第一个数就大于sum/2,所以要1-n模拟一遍再n-1模拟一遍,具体看代码

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=1e5+100;

ll n,a[N],sum,now;
map<ll,int> M1,M2;
int main(){
    scanf("%lld",&n);
    for(int i=1; i<=n; ++i){
        scanf("%lld",a+i);
        sum+=a[i], M1[a[i]]++, M2[a[i]]++;
    }
    if(sum&1){
        cout<<"NO\n";
        return 0;
    }
    for(int i=1; i<n; ++i){
        ll x=(sum/2)-now;
        if(M1[x]>0){
            cout<<"YES\n";
            return 0;
        }
        now+=a[i], M1[a[i]]--;
    }
    now=0;
    for(int i=n; i>1; --i){
        ll x=(sum/2)-now;
        if(M2[x]>0){
            cout<<"YES\n";
            return 0;
        }
        now+=a[i], M2[a[i]]--;
    }
    cout<<"NO\n";
    return 0;
}

 

posted on 2017-10-08 18:01  lazzzy  阅读(172)  评论(0编辑  收藏  举报

导航