Codeforces Round #732 (Div. 2)(A-C)

Codeforces Round #732 (Div. 2)(A-C)

A. AquaMoon and Two Arrays
AquaMoon and Cirno are playing an interesting game with arrays. Cirno has prepared two arrays a and b, both consist of n non-negative integers. AquaMoon can perform the following operation an arbitrary number of times (possibly zero):
She chooses two indices i and j (1≤i,j≤n), then decreases the i-th element of array a by 1, and increases the j-th element of array a by 1. The resulting values at i-th and j-th index of array a are ai−1 and aj+1, respectively. Each element of array a must be non-negative after each operation. If i=j this operation doesn’t change the array a.
AquaMoon wants to make some operations to make arrays a and b equal. Two arrays a and b are considered equal if and only if ai=bi for all 1≤i≤n.
Help AquaMoon to find a sequence of operations that will solve her problem or find, that it is impossible to make arrays a and b equal.
Please note, that you don’t have to minimize the number of operations.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤100).
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤100). The sum of all ai does not exceed 100.
The third line of each test case contains n integers b1,b2,…,bn (0≤bi≤100). The sum of all bi does not exceed 100.
Output
For each test case print “-1” on the only line if it is impossible to make two arrays equal with some sequence of operations.
Otherwise, print an integer m (0≤m≤100) in the first line — the number of operations. Then print m lines, each line consists of two integers i and j — the indices you choose for the operation.
It can be proven that if it is possible to make two arrays equal with some sequence of operations, there exists a sequence with m≤100.
If there are multiple possible solutions, you can print any.

输入样例
4
4
1 2 3 4
3 1 2 4
2
1 3
2 1
1
0
0
5
4 3 2 1 0
0 1 2 3 4

输出样例
2
2 1
3 1
-1
0
6
1 4
1 4
1 5
1 5
2 5
2 5

题目大意
一个数组中选定两个指针i和j,i指针的值-1而j指针的值+1,最后使这个数组变成想要的数组。若找不到就输出-1,若能找到输出操作数和具体操作。不必要求最小化。

解题思路:
首先求两个数组的数组和如果相同则可以转化如果不同则输出-1代表不能转化,然后一一对应初始数组和最终数组的每一个元素,大于的存到一个数组中,小于的存到一个数组中,具体元素直接化为i和j。最终两个数组的元素个数肯定是一样的,元素个数就是操作数,按照顺序输出即可。

ac代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N=110;
int a[N],b[N],c[N],d[N];
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	 int n;
	 cin>>n;
	 int sum1=0,sum2=0;
	 for(int i=1;i<=n;i++)
	 {
	 	cin>>a[i];
	 	sum1+=a[i];
	 }
	 for(int i=1;i<=n;i++)
	 {
	 	cin>>b[i];
	 	sum2+=b[i];
	 }
	 if(sum1!=sum2)cout<<"-1"<<endl;
	 else
	 {
	 	int ans1=0,ans2=0;
	 	for(int i=1;i<=n;i++)
		 {
               if(a[i]<b[i])
			   {
                   for(int j=0;j<b[i]-a[i];j++)
				   {
                       c[ans1++]=i;
                   }
               }else if(a[i]>b[i])
			   {
                   for(int j=0;j<a[i]-b[i];j++)
				   {
                       d[ans2++]=i;   
                   }
               }
           }
        cout<<ans1<<endl;
        for(int i=0;i<ans1;i++)
		{
               cout<<d[i]<<" "<<c[i]<<endl;
        }
	 }
	 }
	return 0;
 } 

B AquaMoon and Stolen String
AquaMoon had n strings of length m each. n is an odd number.
When AquaMoon was gone, Cirno tried to pair these n strings together. After making n−12 pairs, she found out that there was exactly one string without the pair!
In her rage, she disrupted each pair of strings. For each pair, she selected some positions (at least 1 and at most m) and swapped the letters in the two strings of this pair at the selected positions.
For example, if m=6 and two strings “abcdef” and “xyzklm” are in one pair and Cirno selected positions 2, 3 and 6 she will swap ‘b’ with ‘y’, ‘c’ with ‘z’ and ‘f’ with ‘m’. The resulting strings will be “ayzdem” and “xbcklf”.
Cirno then stole away the string without pair and shuffled all remaining strings in arbitrary order.
AquaMoon found the remaining n−1 strings in complete disarray. Also, she remembers the initial n strings. She wants to know which string was stolen, but she is not good at programming. Can you help her?

