Codeforces Round #713 (Div. 3)

原题地址 https://codeforces.com/contest/1512/problem/B

A. Spy Detected!

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn (n≥3n≥3) positive integers. It is known that in this array, all the numbers except one are the same (for example, in the array [4,11,4,4][4,11,4,4] all numbers except one are equal to 44).

Print the index of the element that does not equal others. The numbers in the array are numbered from one.

Input

The first line contains a single integer tt (1≤t≤1001≤t≤100). Then tt test cases follow.

The first line of each test case contains a single integer nn (3≤n≤1003≤n≤100) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100).

It is guaranteed that all the numbers except one in the aa array are the same.

Output

For each test case, output a single integer — the index of the element that is not equal to others.

 

找一个不同的数。。送分题切掉

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=1005;
int n,t,num[maxn],ton[maxn],idx[maxn];
int main()
{
	ios
	cin>>t;
	while(t--)
	{
		memset(ton,0,sizeof(ton));
		memset(idx,0,sizeof(idx));
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>num[i];
			ton[num[i]]++;
			idx[num[i]]=i;
		}
		for(int i=1;i<=105;i++)
		{
			if(ton[i]==1)cout<<idx[i]<<endl;
		}
	}
	return 0;
}

 

B. Almost Rectangle

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There is a square field of size n×nn×n in which two cells are marked. These cells can be in the same row or column.

You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes.

For example, if n=4n=4 and a rectangular field looks like this (there are asterisks in the marked cells):

 

..∗.....∗.........∗.....∗.......

Then you can mark two more cells as follows

 

∗.∗.....∗.∗.....∗.∗.....∗.∗.....

If there are several possible solutions, then print any of them.

Input

The first line contains a single integer tt (1≤t≤4001≤t≤400). Then tt test cases follow.

The first row of each test case contains a single integer nn (2≤n≤4002≤n≤400) — the number of rows and columns in the table.

The following nn lines each contain nn characters '.' or '*' denoting empty and marked cells, respectively.

It is guaranteed that the sums of nn for all test cases do not exceed 400400.

It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column.

It is guaranteed that the solution exists.

Output

For each test case, output nn rows of nn characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them.

 

大意是找给你两个点,让你找到一个矩形。分类讨论下就可以了。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int t,n; char str[405][405];
int main()
{
	ios
	cin>>t;
	while(t--)
	{
		cin>>n;
		int flag=0,tmpx1,tmpx2,tmpy1,tmpy2;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				cin>>str[i][j];
				if(str[i][j]=='*'&&!flag)tmpx1=i,tmpy1=j,flag=1;
				else if(str[i][j]=='*'&&flag==1)tmpx2=i,tmpy2=j,flag=1;
			}
		}
		if(tmpx1!=tmpx2&&tmpy1!=tmpy2)
		{
			str[tmpx1][tmpy2]='*';
			str[tmpx2][tmpy1]='*';
		}
		else
		{
			if(tmpx1==tmpx2)
			{
				if(tmpx1>=2)
				{
					str[tmpx1-1][tmpy1]='*';
					str[tmpx2-1][tmpy2]='*';
				}
				else
				{
					str[tmpx1+1][tmpy1]='*';
					str[tmpx2+1][tmpy2]='*';
				}
			}
			else
			{
				if(tmpy1>=2)
				{
					str[tmpx1][tmpy1-1]='*';
					str[tmpx2][tmpy2-1]='*';
				}
				else
				{
					str[tmpx1][tmpy1+1]='*';
					str[tmpx2][tmpy2+1]='*';
				}
			}
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				cout<<str[i][j];
			}	
			cout<<endl;
		}
	}
	return 0;
}

C. A-B Palindrome

You are given a string ss consisting of the characters '0', '1', and '?'. You need to replace all the characters with '?' in the string ss by '0' or '1' so that the string becomes a palindrome and has exactly aa characters '0' and exactly bb characters '1'. Note that each of the characters '?' is replaced independently from the others.

A string tt of length nn is called a palindrome if the equality t[i]=t[n−i+1]t[i]=t[n−i+1] is true for all ii (1≤i≤n1≤i≤n).

For example, if s=s="01?????0", a=4a=4 and b=4b=4, then you can replace the characters '?' in the following ways:

  • "01011010";
  • "01100110".

For the given string ss and the numbers aa and bb, replace all the characters with '?' in the string ss by '0' or '1' so that the string becomes a palindrome and has exactly aa characters '0' and exactly bb characters '1'.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

The first line of each test case contains two integers aa and bb (0≤a,b≤2⋅1050≤a,b≤2⋅105, a+b≥1a+b≥1).

The second line of each test case contains the string ss of length a+ba+b, consisting of the characters '0', '1', and '?'.

It is guaranteed that the sum of the string lengths of ss over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output:

  • "-1", if you can't replace all the characters '?' in the string ss by '0' or '1' so that the string becomes a palindrome and that it contains exactly aa characters '0' and exactly bb characters '1';
  • the string that is obtained as a result of the replacement, otherwise.

If there are several suitable ways to replace characters, you can output any.

 

给你一串字符串和0,1的数量,让你把这个字符串中的‘?’还原成0和1。字符串要求为回文。无法还原输出-1.

暴力写啊写啊,就不知道崩哪里了。。

一开始思路是边还原边统计,在判断中间是否有?,再判断剩下的没有还原的问号,写着写着就乱了。。改半天不知道错哪里..太乱了

