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] 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=⋯=an).
- In one operation, he can take some indices in the array and increase the elements of the array at those indices by 1.
For example, let 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 1. As a result, in one operation, he can get a new state of the array 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=⋯=an)?
Input
The first line of the input contains a single integer t (1≤t≤1e4) — 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 n (1≤n≤50) — the array a.
The second line of the description of each test case contains nn integers a1,a2,…,an (1≤ai≤1e9) — 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.
Example
input
3 6 3 4 2 4 1 2 3 1000 1002 998 2 12 11output
3 4 1Note
First test case:
- a=[3,4,2,4,1,2] take a3,a5 and perform an operation plus one on them, as a result we get a=[3,4,3,4,2,2]
- a=[3,4,3,4,2,2] we take a1,a5,a6 and perform an operation on them plus one, as a result we get a=[4,4,3,4,3,3]
- a=[4,4,3,4,3,3]we take a3,a5,a6 and perform an operation on them plus one, as a result we get a=[4,4,4,4,4,4]
There are other sequences of 3 operations, after the application of which all elements become equal.
Second test case:
- a=[1000,1002,998] 2 times we take a1,a3 and perform an operation plus one on them, as a result we get a=[1002,1002,1000]
- a=[1002,1002,1000]also take a3 2 times and perform an operation plus one on it, as a result we get a=[1002,1002,1002]
Third test case:
- a=[12,11] take a2 and perform an operation plus one on it, as a result we get a=[12,12]
最大值和最小值的差值即为答案
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <cstring> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int num; cin >> num; int max1 = 0, min1 = 0x3f3f3f3f; int a[55]; for (int i = 0; i < num; i++) { cin >> a[i]; if (a[i] > max1) { max1 = a[i]; } if (a[i] < min1) { min1 = a[i]; } } cout << max1 - min1 << '\n'; } return 0; }
B. Make AP
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarp has 3 positive integers a, b and c. He can perform the following operation exactly once.
- Choose a positive integer m and multiply exactly one of the integers a, b or c by m.
Can Polycarp make it so that after performing the operation, the sequence of three numbers a, b, c (in this order) forms an arithmetic progression? Note that you cannot change the order of a, b and c.
Formally, a sequence x1,x2,…,xn is called an arithmetic progression (AP) if there exists a number d (called "common difference") such that xi+1=xi+d for all i from 1 to n−1. In this problem, n=3.
For example, the following sequences are AP: [5,10,15], [3,2,1], [1,1,1], and [13,10,7]. The following sequences are not AP: [1,2,4], [0,1,0] and [1,3,2].
You need to answer tt independent test cases.
Input
The first line contains the number t (1≤t≤1e4) — the number of test cases.
Each of the following t lines contains 3 integers a, b, c (1≤a,b,c≤1e8).
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).
Example
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 2output
YES YES YES YES NO YES NO YES YES NO YESNote
In the first and second test cases, you can choose the number m=4 and multiply the second number (b=5) by 4.
In the first test case the resulting sequence will be [10,20,30]. This is an AP with a difference d=10.
In the second test case the resulting sequence will be [30,20,10]. This is an AP with a difference d=−10.
In the third test case, you can choose m=1 and multiply any number by 1. The resulting sequence will be [1,2,3]. This is an AP with a difference d=1.
In the fourth test case, you can choose m=9 and multiply the first number (a=1) by 9. The resulting sequence will be [9,6,3]. This is an AP with a difference d=−3.
In the fifth test case, it is impossible to make an AP.
因为词穷QAQ,以为是加某个数,原来是要乘某个数,卡了半天,还是要好好学英语,根据等差数列的性质,判断b是否符合
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <cstring> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; if (2 * b - a > 0 && (2 * b - a) % c == 0) { cout << "YES" << endl; } else if (2 * b - c > 0 && (2 * b - c) % a == 0) { cout << "YES" << endl; } else if ((a + c) % 2 == 0 && (a + c) / 2 % b == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
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 a consisting of nn positive integers. You can perform operations on it.
In one operation you can replace any element of the array aiai with ⌊ai/2⌋⌋, that is, by an integer part of dividing ai by 2 (rounding down).
See if you can apply the operation some number of times (possible 0) to make the array a become a permutation of numbers from 1 to n —that is, so that it contains all numbers from 1 to n, each exactly once.
For example, if a=[1,8,25,2], n=4, then the answer is yes. You could do the following:
- Replace 8 with ⌊8/2⌋=4, then a=[1,4,25,2]
- Replace 25 with ⌊25/2⌋=12, then a=[1,4,12,2]
- Replace 12 with ⌊12/2⌋=6, then a=[1,4,6,2]
- Replace 6 with ⌊6/2⌋=3, then a=[1,4,3,2]
Input
The first line of input data contains an integer t (1≤t≤1e4) —the number of test cases.
Each test case contains exactly two lines. The first one contains an integer n (1≤n≤50), the second one contains integers a1,a2,…,an (1≤ai≤1e9).
Output
For each test case, output on a separate line:
- YES if you can make the array aa become a permutation of numbers from 1 to n,
- 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).
Example
input
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 22output
YES NO YES NO NO YESNote
The first test case is explained in the text of the problem statement.
In the second test case, it is not possible to get a permutation
判断能否组成1到n的全排列,先把所有大于n的都除以2,再倒着判断n,n-1...,因为如果没有n,那永远也组成不了全排列,小的数可能由大的数除以2得到,但大的数就无法得到了
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <string> #include <cstring> #include <vector> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { while (a[i] > n) { a[i] /= 2; } } bool ok = true; for (int i = n; i >= 1; i--) { vector<int> p; for (int k = 0; k < n; k++) { if (a[k] == i) { p.push_back(k); } } if (p.empty()) { ok = false; break; } else { int cnt = p.size(); for (int j = 1; j < cnt; j++) { a[p[j]] /= 2; } } } if (ok) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
Codeforces Round #764 (Div. 3)A B C题解
于 2022-01-11 20:15:59 首次发布