Input
This problem is made as interactive. It means, that your solution will read the input, given by the interactor. But the interactor will give you the full input at the beginning and after that, you should print the answer. So you should solve the problem, like as you solve the usual, non-interactive problem because you won’t have any interaction process. The only thing you should not forget is to flush the output buffer, after printing the answer. Otherwise, you can get an “Idleness limit exceeded” verdict. Refer to the interactive problems guide for the detailed information about flushing the output buffer.
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains two integers n, m (1≤n≤105, 1≤m≤105) — the number of strings and the length of each string, respectively.
The next n lines each contain a string with length m, describing the original n strings. All string consists of lowercase Latin letters.
The next n−1 lines each contain a string with length m, describing the strings after Cirno exchanged and reordered them.
It is guaranteed that n is odd and that the sum of n⋅m over all test cases does not exceed 105.
Hack format:
The first line should contain a single integer t. After that t test cases should follow in the following format:
The first line should contain two integers n and m.
The following n lines should contain n strings of length m, describing the original strings.
The following n−12 lines should describe the pairs. They should contain, in the following order: the index of the first string i (1≤i≤n), the index of the second string j (1≤j≤n, i≠j), the number of exchanged positions k (1≤k≤m), and the list of k positions that are exchanged (k distinct indices from 1 to m in any order)
The final line should contain a permutation of integers from 1 to n, describing the way the strings should be reordered. The strings will be placed in the order indices placed in this permutation, the stolen string index will be ignored.
Output
For each test case print a single line with the stolen string.

Input
3
3 5
aaaaa
bbbbb
ccccc
aaaaa
bbbbb
3 4
aaaa
bbbb
cccc
aabb
bbaa
5 6
abcdef
uuuuuu
kekeke
ekekek
xyzklm
xbcklf
eueueu
ayzdem
ukukuk

Output
ccccc
cccc
kekeke

题目大意
给定n个长度一致(已知)的字符串和n-1个长度一致(已知)的字符串,下面的字符串是由上面的字符串转化来的,转化是对应的下标的字符可以相互交换得到新的字符串,找到下面n-1个字符串都没用过的字符串。

思路 :
按照列来找,字符串每一列必定上字符串比下字符串多一个,每一列加起来组成的字符串即为所求;

ac代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath> 
using namespace std;
const int N=100010;
int a[N][26];
int main() {
    int t;
    cin >> t;
    while (t--) 
	{
        int n, m;
        char c;
        cin >> n >> m;
        for(int i=0;i<m;i++)
        {
        	for(int j=0;j<26;j++)
        	{
        		a[i][j]=0;
			}
		}
        for (int i = 0; i < n; i++) 
		{  
            for (int j = 0; j < m; j++) 
			{
                cin >> c;
                a[j][c-'a']++;
            }
        }
        for (int i = 0; i < n - 1; i++) 
		{
            for (int j = 0; j < m; j++) 
			{
                cin >> c;
                a[j][c - 'a']--;
            }
        }
        for (int i = 0; i < m; i++) 
		{
            for (int j = 0; j < 26; j++) 
			{
                if (a[i][j] != 0) 
				{
                    c='a'+j;
                    cout << c;
                    break;
                }
            }
        }
        cout << endl;
        fflush(stdout);
    }
    return 0;
}

C. AquaMoon and Strange Sort
AquaMoon has n friends. They stand in a row from left to right, and the i-th friend from the left wears a T-shirt with a number ai written on it. Each friend has a direction (left or right). In the beginning, the direction of each friend is right.
AquaMoon can make some operations on friends. On each operation, AquaMoon can choose two adjacent friends and swap their positions. After each operation, the direction of both chosen friends will also be flipped: left to right and vice versa.
AquaMoon hopes that after some operations, the numbers written on the T-shirt of n friends in the row, read from left to right, become non-decreasing. Also she wants, that all friends will have a direction of right at the end. Please find if it is possible.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤50) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤105) — the number of Aquamoon’s friends.
The second line contains n integers a1,a2,…,an (1≤ai≤105) — the numbers, written on the T-shirts.
It is guaranteed that the sum of n for all test cases does not exceed 105.
Output
For each test case, if there exists a possible sequence of operations, print “YES” (without quotes); otherwise, print “NO” (without quotes).
You can print each letter in any case (upper or lower).

输入样例
3
4
4 3 2 5
4
3 3 2 2
5
1 2 3 5 4
输出样例
YES
YES
NO

题目大意
给定n个数,可以进行任意次相邻数交换,目标序列是不递增序列,问对于每个数能否用偶数次操作使得它们回到目标位置上。
思路:
如果序列没有重复数,那么原位置和目标位置是明确的,看一下距离是否都为偶数即可。有重复数时易知原序列的数在奇数位置上,最后必回到奇数位置上,偶数同理,我们统计一下重复数原来在奇数位置上的个数,和在目标序列中奇数位置上的个数是否相等即可。

ac代码:

 #include<iostream>
 #include<algorithm>
 #include<cstring>
 using namespace std;
 const int N=100010;
 int a[N],b[N][2],c[N][2];
 int main()
 {
 	int t;
 	cin>>t;
 	while(t--)
	 {
	 	int n;
	 	cin>>n;
	 	memset(b,0,sizeof(b));
	 	memset(c,0,sizeof(c));
	 	for(int i=1;i<=n;i++)
	 	{
	 		cin>>a[i];
	 		b[a[i]][i%2]++;
		 }
		 sort(a+1,a+1+n);
		 for(int i=1;i<=n;i++)
		 {
		 	c[a[i]][i%2]++;
		 }
		 bool flag=true;
		 for(int i=1;i<=n;i++)
		 {
		 	if(b[a[i]][0]!=c[a[i]][0])
		 	{
		 		flag=false;
		 		break;
			 }
		 }
		 if(flag)cout<<"yes"<<endl;
		 else cout<<"no"<<endl;
	 	
	  } 
	  return 0;
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值