P6956 题解
题目分析
输入
显然,可以用一个 visit[i]--,实在 No,否则
代码
#include<bits/stdc++.h>
using namespace std;
int n,visit[100001]={0},a[100010],timesOf0=0,h=0,outOf0[100001]={0},no=0,yes=1;
int main(){
cin>>n;
while(n--){
int temp;
cin>>temp;
if(no==1){
continue;
}
if(temp<0){
if(visit[-temp]>=1){
visit[-temp]--;
continue;
}
if(timesOf0>0){
timesOf0--;
outOf0[h++]=-temp;
continue;
}
cout<<"No";
no=1;
yes=0;
}
if(temp==0){
timesOf0++;
}
if(temp>0){
visit[temp]++;
}
}
if(yes==0){
return 0;
}
cout<<"Yes"<<endl;
for(int i=0;i<h;i++){
cout<<outOf0[i]<<" ";
}
return 0;
}
这样就能 AC 吗?不行。这样会出现wrong output format Unexpected end of file - int32 expected的错误。因为题目其实要求是:输出用过的
AC 代码
#include<bits/stdc++.h>
using namespace std;
int n,visit[100001]={0},a[100010],timesOf0=0,h=0,outOf0[100001]={0},no=0,yes=1;
int main(){
cin>>n;
while(n--){
int temp;
cin>>temp;
if(no==1){
continue;
}
if(temp<0){
if(visit[-temp]>=1){
visit[-temp]--;
continue;
}
if(timesOf0>0){
timesOf0--;
outOf0[h++]=-temp;
continue;
}
cout<<"No";
no=1;
yes=0;
}
if(temp==0){
timesOf0++;
}
if(temp>0){
visit[temp]++;
}
}
if(yes==0){
return 0;
}
cout<<"Yes"<<endl;
for(int i=0;i<h;i++){
cout<<outOf0[i]<<" ";
}
for(int i=0;i<timesOf0;i++){
cout<<1<<' ';
}
return 0;
}