题意:给你n(n<=100000)个数其中每个数a,满足(1<=ai<=i)要求确定每个数得正负号,使的所有总=0;
个人感想:
一开始我也懵逼了,我感觉我怎么也不会做了这题,我觉得我应该直接看题解好了,但是我还是想了一会,我写了个很low的暴力,拍出yes和no,这样对拍我的想法,结果居然对了…而且和我的数据拍的都对,我的想法是这样例如(1,1,2,2,3,3,4,6)那么我用大的减去次大的数-》(1,1,2,2,3,3),再操作一次,(1,1,2,2)再操作(1,1)如果剩下的1的个数是偶数,那么证明是YES,至于为什么这样,反正我也想不通,至此我没看过题解,先假设我当前成立吧.看看分析咯.
分析:
我用的是dfs的想法并用优先队列来维护数组数字大小,dfs的用处:确定符号,可能疑点在这样做如何确定符号,我们知道dfs肯定是可以回溯的.所以我想通过dfs来帮我记忆,为什么?例如如果出现(6,4)相减求出来的答案我要的是 - 号,我每次操作默认的是大减小,可是当我的序列里需要的是 -2,那么我的(6,4)相减就要改变方向了.所以我建立了一个Point类,其中属性x记录值,id记录数字的id(正的就是我当前数字是输入,负的代表我当前数字是通过dfs某个步骤得出来的,但我需要这个步骤得id,通过回溯,我就能确定当前的dfs步骤 相减的方向)!!. 然后把符号记录在zf【maxn】中,如果答案符合,再输出这个zf【maxn】数组.思想比较奇怪,具体看看代码,可能就明白了.
代码:
/* Author:GavinjouElephant
* Title:
* Number:
* main meanning:
*
*
*
*/
//#define OUT
#include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <set>
#include <cstdlib>
#include <map>
#include <queue>
//#include<initializer_list>
//#include <windows.h>
//#include <fstream>
//#include <conio.h>
#define MaxN 0x7fffffff
#define MinN -0x7fffffff
#define Clear(x) memset(x,0,sizeof(x))
const int INF=0x3f3f3f3f;
const int maxn=100005;
int N;
int zf[maxn];
bool inde[maxn];
bool flag;
class Point
{
public:
int x;
int id;
};
struct cmp
{
bool operator()(const Point & a,const Point &b)
{
if(a.x<b.x)return true;
return false;
}
};
void init()
{
flag=true;
memset(zf,0,sizeof(zf));
memset(inde,0,sizeof(inde));
}
void dfs(int index,priority_queue<Point,vector<Point>,cmp> &t)
{
if(t.size()==1){flag=false;return;}
if(t.size()==2&&t.top().x==1)
{
flag=true;
Point nowx=t.top();
t.pop();
Point nowy=t.top();
t.pop();
if(nowx.id>=0)
{
zf[nowx.id]=1;
}
else
{
inde[-nowx.id]=false;
}
if(nowy.id>=0)
{
zf[nowy.id]=-1;
}
else
{
inde[-nowy.id]=true;
}
return;
}
Point x=t.top();
t.pop();
Point y=t.top();
t.pop();
if( (x.x-y.x) !=0)
{
Point tmp;
tmp.x=x.x-y.x;
tmp.id=-index;
t.push(tmp);
}
dfs(index+1,t);
if(!flag)return;
if(inde[index]==false)
{
if(x.id>=0)
{
zf[x.id]=1;
}
else
{
inde[-x.id]=false;
}
if(y.id>=0)
{
zf[y.id]=-1;
}
else
{
inde[-y.id]=true;
}
}
else
{
if(x.id>=0)
{
zf[x.id]=-1;
}
else
{
inde[-x.id]=true;
}
if(y.id>=0)
{
zf[y.id]=1;
}
else
{
inde[-y.id]=false;
}
}
}
int main()
{
#ifdef OUT
freopen("coco.txt","r",stdin);
freopen("lala.txt","w",stdout);
#endif
while(scanf("%d",&N)!=EOF)
{
priority_queue<Point,vector<Point>,cmp > q;
while(!q.empty())q.pop();
init();
Point tmp;
for(int i=0;i<N;i++)
{
scanf("%d",&tmp.x);
tmp.id=i;
q.push(tmp);
}
dfs(1,q);
if(flag)
{
printf("Yes\n");
for(int j=0;j<N;j++)
{
if(j==0)printf("%d",zf[j]);
else printf(" %d",zf[j]);
}
printf("\n");
}
else printf("No\n");
}
return 0;
}