Codeforces Round #784 (Div. 4)

A. Division?

Codeforces separates its users into 4 divisions by their rating:

For Division 1: 1900≤rating
For Division 2: 1600≤rating≤1899
For Division 3: 1400≤rating≤1599
For Division 4: rating≤1399
Given a rating, print in which division the rating belongs.

Input
The first line of the input contains an integer t (1≤t≤104) — the number of testcases.

The description of each test consists of one line containing one integer rating (−5000≤rating≤5000).

Output
For each test case, output a single line containing the correct division in the format “Division X”, where X is an integer between 1 and 4 representing the division for the corresponding rating.

Example
inputCopy

7
-789
1299
1300
1399
1400
1679
2300
outputCopy
Division 4
Division 4
Division 4
Division 4
Division 3
Division 2
Division 1

Note
For test cases 1−4, the corresponding ratings are −789, 1299, 1300, 1399, so all of them are in division 4.

For the fifth test case, the corresponding rating is 1400, so it is in division 3.

For the sixth test case, the corresponding rating is 1679, so it is in division 2.

For the seventh test case, the corresponding rating is 2300, so it is in division 1.

代码

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	int T, a;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &a);
		if (a >= 1900) 
			printf("Division 1\n");
		else if (a >= 1600 && a <= 1899)
			printf("Division 2\n");
		else if (a >= 1400 && a <= 1599)
			printf("Division 3\n");
		else if (a <= 1399)
			printf("Division 4\n");
	}
	return 0;
}

B. Triple

Given an array a of n elements, print any value that appears at least three times or print -1 if there is no such value.

Input
The first line contains an integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of the array.

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

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

Output
For each test case, print any value that appears at least three times or print -1 if there is no such value.

Example

inputCopy
7
1
1
3
2 2 2
7
2 2 3 3 4 2 2
8
1 4 3 4 3 2 4 1
9
1 1 1 2 2 2 3 3 3
5
1 5 2 4 3
4
4 4 4 4
outputCopy
-1
2
2
4
3
-1
4

Note
In the first test case there is just a single element, so it can’t occur at least three times and the answer is -1.

In the second test case, all three elements of the array are equal to 2, so 2 occurs three times, and so the answer is 2.

For the third test case, 2 occurs four times, so the answer is 2.

For the fourth test case, 4 occurs three times, so the answer is 4.

For the fifth test case, 1, 2 and 3 all occur at least three times, so they are all valid outputs.

For the sixth test case, all elements are distinct, so none of them occurs at least three times and the answer is -1.

题目大意
输入一堆数字,如果有一个数字出现了三次及以上,就输出这个数字,如果有多个数字,任意输出一个即可,如果没有就输出-1;

代码

输入的时候直接用map接着,然后判断大于等于3的记录一下,最后的时候输出就好了;

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;
int T, n;
int main() {
	scanf("%d", &T);
	while (T--) {
		bool flag = false;
		map<int, int> mp;
		scanf("%d", &n);
		while (n--) {
			int a;
			scanf("%d", &a);
			mp[a]++;
			if (mp[a] >= 3 && flag == false) {
				printf("%d\n", a);
				flag = true;
			}
		}
		if (flag == false) printf("-1\n");
	}
	return 0;
}

C. Odd/Even Increments

Given an array a=[a1,a2,…,an] of n positive integers, you can do operations of two types on it:

Add 1 to every element with an odd index. In other words change the array as follows: a1:=a1+1,a3:=a3+1,a5:=a5+1,….
Add 1 to every element with an even index. In other words change the array as follows: a2:=a2+1,a4:=a4+1,a6:=a6+1,….
Determine if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers. In other words, determine if you can make all elements of the array have the same parity after any number of operations.

Note that you can do operations of both types any number of times (even none). Operations of different types can be performed a different number of times.

Input
The first line contains an integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains an integer n (2≤n≤50) — the length of the array.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤103) — the elements of the array.

Note that after the performed operations the elements in the array can become greater than 103.

Output
Output t lines, each of which contains the answer to the corresponding test case. As an answer, output “YES” if after any number of operations it is possible to make the final array contain only even numbers or only odd numbers, and “NO” otherwise.

You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

Example

inputCopy
4
3
1 2 1
4
2 2 2 3
4
2 2 2 2
5
1000 1 1000 1 1000
outputCopy
YES
NO
YES
YES

Note
For the first test case, we can increment the elements with an even index, obtaining the array [1,3,1], which contains only odd numbers, so the answer is “YES”.

For the second test case, we can show that after performing any number of operations we won’t be able to make all elements have the same parity, so the answer is “NO”.

For the third test case, all elements already have the same parity so the answer is “YES”.

For the fourth test case, we can perform one operation and increase all elements at odd positions by 1, thus obtaining the array [1001,1,1001,1,1001], and all elements become odd so the answer is “YES”.

