Codeforces: Round #706 (Div.2)
Codeforces
A. Split it!
Kawashiro Nitori is a girl who loves competitive programming.
One day she found a string and an integer. As an advanced problem setter, she quickly thought of a problem.
Given a string $𝑠$ and a parameter $𝑘$, you need to check if there exist $𝑘+1$ non-empty strings $𝑎_1,𝑎_2…,𝑎_{𝑘+1},$ such that
$𝑠=𝑎_1+𝑎_2+…+𝑎_𝑘+𝑎_{𝑘+1}+𝑅(𝑎_𝑘)+𝑅(𝑎_{𝑘−1})+…+𝑅(𝑎_1)$.
Here $+$ represents concatenation. We define $𝑅(𝑥)$ as a reversed string $𝑥$. For example $𝑅(𝑎𝑏𝑐𝑑)=𝑑𝑐𝑏𝑎$. Note that in the formula above the part $𝑅(𝑎_{𝑘+1})$ is intentionally skipped.
Input
The input consists of multiple test cases. The first line contains a single integer $𝑡$ $(1≤𝑡≤100)$ — the number of test cases. The description of the test cases follows.
The first line of each test case description contains two integers $𝑛, 𝑘$ $(1≤𝑛≤100, 0≤𝑘≤⌊\frac{𝑛}{2}⌋)$ — the length of the string $𝑠$ and the parameter $𝑘$.
The second line of each test case description contains a single string $𝑠$ of length $𝑛$, consisting of lowercase English letters.
Output
For each test case, print “YES” (without quotes), if it is possible to find $𝑎_1,𝑎_2,…,𝑎_{𝑘+1}$, and “NO” (without quotes) otherwise.
You can print letters in any case (upper or lower).
풀이
코드
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int n, k; cin >> n >> k;
string s; cin >> s;
bool flag = false;
if (k == 0) { cout << "YES\n"; return; }
for (int i = 0; i < k; i++) {
if (s[i] == s[n - i - 1]) continue;
flag = true;
}
if (!flag && n > 2 * k) cout << "YES\n";
else cout << "NO\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while(t--) solve();
return 0;
}
B. Max and Mex
You are given a multiset $𝑆$ initially consisting of $𝑛$ distinct non-negative integers. A multiset is a set, that can contain some elements multiple times.
You will perform the following operation 𝑘 times:
- Add the element $⌈\frac{𝑎+𝑏}{2}⌉$ (rounded up) into $𝑆$, where $𝑎=mex(𝑆)$ and $𝑏=max(𝑆)$. If this number is already in the set, it is added again.
Here $max$ of a multiset denotes the maximum integer in the multiset, and $mex$ of a multiset denotes the smallest non-negative integer that is not present in the multiset. For example:
- $mex({1,4,0,2})=3$;
- $mex({2,5,1})=0$.
Your task is to calculate the number of distinct elements in $𝑆$ after $𝑘$ operations will be done.
Input
The input consists of multiple test cases. The first line contains a single integer $𝑡$ $(1≤𝑡≤100)$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $𝑛, 𝑘$ $(1≤𝑛≤10^5, 0≤𝑘≤10^9)$ — the initial size of the multiset $𝑆$ and how many operations you need to perform.
The second line of each test case contains $𝑛$ distinct integers $𝑎_1,𝑎_2,…,𝑎_𝑛$ $(0≤𝑎_𝑖≤10^9)$ — the numbers in the initial multiset.
It is guaranteed that the sum of $𝑛$ over all test cases does not exceed $10^5$.
Output
For each test case, print the number of distinct elements in $𝑆$ after $𝑘$ operations will be done.
풀이
코드
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int n, k; cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
if(k == 0) { cout << n << "\n"; return; }
sort(v.begin(), v.end());
int mx = *max_element(v.begin(), v.end());
int mex = -1;
for (int i = 0; i < n; i++) {
if (v[i] != i) { mex = i; break; }
}
if (mex == -1) { cout << n + k << "\n"; return; }
for (int i = 0; i < n; i++) {
if (v[i] == (mx + mex + 1) / 2) { cout << n << "\n"; return; }
}
cout << n + 1 << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while(t--) solve();
return 0;
}
C. Diamond Miner
Diamond Miner is a game that is similar to Gold Miner, but there are $𝑛$ miners instead of $1$ in this game.
The mining area can be described as a plane. The $𝑛$ miners can be regarded as $𝑛$ points on the y-axis. There are $𝑛$ diamond mines in the mining area. We can regard them as 𝑛 points on the x-axis. For some reason, no miners or diamond mines can be at the origin (point $(0,0)$).
Every miner should mine exactly one diamond mine. Every miner has a hook, which can be used to mine a diamond mine. If a miner at the point $(𝑎,𝑏)$ uses his hook to mine a diamond mine at the point $(𝑐,𝑑)$, he will spend $\sqrt{(𝑎−𝑐)^2+(𝑏−𝑑)^2}$ energy to mine it (the distance between these points). The miners can’t move or help each other.
The object of this game is to minimize the sum of the energy that miners spend. Can you find this minimum?
Input
The input consists of multiple test cases. The first line contains a single integer $𝑡$ $(1≤𝑡≤10)$ — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $𝑛$ $(1≤𝑛≤10^5)$ — the number of miners and mines.
Each of the next $2𝑛$ lines contains two space-separated integers $𝑥$ $(−10^8≤𝑥≤10^8)$ and $𝑦$ $(−10^8≤𝑦≤10^8)$, which represent the point $(𝑥,𝑦)$ to describe a miner’s or a diamond mine’s position. Either $𝑥=0$, meaning there is a miner at the point $(0,𝑦)$, or $𝑦=0$, meaning there is a diamond mine at the point $(𝑥,0)$. There can be multiple miners or diamond mines at the same point.
It is guaranteed that no point is at the origin. It is guaranteed that the number of points on the x-axis is equal to $𝑛$ and the number of points on the y-axis is equal to $𝑛$.
It’s guaranteed that the sum of $𝑛$ for all test cases does not exceed $10^5$.
Output
For each test case, print a single real number — the minimal sum of energy that should be spent.
Your answer is considered correct if its absolute or relative error does not exceed $10^{−9}$.
Formally, let your answer be $𝑎$, and the jury’s answer be $𝑏$. Your answer is accepted if and only if $\frac{|𝑎−𝑏|}{max(1,|𝑏|)}≤10^{−9}$.
풀이
코드
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
void solve() {
int n; cin >> n;
vector<ll> m;
vector<ll> d;
for (int i = 0; i < 2 * n; i++) {
int x, y; cin >> x >> y;
if (x == 0) m.push_back(abs(y));
else d.push_back(abs(x));
}
sort(m.begin(), m.end());
sort(d.begin(), d.end());
ld ans = 0;
for (int i = 0; i < n; i++) ans += sqrt(m[i] * m[i] + d[i] * d[i]);
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed; cout.precision(10);
int t; cin >> t;
while(t--) solve();
return 0;
}