Codeforces Round #713 (Div. 3)
题号 | 题目 | 考点 |
---|---|---|
A | Spy Detected! | 签到题 |
B | Almost Rectangle | 模拟题 |
C | A-B Palindrome | 构造 |
D | Corrupted Array | 构造 |
E | Permutation by Sum | 构造 |
F | Education | 模拟+暴力 |
G | Short Task | 筛法求因数和 |
A
题意:
输出数组中唯一一个不一样的数的下标
题解:
签到题
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
}
const int maxn=300;
int a[maxn];
int x,y;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
x=a[1];
if(a[2]!=x&&a[3]!=x)
{
cout<<1<<endl;
}
else if(a[2]!=x&&a[3]==x)
{
cout<<2<<endl;
}
else if(a[2]==x&&a[3]!=x)
{
cout<<3<<endl;
}
else
for(int i=4;i<=n;i++)
{
if(a[i]!=x)
{
cout<<i<<endl;
break;
}
}
}
return 0;
}
B
题意:
给你一个带两个 * 的题,请你加上两个 *,让他们四个构成矩形
题解:
照着模拟就行了,记得别超出边界
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
}
const int maxn=500;
char a[maxn][maxn];
int x1,y1,x2,y2;
int x3,y3,x4,y4;
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(a,0,sizeof(a));
char ch=getchar();
x1=0;y1=0;x2=0;y2=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
if(a[i][j]=='*')
{
if(x1==0&&y1==0)
{
x1=i;
y1=j;
}
else if(x2==0&&y2==0)
{
x2=i;
y2=j;
}
}
}
char ch=getchar();
}
int len1=0,len2=0;
len1=1;
len2=1;
if(x1==x2)
{
if(x1+len2<=n)
{
y3=y1;
y4=y2;
x3=x1+len2;
x4=x2+len2;
}
else if(x1-len2>=1)
{
y3=y1;
y4=y2;
x3=x1-len2;
x4=x2-len2;
}
}
else if(y1==y2)
{
if(y1+len1<=n)
{
x3=x1;
x4=x2;
y3=y1+len1;
y4=y1+len1;
}
else if(y1-len1>=1)
{
x3=x1;
x4=x2;
y3=y1-len1;
y4=y1-len1;
}
}
else
{
x3=x2;
y3=y1;
x4=x1;
y4=y2;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==x1&&j==y1)cout<<"*";
else if(i==x2&&j==y2)cout<<"*";
else if(i==x3&&j==y3)cout<<"*";
else if(i==x4&&j==y4)cout<<"*";
else cout<<".";
}
cout<<endl;
}
}
return 0;
}
C
题意:
给定0和1的数量,然后给你一个01串,对01串中?部分进行填充(用0,1),要求01数量和题目给出的一致,且使得01串变成回文串
题解:
其实很好想,就是对问好进行填充如何01不够就说明构造失败,但是要注意细节,首先将可以确定的?进行填充,(什么叫确定的,因为是回文串所以只要有一个确定,对应位置也应该确定)
然后从左往右扫,有1就填1,有0填0
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
}
int main()
{
//freopen("out.txt","w",stdout);
int t;
cin>>t;
while(t--){
int a,b;
cin>>a>>b;
string s;
cin>>s;
int j=s.length()-1;
bool f=0;
for(int i=0;i<s.length();i++)
{
if(s[i]=='?')s[i]=s[s.length()-i-1];
}
for(int i=0;i<s.length();i++)
{
if(s[i]=='0')a--;
if(s[i]=='1')b--;
}
if(a<0||b<0)
{
cout<<"-1"<<endl;
continue;
}
int aa=a,bb=b;
if(s.length()==1)
{
if(a)cout<<0<<endl;
else if(b)cout<<1<<endl;
else cout<<s<<endl;
continue;
}
for(int i=0;i<s.length();i++)
{
if(i>j)break;
if(i==j)//如果正好重叠到最中间
{
if(s[i]=='?')//如果最中间为空
{
if(a)
{
s[i]='0';
a--;
}
else if(b)
{
s[i]='1';
b--;
}
else
{
f=1;
break;
}
}
break;
}
if(s[i]=='?')//i不确定
{
if(s[j]=='?')//i,j不确定
{
if(a>=2)
{
s[i]='0';
s[j]='0';
a-=2;
}
else if(b>=2)
{
s[i]='1';
s[j]='1';
b-=2;
}
else
{
f=1;
break;
}
}
else if(s[j]!='?')//i不确定,j确定了
{
if(s[j]=='0'&&a)
{
s[i]='0';
a--;
}
else if(s[j]=='1'&&b)
{
s[i]='1';
b--;
}
else
{
f=1;
break;
}
}
}
else if(s[j]=='?')//i确定,j不确定
{
if(s[i]=='0'&&a)
{
s[j]='0';
a--;
}
else if(s[i]=='1'&&b)
{
s[j]='1';
b--;
}
else
{
f=1;
break;
}
}
else if(s[j]!='?')//i和j都确定
{
if(s[i]!=s[j])
{
f=1;
break;
}
}
j--;
}
if(f==1||a!=0||b!=0)cout<<"-1"<<endl;
else cout<<s<<endl;
}
}
D
题意:
b数组是由a数组构造的
构造方式:
b[i] = a[i] 1<i<n
b[n+1] = a1+…+an
b[n+2] = x(x为任意数)
然后把b数组打乱给你,让你猜a数组
题解:
题目麻烦在存在一个x
a数组一共有n个数,b数组比a多个x 和 前n项的和,关键在于找这两个东西,
对b排序,前n项的和肯定就在b[n+1]和b[n+2]这两个中
如果前n个数的和等于b[n+1]说明b[n+2]是x,b[n+1]是求和
如果前n+1个数的和小于b[n+2],说明答案不存在
如果前n+1个数的和大于b[n+2],我们就看多出来的部分是否在b中存在,如果前n+1个数的和比b[n+2]多m,m在b中存在,说m就是x,b[n+2]就是和,否则大家就不存在
代码:
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
return s*w;
}
const int maxn=2e5+9;
ll a[maxn];
map<ll,int>mp;
int main()
{
int t;
cin>>t;
while(t--)
{
ll sum=0;
int n;
cin>>n;
mp.clear();
for(int i=1;i<=n+2;i++)a[i]=0;
for(int i=1;i<=n+2;i++)
{
cin>>a[i];
mp[a[i]]++;
sum+=a[i];
}
sort(a+1,a+3+n);
sum-=a[n+2];
mp[a[n+2]]--;
ll ans=sum-a[n+1];
if(ans==a[n+1])
{
mp[a[n+1]]--;
}
else if(sum<=a[n+2])
{
cout<<"-1"<<endl;
continue;
}
else if(sum>a[n+2])
{
ll w=sum-a[n+2];
if(mp[w])mp[w]--;
else if(mp[w]==0)
{
cout<<-1<<endl;
continue;
}
}
for(int i=1;i<=n+2;i++){
if(mp[a[i]])
{
cout<<a[i]<<" ";
mp[a[i]]--;
}
}
cout<<endl;
}
return 0;
}