题目大意

给定一个int数组,每次可以对所有奇数位置的数字都+1,或者偶数位置都+1,可以操作任意次数,也可以一次也不操作。判断最后是否能让所有的数字同时奇数或者偶数

代码

只需要判断同是偶数位置或者奇数位置上面有没有一个是奇数一个是偶数的,如果有就no

#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
int T, n;
int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		int a, p = 0, q = 0, r = 0, s = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a);
			if (i % 2) {
				if (a % 2) p = 1;
				else q = 1;
			}
			else {
				if (a % 2) r = 1;
				else s = 1;
			}
		}
		if ((p == 1 && q == 1) || (r == 1 && s == 1)) 
			printf("NO\n");
		else 
			printf("YES\n");
	}
	return 0;
}

E. 2-Letter Strings

Given n strings, each of length 2, consisting of lowercase Latin alphabet letters from ‘a’ to ‘k’, output the number of pairs of indices (i,j) such that i<j and the i-th string and the j-th string differ in exactly one position.

In other words, count the number of pairs (i,j) (i<j) such that the i-th string and the j-th string have exactly one position p (1≤p≤2) such that sip≠sjp.

The answer may not fit into 32-bit integer type, so you should use 64-bit integers like long long in C++ to avoid integer overflow.

Input
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (1≤n≤105) — the number of strings.

Then follows n lines, the i-th of which containing a single string si of length 2, consisting of lowercase Latin letters from ‘a’ to ‘k’.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case, print a single integer — the number of pairs (i,j) (i<j) such that the i-th string and the j-th string have exactly one position p (1≤p≤2) such that sip≠sjp.

Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Example

inputCopy
4
6
ab
cb
db
aa
cc
ef
7
aa
bb
cc
ac
ca
bb
aa
4
kk
kk
ab
ab
5
jf
jf
jk
jk
jk
outputCopy
5
6
0
6

Note
For the first test case the pairs that differ in exactly one position are: (“ab”, “cb”), (“ab”, “db”), (“ab”, “aa”), (“cb”, “db”) and (“cb”, “cc”).

For the second test case the pairs that differ in exactly one position are: (“aa”, “ac”), (“aa”, “ca”), (“cc”, “ac”), (“cc”, “ca”), (“ac”, “aa”) and (“ca”, “aa”).

For the third test case, the are no pairs satisfying the conditions.

题目大意
给n个字符串,每个字符串含2个字符,遍历,判断和该字符串只差一个字符不相等的字符串有多少个,
遍历n个字符串。

代码1

将其转化成数字0~25,放到一个二维的int数组里面来写。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>

using namespace std;

const int  N = 100001;
long long cnt;
int T;
int s[26][26];
int main() {
	scanf("%d", &T);
	while (T--) {
		cnt = 0;
		memset(s, 0, sizeof(s));
		int n;
		scanf("%d", &n);
		while (n--) {
			getchar();
			char a, b;
			scanf("%c%c", &a, &b);
			a -= 'a', b -= 'a';
			for (int i = 0; i < 26; i++)
				for (int j = 0; j < 26; j++)
					if ((i != a) + (j != b) == 1) //某个字符串和ab只有一个字符不相等
						cnt += s[i][j];
			s[a][b]++;
		}
		
		printf("%lld\n", cnt);
	}
	
	
	return 0;
}

代码2

map来写,枚举每一种情况并且统计

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
 
using namespace std;
 
const int  N = 100001;
long long cnt;
int T;
int main() {
	scanf("%d", &T);
	while (T--) {
		map<string, int> mp;
		vector<string> s(N);
		int n;
		cnt = 0;
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			cin >> s[i], mp[s[i]]++;
			
		for (int i = 0; i < n; i++) {
			// string t = s[i];
			for (int j = 0; j < 2; j++) {
				string t = s[i];// 必须定义在这个里面,因为j变化的时候要让t恢复到和s[i]一样,这样方便下面修改第二个字符
				for (char k = 'a'; k <= 'k'; k++) {
					t[j] = k;
					if (t != s[i])
					cnt += mp[t];
				}
			}
		}
		printf("%lld\n", cnt / 2);// 这几个字符串会重复,所以要除2
	}
	
	return 0;
}

F. Eating Candies

There are n candies put from left to right on a table. The candies are numbered from left to right. The i-th candy has weight wi. Alice and Bob eat candies.

Alice can eat any number of candies from the left (she can’t skip candies, she eats them in a row).

Bob can eat any number of candies from the right (he can’t skip candies, he eats them in a row).

Of course, if Alice ate a candy, Bob can’t eat it (and vice versa).

They want to be fair. Their goal is to eat the same total weight of candies. What is the most number of candies they can eat in total?

Input
The first line contains an integer t (1≤t≤104) — the number of test cases.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the number of candies on the table.

