Problem A
A. Plus One on the Subset
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp got an array of integers a[1…n]a[1…n] as a gift. Now he wants to perform a certain number of operations (possibly zero) so that all elements of the array become the same (that is, to become a1=a2=⋯=ana1=a2=⋯=an).
- In one operation, he can take some indices in the array and increase the elements of the array at those indices by 11.
For example, let a=[4,2,1,6,2]a=[4,2,1,6,2]. He can perform the following operation: select indices 1, 2, and 4 and increase elements of the array in those indices by 11. As a result, in one operation, he can get a new state of the array a=[5,3,1,7,2]a=[5,3,1,7,2].
What is the minimum number of operations it can take so that all elements of the array become equal to each other (that is, to become a1=a2=⋯=ana1=a2=⋯=an)?
Input
The first line of the input contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test.
The following are descriptions of the input test cases.
The first line of the description of each test case contains one integer nn (1≤n≤501≤n≤50) — the array aa.
The second line of the description of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — elements of the array aa.
Output
For each test case, print one integer — the minimum number of operations to make all elements of the array aa equal.
题解:
题目意思是;给你一个数组,对数组元素每次若干个元素加一,问最少多少次可以使元素的所有值都相等,只要那最大的元素减去最小的元素 就行
intput:
3
6
3 4 2 4 1 2
3
1000 1002 998
2
12 11
output:
3
4
1
#include<iostream>
using namespace std;
const int MAX = 55;
int t, n;
int arr[MAX];
int main() {
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int minn =arr[0];
int maxn = arr[0];
for (int j = 0; j < n; j++) {
if (arr[j] < minn) {
minn = arr[j];
}
if (arr[j] > maxn) {
maxn = arr[j];
}
}
cout << maxn - minn << endl;
}
return 0;
}
Promblem B
B. Make AP
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp has 33 positive integers aa, bb and cc. He can perform the following operation exactly once.
- Choose a positive integer mm and multiply exactly one of the integers aa, bb or cc by mm.
Can Polycarp make it so that after performing the operation, the sequence of three numbers aa, bb, cc (in this order) forms an arithmetic progression? Note that you cannot change the order of aa, bb and cc.
Formally, a sequence x1,x2,…,xnx1,x2,…,xn is called an arithmetic progression (AP) if there exists a number dd (called "common difference") such that xi+1=xi+dxi+1=xi+d for all ii from 11 to n−1n−1. In this problem, n=3n=3.
For example, the following sequences are AP: [5,10,15][5,10,15], [3,2,1][3,2,1], [1,1,1][1,1,1], and [13,10,7][13,10,7]. The following sequences are not AP: [1,2,4][1,2,4], [0,1,0][0,1,0] and [1,3,2][1,3,2].
You need to answer tt independent test cases.
Input
The first line contains the number tt (1≤t≤1041≤t≤104) — the number of test cases.
Each of the following tt lines contains 33 integers aa, bb, cc (1≤a,b,c≤1081≤a,b,c≤108).
Output
For each test case print "YES" (without quotes) if Polycarp can choose a positive integer mm and multiply exactly one of the integers aa, bb or cc by mm to make [a,b,c][a,b,c] be an arithmetic progression. Print "NO" (without quotes) otherwise.
You can print YES and NO in any (upper or lower) case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer).
题解:
给你一个三个数a,b,c.如果有一个数m乘以其中一个数使得a,b,c成等差数列(a,b,c的位置不能发生改变),则输出“YES”,否则输出“NO”,这题找到规律就行
input:
11
10 5 30
30 5 10
1 2 3
1 6 3
2 6 3
1 1 1
1 1 2
1 1 3
1 100000000 1
2 1 1
1 2 2
output:
YES
YES
YES
YES
NO
YES
NO
YES
YES
NO
YES
#include <iostream>
using namespace std;
inline bool solve(int a, int b, int c) {
return false
or (2 * b > c and (((2 * b - c) % a) == 0))
or (2 * b > a and (((2 * b - a) % c) == 0))
or (((a + c) % (2 * b)) == 0)
;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) {
int a, b, c;
cin >> a >> b >> c;
cout << (solve(a, b, c) ? "YES\n" : "NO\n");
}
}
Problem C
C. Division by Two and Permutation
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array aa consisting of nn positive integers. You can perform operations on it.
In one operation you can replace any element of the array aiai with ⌊ai2⌋⌊ai2⌋, that is, by an integer part of dividing aiai by 22 (rounding down).
See if you can apply the operation some number of times (possible 00) to make the array aa become a permutation of numbers from 11 to nn —that is, so that it contains all numbers from 11 to nn, each exactly once.
For example, if a=[1,8,25,2]a=[1,8,25,2], n=4n=4, then the answer is yes. You could do the following:
- Replace 88 with ⌊82⌋=4⌊82⌋=4, then a=[1,4,25,2]a=[1,4,25,2].
- Replace 2525 with ⌊252⌋=12⌊252⌋=12, then a=[1,4,12,2]a=[1,4,12,2].
- Replace 1212 with ⌊122⌋=6⌊122⌋=6, then a=[1,4,6,2]a=[1,4,6,2].
- Replace 66 with ⌊62⌋=3⌊62⌋=3, then a=[1,4,3,2]a=[1,4,3,2].
Input
The first line of input data contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases.
Each test case contains exactly two lines. The first one contains an integer nn (1≤n≤501≤n≤50), the second one contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).
Output
For each test case, output on a separate line:
- YES if you can make the array aa become a permutation of numbers from 11 to nn,
- NO otherwise.
You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).
题解:
题目的意思是给你一个数组,你可以对数组的元素一直除以2(等于零的时候就不要除了),将每个元素对应1到n的一个值,如果1到n都有一个不重复的元素对应,就输出“YES”,否则输出“NO”,
这题将元素按从大到小排列,从大到小一次进行除2,只要除数小于n并且对应标记数组没有被标记,就将对应标记数进行标记(对应的第一个元素就是最优解,不应担心多个元素抢一个元素映射)
intput:
6
4
1 8 25 2
2
1 1
9
9 8 3 4 2 7 1 5 6
3
8 2 1
4
24 7 16 7
5
22 6 22 4 22
output:
YES
NO
YES
NO
NO
YES
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX = 55;
int t, n;
int arr[MAX];
int arr1[MAX];
inline bool compare(int a, int b) {
return a > b;
}
int main() {
cin >> t;
while (t--) {
memset(arr1, 0, sizeof(int) * MAX);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
//从大到小排序
sort(&arr[1], &arr[1] + n, compare);
//对数组每个元素进行映射
for (int i = 1; i <= n; i++) {
for (int j = arr[i]; j != 0; j = j / 2) {
if (j <= n and arr1[j] == 0) {
arr1[j] = 1;
break;
}
}
}
//遍历看标记数组是否全为1
int num = 0;
for (int i = 1; i <= n; i++) {
if (arr1[i] == 0) {
cout << "NO" << endl;
break;
}
num++;
}
if (num == n)
cout << "YES" << endl;
}
return 0;
}
D. Palindromes Coloring
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a string ss consisting of lowercase Latin alphabet letters.
You can color some letters in colors from 11 to kk. It is not necessary to paint all the letters. But for each color, there must be a letter painted in that color.
Then you can swap any two symbols painted in the same color as many times as you want.
After that, kk strings will be created, ii-th of them will contain all the characters colored in the color ii, written in the order of their sequence in the string ss.
Your task is to color the characters of the string so that all the resulting kk strings are palindromes, and the length of the shortest of these kk strings is as large as possible.
Read the note for the first test case of the example if you need a clarification.
Recall that a string is a palindrome if it reads the same way both from left to right and from right to left. For example, the strings abacaba, cccc, z and dxd are palindromes, but the strings abab and aaabaa — are not.
Input
The first line of input data contains a single integer tt (1≤t≤1041≤t≤104) — the number of input data sets in the test.
The descriptions of the input data sets follow.
The first line of the description of each input data set contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the length of the string and the number of colors in which its letters can be painted. The second line of the description of each input data set contains a string ss of length nn consisting of lowercase letters of the Latin alphabet.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅1052⋅105.
input:
10
8 2
bxyaxzay
6 3
aaaaaa
6 1
abcdef
6 6
abcdef
3 2
dxd
11 2
abcabcabcac
6 6
sipkic
7 2
eatoohd
3 1
llw
6 2
bfvfbv
output:
3
2
1
1
1
5
1
1
3
3
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX_N = 2e5;
int main(int argc, char* argv[]) {
int t;
cin >> t;
for (int _ = 0; _ < t; ++_) {
int n, k;
string s;
cin >> n >> k >> s;
vector<int> cnt(26);
for (char c : s) {
cnt[c - 'a']++;
}
int cntPairs = 0, cntOdd = 0;
for (int c : cnt) {
cntPairs += c / 2;
cntOdd += c % 2;
}
int ans = 2 * (cntPairs / k);
cntOdd += 2 * (cntPairs % k);
if (cntOdd >= k) {
ans++;
}
cout << ans << '\n';
}
}