B. Two Large Bags
- 最优解一定可以满足同一组内的数互异,于是可以通过贪心地执行加数操作来构造方案
#include <bits/stdc++.h>
using namespace std;
int a[1005];
int v[10005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
v[a[i]]++;
while(v[a[i]]>2)
{
v[a[i]]--;
a[i]++;
v[a[i]]++;
}
}
bool f=true;
for(int i=1;i<=n;i++)
{
if(v[a[i]]%2!=0)
{
f=false;
}
v[a[i]]=0;
}
f==true? cout<<"Yes\n":cout<<"No\n";
}
return 0;
}
C. Devyatkino
- 你知道应该怎样做的,可是你却在拼命尝试说服自己你并不会做
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
int ans=n%10-7;
if(ans<0)
{
ans+=10;
}
int op=10,cur=n%10;
n/=10;
while(n)
{
int x=7-n%10;
if(x<0)
{
x+=10;
}
if(cur<x)
{
x++;
}
if(n%10==8)
{
ans=min(ans,cur+1);
}
ans=min(ans,x);
cur+=n%10*op;
op*=10;
n/=10;
}
cout<<min(ans,7)<<"\n";
}
return 0;
}
D. Object Identification
#include <bits/stdc++.h>
using namespace std;
int x[200005];
int v[200005];
int query(int i,int j)
{
cout<<"? "<<i<<" "<<j<<endl;
int ans;
cin>>ans;
return ans;
}
void output(string x)
{
cout<<"! "<<x<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
v[i]=0;
}
int sum=0;
for(int i=1;i<=n;i++)
{
cin>>x[i];
sum+=(v[x[i]]==0);
v[x[i]]=i;
}
if(sum==n)
{
int p=query(v[1],v[n]);
if(p<n-1)
{
output("A");
}
else if(p>n-1)
{
output("B");
}
else if(p==n-1)
{
p=query(v[n],v[1]);
if(p==n-1)
{
output("B");
}
else
{
output("A");
}
}
}
else
{
for(int i=1;i<=n;i++)
{
if(v[i]==0)
{
int u=i,v=0;
for(int j=1;j<=n;j++)
{
if(x[j]!=x[u])
{
v=j;
break;
}
}
int p=query(u,v);
if(p==0)
{
output("A");
}
else
{
output("B");
}
break;
}
}
}
}
return 0;
}
E. White Magic
- 【注意到】只要不选0,所有非0的数都可以纳入答案;如果要选0,至多让答案增加1;且左边的0一定不劣于右边的0
#include <bits/stdc++.h>
using namespace std;
int a[200005];
bool b[200005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
b[0]=false;
for(int i=1;i<=n;i++)
{
b[i]=false;
cin>>a[i];
}
int sum=0,p=-1,q=0;
for(int i=1;i<=n;i++)
{
sum+=(a[i]!=0);
if(a[i]==0&&p==-1)
{
p=i;
}
}
if(p==-1)
{
cout<<sum<<"\n";
}
else
{
bool f=true;
for(int i=n;i>=1;i--)
{
if(a[i]!=0&&a[i]<=n)
{
if(a[i]<q)
{
f=false;
}
b[a[i]]=true;
while(b[q]==true)
{
q++;
}
}
else if(i==p)
{
b[0]=true;
while(b[q]==true)
{
q++;
}
}
}
sum+=(f==true);
cout<<sum<<"\n";
}
}
return 0;
}