Codeforces Round #717 (Div. 2) (异或)

A. Tit for Tat

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Given an array a of length n, you can do at most k operations of the following type on it:

choose 2 different elements in the array, add 1 to the first, and subtract 1 from the second. However, all the elements of a have to remain non-negative after this operation.
What is lexicographically the smallest array you can obtain?

An array x is lexicographically smaller than an array y if there exists an index i such that xi<yi, and xj=yj for all 1≤j<i. Less formally, at the first index i in which they differ, xi<yi.

Input
The first line contains an integer t (1≤t≤20) – the number of test cases you need to solve.

The first line of each test case contains 2 integers n and k (2≤n≤100, 1≤k≤10000) — the number of elements in the array and the maximum number of operations you can make.

The second line contains n space-separated integers a1, a2, …, an (0≤ai≤100) — the elements of the array a.

Output
For each test case, print the lexicographically smallest array you can obtain after at most k operations.

Example
inputCopy
2
3 1
3 1 4
2 10
1 0
outputCopy
2 1 5
0 1
Note
In the second test case, we start by subtracting 1 from the first element and adding 1 to the second. Then, we can’t get any lexicographically smaller arrays, because we can’t make any of the elements negative.

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const int maxn=110;
int a[maxn];
int main(){
	int t; cin>>t;
	while(t--){
		int n,k; cin>>n>>k;
		for(int i=1; i<=n; i++) cin>>a[i];
		for(int i=1; i<=n-1; i++){ //最后一个直接不能减 
			if(a[i]<k){
				a[n]+=a[i]; k-=a[i]; a[i]=0; 
			} else { //顺序 
				a[i]-=k; a[n]+=k; k=0; break;
			}
		}
		for(int i=1; i<=n; i++) cout<<a[i]<<" ";
		cout<<endl;
	}
	return 0;
}


B. AGAGA XOOORRR

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Baby Ehab is known for his love for a certain operation. He has an array a of length n, and he decided to keep doing the following operation on it:

he picks 2 adjacent elements; he then removes them and places a single integer in their place: their bitwise XOR. Note that the length of the array decreases by one.
Now he asks you if he can make all elements of the array equal. Since babies like to make your life harder, he requires that you leave at least 2 elements remaining.

Input
The first line contains an integer t (1≤t≤15) — the number of test cases you need to solve.

The first line of each test case contains an integers n (2≤n≤2000) — the number of elements in the array a.

The second line contains n space-separated integers a1, a2, …, an (0≤ai<230) — the elements of the array a.

Output
If Baby Ehab can make all elements equal while leaving at least 2 elements standing, print “YES”. Otherwise, print “NO”.

Example
inputCopy
2
3
0 2 2
4
2 3 1 10
outputCopy
YES
NO
Note
In the first sample, he can remove the first 2 elements, 0 and 2, and replace them by 0⊕2=2. The array will be [2,2], so all the elements are equal.

In the second sample, there’s no way to make all the elements equal.

在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const int maxn=2010;
int a[maxn];
int main(){
	int t; cin>>t;
	while(t--){
		int n, allsum=0;; cin>>n;
		for(int i=1; i<=n; i++){
			cin>>a[i]; allsum^=a[i];
		}
		if(allsum == 0){
			cout<<"YES"<<endl; continue;
		}
		int nowsum = 0, cnt = 0;
		for(int i=1; i<=n; i++){
			nowsum ^= a[i];
			if(nowsum == allsum){
				nowsum = 0; cnt++; //分割
			}
		}
		if(cnt > 1) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


C. Baby Ehab Partitions Again

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Baby Ehab was toying around with arrays. He has an array a of length n. He defines an array to be good if there’s no way to partition it into 2 subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in a so that it becomes a good array. Can you help him?

A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into 2 subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can’t share any elements.

Input
The first line contains an integer n (2≤n≤100) — the length of the array a.

The second line contains n integers a1, a2, …, an (1≤ai≤2000) — the elements of the array a.

Output
The first line should contain the minimum number of elements you need to remove.

The second line should contain the indices of the elements you’re removing, separated by spaces.

We can show that an answer always exists. If there are multiple solutions, you can print any.

Examples
inputCopy
4
6 3 9 12
outputCopy
1
2
inputCopy
2
1 2
outputCopy
0
Note
In the first example, you can partition the array into [6,9] and [3,12], so you must remove at least 1 element. Removing 3 is sufficient.

In the second example, the array is already good, so you don’t need to remove any elements.

涉及背包问题,后序补,先上思想
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值