The second line of each test case contains n integers w1,w2,…,wn (1≤wi≤104) — the weights of candies from left to right.

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

Output
For each test case, print a single integer — the maximum number of candies Alice and Bob can eat in total while satisfying the condition.

Example
input

4
3
10 20 10
6
2 1 4 2 4 1
5
1 2 4 8 16
9
7 3 20 5 15 1 11 8 10

output

2
6
0
7

Note
For the first test case, Alice will eat one candy from the left and Bob will eat one candy from the right. There is no better way for them to eat the same total amount of weight. The answer is 2 because they eat two candies in total.

For the second test case, Alice will eat the first three candies from the left (with total weight 7) and Bob will eat the first three candies from the right (with total weight 7). They cannot eat more candies since all the candies have been eaten, so the answer is 6 (because they eat six candies in total).

For the third test case, there is no way Alice and Bob will eat the same non-zero weight so the answer is 0.

For the fourth test case, Alice will eat candies with weights [7,3,20] and Bob will eat candies with weights [10,8,11,1], they each eat 30 weight. There is no better partition so the answer is 7.

题目大意
给出一个数组,数组中每个位置的数都代表糖果的重量,两个人一个从分别从左右两边来吃糖果,不可以跳过某个位置,不可以吃相同位置的。
我们模拟这个吃糖果的过程,两个指针,从左右两边开始移动
ming是左边人吃的糖果总重量,hong是右边的人吃的糖果总重量。
初始化 l = -1, r = n;
之后模拟过程,开始判断

  • ming == hong,二者-=现在吃的糖果总重量相等,namo就来更新一下吃的糖果总数的最大值,maxn = (l + 1) + (n - r)
  • 如果ming > hong,左边重量大,让右边吃
  • ming < hong,右边重量大,让左边吃

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2e5 + 1;

int candy[N], n, maxn;
void solve() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &candy[i]);

  int l = -1, r = n;
  int ming = 0, hong = 0;
  while (l < r) {
    if (ming == hong) {
      maxn = (l + 1) + (n - r);
      ming += candy[++l];
      hong += candy[--r];
    } else
      if (ming < hong) {
      ming += candy[++l];
    } else {
      hong += candy[--r];
    }
  }

  printf("%d\n", maxn);
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		solve();
	}
	
	return 0;
}

H. Maximal AND

Let AND denote the bitwise AND operation, and OR denote the bitwise OR operation.

You are given an array a of length n and a non-negative integer k. You can perform at most k operations on the array of the following type:

Select an index i (1≤i≤n) and replace ai with ai OR 2j where j is any integer between 0 and 30 inclusive. In other words, in an operation you can choose an index i (1≤i≤n) and set the j-th bit of ai to 1 (0≤j≤30).
Output the maximum possible value of a1 AND a2 AND … AND an after performing at most k operations.

Input
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains the integers n and k (1≤n≤2⋅105, 0≤k≤109).

Then a single line follows, containing n integers describing the arrays a (0≤ai<231).

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 line containing the maximum possible AND value of a1 AND a2 AND … AND an after performing at most k operations.

Example

input

4
3 2
2 1 1
7 0
4 6 6 28 6 6 12
1 30
0
4 4
3 1 3 1

output

2
4
2147483646
1073741825

Note
For the first test case, we can set the bit 1 (21) of the last 2 elements using the 2 operations, thus obtaining the array [2, 3, 3], which has AND value equal to 2.

For the second test case, we can’t perform any operations so the answer is just the AND of the whole array which is 4.

思路
大意就是给一个数组,n个数,最多可以操作k次,输出最后的按位与的最大值。
每次操作就是把n中的第i个数 按位或一个 2j,其实就是把第i个数的第j位变成1
这题是贪心的思路。
我们统一从最高位开始模拟,遍历这个数组,对每个数的最高位进行操作,如果是1了就不动,不是1就把它变成1,然后cnt++,cnt用来记录这次遍历操作了多少次;
如果k是大于等于cnt的,说明这次遍历结束之后把这个每个数的这个位置都能变成1,所以最后按位与的时候这个位置也是1。
然后 k -= cnt,更新一下k。
想了好久为什么从最大化位开始不会出错,最后才明白,如果数太多k是小的,n个数最大位都是0,那么一轮操作之后把它们都变成1的cnt一定是大于k的,所以不会影响后面。

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 2e5 + 1;
int s[N];
int n, k;
void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &s[i]);

  // 贪心
  int res = 0;
  for (int i = 30; i >= 0; i--) {
    int cnt = 0;
    for (int j = 0; j < n; j++)
      if (((s[j] >> i) & 1) == 0)
        cnt++;

    if (k >= cnt) {
      res |= (1 << i), k -= cnt;
    }
  }

  printf("%d\n", res);

}
int main() {
  int T;
  scanf("%d", &T);
  while (T--)
    solve();
  return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值