P17774 安达樱不是罪过
题解:P17774 安达樱不是罪过
思路
注意到,若先执行操作\(2\)再执行操作\(1\)数组必定符合要求。所以答案至多为\(2\)。
分类讨论:
- 不用执行操作,也就是一开始\(a_i\)就全部相等。
- 只需要执行\(1\)操作,也就是一开始没有\(0\)或一开始\(0\)都在最后。
- 只需要执行\(2\)操作,也就是\(\text{MEX}([a_1,a_2,\dots,a_n])=\text{MAX}([a_1,a_2,\dots,a_n])+1\)或着说是数组里包含所有\(0,1,\dots,\text{MAX}([a_1,a_2,\dots,a_n])\)。
- 不满条上面的任意条,答案为\(2\)。
代码
赛时马蜂较为奇特,不建议参考。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[100005];
int f[100005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
bool fl=0;
int ma=0;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]!=a[i-1]&&i>1)fl=1;
f[a[i]]++;
ma=max(ma,a[i]);
}
if(!fl)cout<<0;
else{
int cnt=0;
bool ff=0;
for(int i=n;i>=n-f[0]+1;i--)if(a[i])ff=1;
for(int i=0;i<=ma;i++)fl&=f[i];
if(!ff||!f[0])cout<<1;
else cout<<!fl+1;
}
return 0;
}
赛后重打的一份:
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[100005];
int f[100005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
bool fl=0;
int ma=0;
for(int i=1;i<=n;i++){
cin>>a[i];
f[a[i]]++;
ma=max(ma,a[i]);
}
for(int i=1;i<n;i++){
if(a[i]!=a[i+1]){
fl=1;
break;
}
}
if(!fl){
cout<<0;
return 0;
}//第1种
fl=0;
for(int i=n;i>=n-f[0]+1;i--){
if(a[i]){
fl=1;
break;
}
}
if(!fl||!f[0]){
cout<<1;
return 0;
}//第2种
fl=0;
for(int i=0;i<=ma;i++){
if(!f[i]){
fl=1;
break;
}
}
if(!fl)cout<<1;//第3种
else cout<<2;//第4种
return 0;
}

浙公网安备 33010602011771号