Codeforces Round #723 (Rated for Div 2)
A. Mean Inequality
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
You are given an array \(a\) of \(2n\) distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its \(2\) neighbours.
More formally, find an array \(b\), such that:
- \(b\) is a permutation of \(a\).
- For every \(i\) from \(1\) to \(2n\), \(b \neq \frac{b_{i−1} + b-{i+1}}{2}\), where \(b_{0}=b_{2n}\) and \(b_{2n+1}=b_{1}\) .
It can be proved that under the constraints of this problem, such array b always exists.
Input
The first line of input contains a single integer \(t (1\leqslant t \leqslant 1000)\) — the number of testcases. The description of testcases follows.
The first line of each testcase contains a single integer \(n (1\leqslant n\leqslant 25)\) .
The second line of each testcase contains 2n integers \(a_{1},a_{2},\cdots ,a_{2n} (1\leqslant a_{i}\leqslant 109)\) — elements of the array.
Note that there is no limit to the sum of n over all testcases.
Output
For each testcase, you should output \(2n\) integers, \(b_{1},b_{2},\cdots ,b_{2n}\) for which the conditions from the statement are satisfied.
Example
input
3
3
1 2 3 4 5 6
2
123 456 789 10
1
6 9
output
3 1 4 2 5 6
123 10 456 789
9 6
Note
In the first testcase, array \([3,1,4,2,5,6]\) works, as it's a permutation of \([1,2,3,4,5,6]\), and \(3+42\neq 1, 1+22\neq 4, 4+52\neq 2, 2+62\neq 5, 5+32\neq 6, 6+12\neq 3\) .
Solution
要求中间数字不为两边数字的平均值(包括第0和n-1个),那么只要排序后头尾交错输出即可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <list>
using namespace std;
int main()
{
int t;
int n;
int sta[1005];
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%d", &n);
for (int i = 0; i < n*2; i++)
scanf("%d", &sta[i]);
sort(sta, sta + 2 * n);
printf("%d %d", sta[0], sta[2*n-1]);
for (int i = 1; i < n; i++) {
printf(" %d %d", sta[i], sta[2*n-i-1]);
}
putchar('\n');
}
return 0;
}
B. I Hate 1111
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
You are given an integer \(x\). Can you make \(x\) by summing up some number of \(11,111,1111,11111,\cdots\)? (You can use any number among them any number of times).
For instance,
- \(33=11+11+11\)
- \(144=111+11+11+11\)
Input
The first line of input contains a single integer \(t (1\leqslant t\leqslant 10000)\) — the number of testcases.
The first and only line of each testcase contains a single integer \(x (1\leqslant x\leqslant 109)\) — the number you have to make.
Output
For each testcase, you should output a single string. If you can make \(x\), output "YES" (without quotes). Otherwise, output "NO".
You can print each letter of "YES" and "NO" in any case (upper or lower).
Example
input
3
33
144
69
output
YES
YES
NO
Note
Ways to make 33 and 144 were presented in the statement. It can be proved that we can't present 69 this way.
Solution
可以发现,除了11以外,其他都可以用多个11加1的形式替换:如111可以转换为11*10+1,1111可以转换为11*100+1。那么显然最少10个11可以带走一个1。显然如果一个所给的数不能被11整除,只要有足够的11来带走余下的数即可,否则不可。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <list>
using namespace std;
int main()
{
long long k;
int len, t;
scanf("%d", &t);
for (int i = 0; i < t; i++) {
scanf("%lld", &k);
int k11 = k / 11;
int klft = k % 11;
if (k11 / 10 >= klft) printf("YES\n");
else printf("NO\n");
}
return 0;
}
C2. Potions (Hard Version)
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
This is the hard version of the problem. The only difference is that in this version \(n\leqslant 200000\). You can make hacks only if both versions of the problem are solved.
There are \(n\) potions in a line, with potion \(1\) on the far left and potion \(n\) on the far right. Each potion will increase your health by \(a_{i}\) when drunk. \(a_{i}\) can be negative, meaning that potion will decrease will health.
You start with \(0\) health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.
What is the largest number of potions you can drink?
Input
The first line contains a single integer \(n (1\leqslant n\leqslant 200000)\) — the number of potions.
The next line contains \(n\) integers \(a_{1}, a_{2}, \cdots ,a_{n} (−109\leqslant a_{i} \leqslant 109)\) which represent the change in health after drinking that potion.
Output
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
Example
input
6
4 -4 1 -3 1 -3
output
5
Note
For the sample, you can drink \(5\) potions by taking potions \(1, 3, 4, 5\) and \(6\). It is not possible to drink all \(6\) potions because your health will go negative at some point
Solution
反悔贪心,如果健康值大于0就一直吃,如果健康值不足,那么就和已经吃的扣血最多的比较。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <queue>
using namespace std;
int main()
{
int n;
int ans = 0;
long long H = 0;
priority_queue<int, vector<int>, greater<int>> pq;
int sta;
scanf("%d", &n);
for (int i=0; i<n; i++) {
scanf("%d", &sta);
if (H + sta >= 0) {
pq.push(sta);
H += (long long)sta;
ans ++;
} else if (!pq.empty() && (pq.top() < 0 && pq.top() < sta)) {
H -= pq.top();
pq.pop();
H += (long long)sta;
pq.push(sta);
}
}
printf("%d\n", ans);
return 0;
}
by SDUST weilinfox