Codeforces Global Round 10

A - Omkar and Password

Lord Omkar has permitted you to enter the Holy Church of Omkar! To test your worthiness, Omkar gives you a password which you must interpret!
A password is an array a of n positive integers. You apply the following operation to the array: pick any two adjacent numbers that are not equal to each other and replace them with their sum. Formally, choose an index i such that 1≤i For example, for array [7,4,3,7] you can choose i=2 and the array will become [7,4+3,7]=[7,7,7]. Note that in this array you can’t apply this operation anymore.
Notice that one operation will decrease the size of the password by 1. What is the shortest possible length of the password after some number (possibly 0) of operations?

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤2⋅1e5) — the length of the password.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤1e9) — the initial contents of your password.
The sum of n over all test cases will not exceed 2⋅1e5.

Output

For each password, print one integer: the shortest possible length of the password after some number of operations.

input

2
4
2 1 3 1
2
420 420

output

1
2

Note

In the first test case, you can do the following to achieve a length of 1:
Pick i=2 to get [2,4,1]
Pick i=1 to get [6,1]
Pick i=1 to get [7]
In the second test case, you can’t perform any operations because there is no valid i that satisfies the requirements mentioned above.

分析

分两种情况
1.数组元素不完全相同
从数组中任意选两个数字每次都选不相同的相加总能加到最后只剩下一个数字
2.数组元素完全相同
无法进行相加,故直接输出数组长度即可

Code

#include
#include
#include
#include
using namespace std;
const int maxn = 1e5+5;
int a[maxn*2];
int main()
{
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof(a));
		int n;
		cin>>n;
		int Cnt = 1;
		for(int i = 1 ; i <= n ; i++)
		{
			cin>>a[i];
			if(a[i] == a[i-1])
			{
				Cnt++;
			}
		}
		if(Cnt == n)
		{
			cout<<Cnt<<endl;
		}else
		{
			cout<<1<<endl;
		}
	}
	return 0;
}

B - Omkar and Infinity Clock

Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem:
You are given an array a of n integers. You are also given an integer k. Lord Omkar wants you to do k operations with this array.
Define one operation as the following:
Set d to be the maximum value of your array.
For every i from 1 to n, replace ai with d−ai.
The goal is to predict the contents in the array after k operations. Please help Ray determine what the final sequence will look like!

Input

Each test contains multiple test cases. The first line contains the number of cases t (1≤t≤100). Description of the test cases follows.
The first line of each test case contains two integers n and k (1≤n≤2⋅105,1≤k≤1018) – the length of your array and the number of operations to perform.
The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) – the initial contents of your array.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each case, print the final version of array a after k operations described above.

input

3
2 1
-199 192
5 19
5 -1 4 2 0
1 2
69

output

391 0
0 6 1 3 5
0

Note

In the first test case the array changes as follows:
Initially, the array is [−199,192]. d=192.
After the operation, the array becomes [d−(−199),d−192]=[391,0].

分析

找规律的题,对着样例写了一会之后发现k = 1 时只进行一次运算
k为奇数的时候进行了三次运算   k为偶数的时候进行了两次运算

Code

#include
#include
#include
#include
using namespace std;
//const int INF  = 0x3f3f3f;
const int maxn = 1e5+5;
typedef long long ll;
ll a[maxn*2];
ll b[maxn*2];
ll c[maxn*2];
int main()
{
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int n;
		ll k;
		cin>>n>>k;
		ll d = -INT_MAX;
		for(int i = 1 ; i <= n ; i++)
		{
			cin>>a[i];
			//d = max(a[i],d);
			if(a[i] > d)
			{
				d = a[i];
			}
		}

		for(int i = 1 ; i <= n ; i++)
		{
			a[i] = (1ll)*(d - a[i]);//操作一次
		}
		d = -INT_MAX;
		for(int i = 1 ; i <= n ; i++)
		{
			if(a[i] > d)
			{
				d = a[i];//新d
			}
		}
		for(int i = 1 ; i <= n ; i++)
		{
			b[i] = (1ll)*(d - a[i]);//操作两次
		}
		d = -INT_MAX;
		for(int i = 1 ; i <= n ; i++)
		{
			if(b[i] > d)
			{
				d = b[i];//新d
			}
		}
		for(int i = 1 ; i <= n ; i++)
		{
			c[i] = (1ll)*(d - b[i]);//操作三次
		}
		if( k % 2 == 0 )
		{
			for(int i = 1 ; i <= n ; i++)
			{
				cout<<b[i]<<" ";
			}
			cout<<endl;
		}else if( k % 3 == 0 )
		{
			for(int i = 1 ; i <= n ; i++)
			{
				cout<<c[i]<<" ";
			}
			cout<<endl;
		}else
		{
			for(int i = 1 ; i <= n ; i++)
			{
				cout<<a[i]<<" ";
			}
			cout<<endl;
		}
	}
	return 0;
}

C - Omkar and Waterslide

Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible.
Omkar currently has n supports arranged in a line, the i-th of which has height ai. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In 1 operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add 1 to each of their heights.
Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide!
An array b is a subsegment of an array c if b can be obtained from c by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.
An array b1,b2,…,bn is called nondecreasing if bi≤bi+1 for every i from 1 to n−1.

Input

Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.
The first line of each test case contains an integer n (1≤n≤2⋅105) — the number of supports Omkar has.
The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109) — the heights of the supports.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case, output a single integer — the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide.

input

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

output

3
2
0

Note

The subarray with which Omkar performs the operation is bolded.
In the first test case:
First operation:
[5,3,2,5]→[5,3,3,5]
Second operation:
[5,3,3,5]→[5,4,4,5]
Third operation:
[5,4,4,5]→[5,5,5,5]
In the third test case, the array is already nondecreasing, so Omkar does 0 operations.

分析

如果前一个数大于后一个数,我们操作使得后一个数与前一个数想同,也就是补到与前一个数字相同,补到相同之后可以一起加,使得操作次数最少
类似一个贪心补值的过程.还是用一个例子来说比较好理解
例子:5  4  3  2  5
对于给出的这个例子
2 消耗 (3-2) 补到 3 此时:5  4  3  3  5
然后往左
3 消耗 (4-3) 补到 4 此时:5  4  4  4  5
因为后面的2刚才补到了3跟原来的3是相同的,在原来的3变成4的过程中,由于两个相同的数是可以一起加的,所以两个3就同时加,变成4
4 消耗 (5-4) 补到 5 此时:5  5  5  5  5
因此消耗的次数是3

Code

#include
#include
#include
using namespace std;
//const int INF  = 0x3f3f3f;
const int maxn = 1e5+5;
typedef long long ll;
ll a[maxn*2];
int main()
{
	ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
	int t;
	cin>>t;
	while(t--)
	{
		memset(a,0,sizeof(a));
		int n;
		cin>>n;
//		int maxa = -INF;
//		int mina = INF;
		for(int i = 1 ; i <= n ; i++)
		{
			cin>>a[i];
		}
		ll Ans = 0;
		for(int i = n ; i > 1 ; i--)
		{
			if(a[i-1] >= a[i])
			{
				Ans += a[i-1] - a[i];
			}
		}
		cout<<Ans<<endl;
	}
	return 0;
}
//1
//5
//5 4 3 2 5
//3

你可能感兴趣的:(Codeforces,Round)