Codeforces Round 983 (div 2) A-D
A. Circuit
Alice has just crafted a circuit with \(n\) lights and \(2n\) switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that:
- Each light is connected to exactly two switches.
- Each switch is connected to exactly one light. It's unknown which light each switch is connected to.
- When all switches are off, all lights are also off.
- If a switch is toggled (from on to off, or vice versa), the state of the light connected to it will also toggle.
Alice brings the circuit, which shows only the states of the \(2n\) switches, to her sister Iris and gives her a riddle: what is the minimum and maximum number of lights that can be turned on?
Knowing her little sister's antics too well, Iris takes no more than a second to give Alice a correct answer. Can you do the same?
Input
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer \(n\) (\(1 \le n \le 50\)) — the number of lights in the circuit.
The second line of each test case contains \(2n\) integers \(a_1, a_2, \ldots, a_{2n}\) (\(0 \le a_i \le 1\)) — the states of the switches in the circuit. \(a_i = 0\) means the \(i\)-th switch is off, and \(a_i = 1\) means the \(i\)-th switch is on.
Output
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.
大致思路: 由题意知开关状态为 \([0,1]\) 时,灯的状态为 \(on\),开关为 \([1,1] , [0,0]\) 时,灯的状态为 \(off\) . 所以统计数组中 0 和 1 的个数,记作 \((a, b)\). 则若求最小值,则尽可能地让 \([0,0],[1,1]\) 配对,若求最大值,则尽可能让 \([0,1]\) 配对。则可以得到如下判断:
①若 \(cnt0\) 为奇数,则 \(res_{min} = 1, res_{max} = min(a,b)\)
②若 \(cnt0\) 为偶数,则 \(res_{min} = 0, res_{max} = min(a,b)\)
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5+5;
int n;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fio();
int t; cin >> t;
while(t--)
{
int a=0,b=0;
cin >> n;
for(int i=1;i<=2*n;i++)
{
int num;
cin >> num;
if(num)a ++;
else b ++;
}
if(a % 2 == 0)cout << 0 << " "<< min(a,b)<<endl;
else cout << 1 <<" "<<min(a,b) << endl;
}
}
B. Medians
You are given an array \(a = [1, 2, \ldots, n]\), where \(n\) is odd, and an integer \(k\).
Your task is to choose an odd positive integer \(m\) and to split \(a\) into \(m\) subarrays\(^{\dagger}\) \(b_1, b_2, \ldots, b_m\) such that:
- Each element of the array \(a\) belongs to exactly one subarray.
- For all \(1 \le i \le m\), \(|b_i|\) is odd, i.e., the length of each subarray is odd.
- \(\operatorname{median}([\operatorname{median}(b_1), \operatorname{median}(b_2), \ldots, \operatorname{median}(b_m)]) = k\), i.e., the median\(^{\ddagger}\) of the array of medians of all subarrays must equal \(k\). \(\operatorname{median}(c)\) denotes the median of the array \(c\).
\(^{\dagger}\)A subarray of the array \(a\) of length \(n\) is the array \([a_l, a_{l + 1}, \ldots, a_r]\) for some integers \(1 \le l \le r \le n\).
\(^{\ddagger}\)A median of the array of odd length is the middle element after the array is sorted in non-decreasing order. For example: \(\operatorname{median}([1,2,5,4,3]) = 3\), \(\operatorname{median}([3,2,1]) = 2\), \(\operatorname{median}([2,1,2,1,2,2,2]) = 2\).
Input
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n < 2 \cdot 10^5\), \(n\) is odd) — the length of array \(a\) and the desired median of the array of medians of all subarrays.
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
Output
For each test case:
- If there is no suitable partition, output \(-1\) in a single line.
- Otherwise, in the first line, output an odd integer \(m\) (\(1 \le m \le n\)), and in the second line, output \(m\) distinct integers \(p_1, p_2 , p_3 , ..., p_m\) (\(1 = p_1 <p_2 < p_3 < ... < p_m \le n\)) — denoting the left borders of each subarray.
In detail, for a valid answer \([p_1, p_2, \ldots, p_m]\):
- \(b_1 = \left[ a_{p_1}, a_{p_1 + 1}, \ldots, a_{p_2 - 1} \right]\)
- \(b_2 = \left[ a_{p_2}, a_{p_2 + 1}, \ldots, a_{p_3 - 1} \right]\)
- \(\ldots\)
- \(b_m = \left[ a_{p_m}, a_{p_m + 1}, \ldots, a_n \right]\).
If there are multiple solutions, you can output any of them.
大致思路:易知,我们可以只用三个子数列便可以得到最终的答案,最简单的答案便是\([1,k-1]\) , \([k]\) , \([k+1,n]\) . 但是由于子数列的个数必须满足是奇数,而 \(n\) 也为奇数。故需要讨论 \(k\) 的奇偶性。
若 \(k\) 为偶数,则子数列 \([1,k-1]\) , \([k+1,n]\) 的元素个数都为奇数,故输出$ 1,k,k+1 $.
若 \(k\) 为奇数,则子数列 \([1,k-1]\) , \([k+1,n]\) 的元素个数都为偶数,而子数列 \([1,k-2]\) , \([k+2,n]\) 的元素个数都为奇数,故输出$ 1,k-1,k+2 $.
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5+5;
int n,k;
void fio()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fio();
int t; cin >> t;
while(t--)
{
cin >> n >> k;
if(n == 1 && k == 1)cout << 1 <<endl<<1<<endl;
else if (k == 1 || k == n)cout << -1 << endl;
else if(k % 2)
{
cout << 3 <<endl;
cout << 1 <<" "<< k-1 <<" "<< k + 2<<endl;
}
else
{
cout << 3 <<endl;
cout << 1 <<" "<< k <<" "<< k + 1<<endl;
}
}
}
C. Trinity
You are given an array \(a\) of \(n\) elements \(a_1, a_2, \ldots, a_n\).
You can perform the following operation any number (possibly \(0\)) of times:
- Choose two integers \(i\) and \(j\), where \(1 \le i, j \le n\), and assign \(a_i := a_j\).
Find the minimum number of operations required to make the array \(a\) satisfy the condition:
- For every pairwise distinct triplet of indices \((x, y, z)\) (\(1 \le x, y, z \le n\), \(x \ne y\), \(y \ne z\), \(x \ne z\)), there exists a non-degenerate triangle with side lengths \(a_x\), \(a_y\) and \(a_z\), i.e. \(a_x + a_y > a_z\), \(a_y + a_z> a_x\) and \(a_z + a_x > a_y\).
Input
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) — the number of elements in the array \(a\).
The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) — the elements of the array \(a\).
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
Output
For each test case, output a single integer — the minimum number of operations required.
大致思路:由题意,本题仅与元素的大小有关,而与元素的顺序无关。故先对 \(a_n\) 进行从小到大排序。要求对任意的 \(a_n\), 均满足 \(a_x + a_y > a_z,x < y < z\) , 求最小的 \(operation\). 考虑如果固定\(a_x, a_y\),最大的\(a_z = a_x +a_y - 1\)。利用二分可以求出 \(\ a_z\) 索引 $index $ 则 \(operation=\) $ n - (index - i + 1)$。则 \(res = min(res, operations)\)
代码如下:
# include<bits/stdc++.h>
using namespace std;
#define long long ll
const int N = 2e5 + 5;
int n;
void fio() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int a[N];
int main() {
fio();
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++)cin >> a[i];
sort(a + 1, a + n + 1);
int res = n;
for (int i = 1; i <= n - 1; i++) {
int sum = a[i] + a[i + 1];
int index = lower_bound(a + i + 1, a + 1 + n, sum) - a - 1;
res = min(res, n - (index - i + 1));
}
cout << res << endl;
}
}
D. Genokraken
This is an interactive problem.
Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies.
The monster's nerve system can be structured as a tree\(^{\dagger}\) of \(n\) nodes (really, everything should stop resembling trees all the time\(\ldots\)), numbered from \(0\) to \(n-1\), with node \(0\) as the root.
Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values \(p_1, p_2, < , p_{n-1}\) of the tree, where \(p_i\) (\(0 \le p_i < i\)) is the direct parent node of node \(i\) (\(1 \le i \le n - 1\)).
She doesn't know exactly how the nodes are placed, but she knows a few convenient facts:
- If we remove root node \(0\) and all adjacent edges, this tree will turn into a forest consisting of only paths\(^{\ddagger}\). Each node that was initially adjacent to the node \(0\) will be the end of some path.
- The nodes are indexed in a way that if \(1 \le x \le y \le n - 1\), then \(p_x \le p_y\).
- Node \(1\) has exactly two adjacent nodes (including the node \(0\)).
The tree in this picture does not satisfy the condition, because if we remove node \(0\), then node \(2\) (which was initially adjacent to the node \(0\)) will not be the end of the path \(4-2-5\). | The tree in this picture does not satisfy the condition, because \(p_3 \le p_4\) must hold. | The tree in this picture does not satisfy the condition, because node \(1\) has only one adjacent node. |
Gretel can make queries to the containment cell:
- "? a b" (\(1 \le a, b < n\), \(a \ne b\)) — the cell will check if the simple path between nodes \(a\) and \(b\) contains the node \(0\).
However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most \(2n - 6\) times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand?
\(^{\dagger}\)A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them.
\(^{\ddagger}\)A path is a tree whose vertices can be listed in the order \(v_1, v_2, \ldots, v_k\) such that the edges are \((v_i, v_{i+1})\) (\(1 \le i < k\)).
Input
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer \(n\) (\(4 \le n \le 10^4\)) — the number of nodes in Genokraken's nerve system.
It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^4\).
Interaction
For each test case, interaction starts by reading the integer \(n\).
Then you can make queries of the following type:
- "? a b" (without quotes) (\(1 \le a, b < n\), \(a \ne b\)).
After the query, read an integer \(r\) — the answer to your query. You are allowed to use at most \(2n - 6\) queries of this type.
- If the simple path between nodes \(a\) and \(b\) does not contain node \(0\), you will get \(r = 0\).
- If the simple path between nodes \(a\) and \(b\) contains node \(0\), you will get \(r = 1\).
- In case you make more than \(2n-6\) queries or make an invalid query, you will get \(r = -1\). You will need to terminate after this to get the "Wrong answer" verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
When you find out the structure, output a line in the format "! \(p_1 \space p_2 \ldots p_{n-1}\)" (without quotes), where \(p_i\) (\(0 \le p_i < i\)) denotes the index of the direct parent of node \(i\). This query is not counted towards the \(2n - 6\) queries limit.
After solving one test case, the program should immediately move on to the next one. After solving all test cases, the program should be terminated immediately.
After printing any query do not forget to output an end of line and flush the output buffer. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.
The interactor is non-adaptive. The tree does not change during the interaction.
大致题意:一个树状结构,其满足如下条件:
- 除了 \(0\) 节点外,其余节点只有一个子节点。
- \(1\) 节点一定存在子节点。
- 若节点 \(i < j\),则 \(p_i < p_j\),\(p_i\) 表示为 \(i\) 的父节点。
交互操作为:
- \(? \;\;a \;\;b\):表示询问 \(a\) 与 \(b\) 之间是否存在节点 \(0\), \(1\) 表示存在, 0表示不存在。最多交互 \(2n - 6\) 次.
最后依次输出 \(p[1], p[2],...,p[n]\).
大致思路:
- 记数组 \(f[i]\) 为节点 \(i\) 的父节点,本题输出结果便是\(f[1],f[2]...,f[n]\).
- 由树的结构可以得出每一层的节点从左到右依次为:\(1,2,3,4....n\)
- 对于有序询问 "\(?\;a\;b\,(a<b)\)",从 \(a=1,b=2\) 开始,如果返回值为 \(1\),则表示节点 \(a\) 无子节点,\(a=a+1\)。如果返回值为 \(0\),则表示节点 \(a\) 为 \(b\) 的父节点,则 \(f[b] = a\),\(a=a+1\), \(b=b+1\).
- \(a = 1\) 时,"\(?\) \(a\) \(b\) \(= 1\)"时,\(0\) 节点便会多一个子节点,直到 \(?\; a\; \;b =0\).
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e4 + 5;
int n;
void fio() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int f[N];
int main() {
fio();
int t;
cin >> t;
while (t--) {
cin >> n;
memset(f, 0, sizeof f);
int b = 2;
int op ;
n -= 1;
int a = 1;
while(a <= n - 1){
if (b > n)break;
cout << "? " << a << " " << b << endl;
cin >> op;
if (op == 0) {
f[b] = a;
b ++ ;
a ++ ;
}
else if (op == 1 && a == 1)b ++;
else a ++;
}
cout << "! ";
for (int i = 1; i <= n; i++)cout << f[i] << " ";
cout << endl;
}
return 0;
}