看了一眼题解,原来可以把字符串反转过来再统计一次,把ab放在后面统计会更加清晰,最后把所有不对的情况都列出来。

代码还是要条理清晰一点好。

学到了reverse和count的用法,确实方便。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
int t,n,a,b,q;string str;

int main()
{
	ios
	cin>>t;
	while(t--)
	{
		cin>>a>>b;
		cin>>str;
		n=str.length()-1;
		for(int times=1;times<=2;times++)
		{
			for(int i=0;i<=n/2;i++)
			{
				if(str[i]=='0'&&str[n-i]=='?')str[n-i]='0';
				if(str[i]=='1'&&str[n-i]=='?')str[n-i]='1';
				if(str[i]=='0'&&str[n-i]=='1')
				{
					cout<<"-1"<<endl;
					goto end;
				}
			}
			reverse(str.begin(),str.end());
		}
		//cout<<str<<endl;
		a-=count(str.begin(),str.end(),'0');
		b-=count(str.begin(),str.end(),'1');
		q=count(str.begin(),str.end(),'?');
		int mid;
		if(n%2==0&&str[n/2]=='?')mid=1;else mid=0;
		if(a<0||b<0||a+b!=q||(!mid&&(a%2||b%2)))
		{
			cout<<-1<<endl;
			goto end;
		}
		if(mid==1)
		{
			if(a%2==1)a--,str[n/2]='0';
			if(b%2==1)b--,str[n/2]='1';
		}
		for(int i=0;i<=n/2;i++)
		{
			if(str[i]=='?')
			{
				if(a>0)
				{
					a-=2;str[i]='0';str[n-i]='0';
				}
				else if(b>0)
				{
					b-=2;str[i]='1';str[n-i]='1';
				}
			}
		}
		cout<<str<<endl;
		end:;
	}
	return 0;
}

 

D. Corrupted Array

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a number nn and an array b1,b2,…,bn+2b1,b2,…,bn+2, obtained according to the following algorithm:

  • some array a1,a2,…,ana1,a2,…,an was guessed;
  • array aa was written to array bb, i.e. bi=aibi=ai (1≤i≤n1≤i≤n);
  • The (n+1)(n+1)-th element of the array bb is the sum of the numbers in the array aa, i.e. bn+1=a1+a2+…+anbn+1=a1+a2+…+an;
  • The (n+2)-th element of the array b was written some number x(1≤x≤10e91≤x≤10e9), i.e. bn+2=xbn+2=x; The
  • array bb was shuffled.

For example, the array b=[2,3,7,12,2]b=[2,3,7,12,2] it could be obtained in the following ways:

  • a=[2,2,3]a=[2,2,3] and x=12x=12;
  • a=[3,2,7]a=[3,2,7] and x=2x=2.

For the given array bb, find any array aa that could have been guessed initially.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105).

The second row of each test case contains n+2n+2 integers b1,b2,…,bn+2b1,b2,…,bn+2 (1≤bi≤1091≤bi≤109).

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, output:

  • "-1", if the array bb could not be obtained from any array aa;
  • nn integers a1,a2,…,ana1,a2,…,an, otherwise.

If there are several arrays of aa, you can output any.

我是读题废物。

大意是给你一个数列,有n个数,n+1个数是n个数的和,第n+2个数是一个随机数。把他们顺序打乱(shuffled),要你找到原来的序列。

  • The (n+2)-th element of the array b was written some number x(1≤x≤10e9), i.e. bn+2=x; The array b was shuffled.

这句话看了十几分钟不知道这是什么意思,又找了好久网上的题解才知道

x是一个1到1e9的随机数。

然后二分忘记写判定失败的情况wa了两发

忘记开longlong wa了两发

还得多打,多试错。

 

思路是先排序,求前缀和,如果随机数是最大的,那n-1一定等于前n-2个数的和.

如果不是最大的,那二分找到前缀和 和 最大的数的差 如果有这个数,把他变成0,输出数组。

如果没有这个数(二分找到的数不等于 差 或者找不到这个数) 输出-1.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn=200005;
ll t,n,num[maxn],pre[maxn];
bool check(ll a,ll tmp)
{
	if(num[a]<=tmp)return 1;
	return 0;
}
int main()
{
	ios
	cin>>t;
	while(t--)
	{
		cin>>n;n+=2;
		rep(i,1,n)cin>>num[i];
		sort(num+1,num+n+1);
		for(int i=1;i<=n;i++)
		{
			pre[i]=pre[i-1]+num[i];
		}
		if(pre[n-2]==num[n-1])
		{
			for(int i=1;i<=n-2;i++)
			{
				cout<<num[i]<<' ';
			}
			cout<<endl;
		}
		else
		{
			ll tmp=pre[n-1]-num[n];
			ll l=1,r=n-1,ans=-2,mid;
			//cout<<pre[n-1]<<' '<<num[n]<<' '<<tmp;
			while(l<=r)
			{
				mid=(l+r)>>1;
				if(check(mid,tmp))
				{
					ans=mid;
					l=mid+1;
				}
				else
				{
					r=mid-1;
				}
			}
			if(ans==-2)
			{
				cout<<-1<<endl;
				continue;
			}
			if(num[ans]==tmp)num[ans]=0;
			else 
			{
				cout<<-1<<endl;
				continue;
			}
			//cout<<' '<<num[ans]<<' '<<ans<<endl;
			rep(i,1,n-1)if(num[i])cout<<num[i]<<' ';
			cout<<endl;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值