Codeforces Round #732 (Div. 2)

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.

Example
input

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

output

2
2 1
3 1
-1
0
6
1 4
1 4
1 5
1 5
2 5
2 5

Note
In the first example, we do the following operations:

i=2, j=1: [1,2,3,4]→[2,1,3,4];
i=3, j=1: [2,1,3,4]→[3,1,2,4];
In the second example, it’s impossible to make two arrays equal.

题意:把a数组的某个数加1,就要把a数组的某个数减1,让a数组和b数组相等;
所以我们可以用vector存储需要改变的位置,再用两个数组存储需要改变多少。
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[1001],b[1001];
signed main()
{
	int t;
	cin>>t;
	while (t--)
	{
		int c[10001],d[10001];//需要改变位置的大小
		int n,sum1=0,sum2=0;sum1存储需要减的,sum2存储需要加的。
		vector<int>cun1,cun2;//存储需要改变的位置
		cin>>n;
		for (int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for (int i=1;i<=n;i++)
		{
			cin>>b[i];
			if (a[i]>b[i]) 
			{
				cun2.push_back(i);
				d[i] = a[i]-b[i];
				sum1+=d[i];
			}
			else if (b[i]>a[i])
			{
				c[i] = b[i]-a[i];
				cun1.push_back(i);
				sum2+=c[i];
			}
		}
		if (sum1!=sum2) cout<<-1<<endl;
		else
		{
			cout<<sum1<<endl;
			for (int i=0,j=0;i<cun1.size(),j<cun2.size();)
			{
				cout<<cun2[j]<<" "<<cun1[i]<<endl;
				int h = cun1[i];
				int k = cun2[j];
				c[h]--;d[k]--;
				if (c[h]==0) i++;
				if (d[k]==0) j++;
			}
		}
	}
}

-------分割线------
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.

Example
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

Note
In the first test case, “aaaaa” and “bbbbb” exchanged all positions, and “ccccc” is the stolen string.

In the second test case, “aaaa” and “bbbb” exchanged two first positions, and “cccc” is the stolen string.

This is the first test in the hack format:

3
3 5
aaaaa
bbbbb
ccccc
1 2 5 1 2 3 4 5
2 1 3
3 4
aaaa
bbbb
cccc
1 2 2 1 2
2 1 3
5 6
abcdef
uuuuuu
kekeke
ekekek
xyzklm
1 5 3 2 3 6
2 4 3 2 4 6
5 4 1 2 3

题意:奇数个字符串,他会选择一串拿走。其他的他可以选择两个字符串在不同位置交换
我记录每一位的字符总和(转换成数字),再减去还在的
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    int t;
	cin>>t;
	while (t--)
	{
		int ch[100025];//记录每一位字符的总和
		memset(ch,0,sizeof(ch));
		int n,m;
		cin>>n>>m;
		for (int i=0;i<n;i++)
		{
			string str;
			cin>>str;
			for (int j=0;j<m;j++)
			{
				int h = str[j]-'a';
				ch[j]+=h;
			}
		}
		for (int i=1;i<n;i++)
		{
			string str;
			cin>>str;
			for (int j=0;j<m;j++)
			{
				int h = str[j]-'a';
				ch[j]-=h;
			}
		}
		string str;
		for (int i=0;i<m;i++)
		{
			char c = (ch[i]+'a');
			str+=c;
		}
		cout<<str<<endl;
	}
}

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).

Example
input

3
4
4 3 2 5
4
3 3 2 2
5
1 2 3 5 4

output

YES
YES
NO

Note
The possible list of operations in the first test case:

Swap a1 and a2. The resulting sequence is 3,4,2,5. The directions are: left, left, right, right.
Swap a2 and a3. The resulting sequence is 3,2,4,5. The directions are: left, left, right, right.
Swap a1 and a2. The resulting sequence is 2,3,4,5. The directions are: right, right, right, right.

题意,每个数字都有方向,一开始是正确的,然后你要交换让这个数组有序。交换一次就改变两者的方向
思路:每个数到他应该到的位置是偶数(证明不会)
方法:偶数位到偶数位是偶数,奇数位到奇数位是偶数。所以存储那个数字在偶数位还是奇数位就行了
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100001];
signed main()
{
	int t;
    cin>>t;
    while (t--)
    {
        int b[100001][2];//存储奇数位还是偶数位
        memset(b,0,sizeof(b));
        int n;
        cin>>n;
        for (int i=1;i<=n;i++)
        {
            cin>>a[i];
            b[a[i]][i%2]++;
        }
        sort(a+1,a+n+1);
        bool flag = true;
        for (int i=1;i<=n;i++)
        {
            if (b[a[i]][i%2]>0)
            {
                b[a[i]][i%2]--;
            }
            else
            {
                flag = false;//如果无法挪动偶数位
                break;
            }
        }
        if (flag == false) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值