Codeforces Round 983 (div 2) A-D
A. Circuit
Alice has just crafted a circuit with
- 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
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
The first line of each test case contains a single integer
The second line of each test case contains
Output
For each test case, output two integers — the minimum and maximum number of lights, respectively, that can be turned on.
大致思路: 由题意知开关状态为
①若
②若
代码如下:
#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
Your task is to choose an odd positive integer
- Each element of the array
belongs to exactly one subarray. - For all
, is odd, i.e., the length of each subarray is odd. , i.e., the median of the array of medians of all subarrays must equal . denotes the median of the array .
Input
Each test consists of multiple test cases. The first line contains a single integer
The first line of each test case contains two integers
It is guaranteed that the sum of
Output
For each test case:
- If there is no suitable partition, output
in a single line. - Otherwise, in the first line, output an odd integer
( ), and in the second line, output distinct integers ( ) — denoting the left borders of each subarray.
In detail, for a valid answer
.
If there are multiple solutions, you can output any of them.
大致思路:易知,我们可以只用三个子数列便可以得到最终的答案,最简单的答案便是
若
若
代码如下:
#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
You can perform the following operation any number (possibly
- Choose two integers
and , where , and assign .
Find the minimum number of operations required to make the array
- For every pairwise distinct triplet of indices
( , , , ), there exists a non-degenerate triangle with side lengths , and , i.e. , and .
Input
Each test consists of multiple test cases. The first line contains a single integer
The first line of each test case contains a single integer
The second line of each test case contains
It is guaranteed that the sum of
Output
For each test case, output a single integer — the minimum number of operations required.
大致思路:由题意,本题仅与元素的大小有关,而与元素的顺序无关。故先对
代码如下:
# 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
Gretel's objective is to learn the exact structure of the monster's nerve system — more specifically, she wants to know the values
She doesn't know exactly how the nodes are placed, but she knows a few convenient facts:
- If we remove root node
and all adjacent edges, this tree will turn into a forest consisting of only paths . Each node that was initially adjacent to the node will be the end of some path. - The nodes are indexed in a way that if
, then . - Node
has exactly two adjacent nodes (including the node ).
![]() |
![]() |
![]() |
---|---|---|
The tree in this picture does not satisfy the condition, because if we remove node |
The tree in this picture does not satisfy the condition, because |
The tree in this picture does not satisfy the condition, because node |
Gretel can make queries to the containment cell:
- "? a b" (
, ) — the cell will check if the simple path between nodes and contains the node .
However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most
Input
Each test consists of multiple test cases. The first line contains a single integer
The first line of each test case contains a single integer
It is guaranteed that the sum of
Interaction
For each test case, interaction starts by reading the integer
Then you can make queries of the following type:
- "? a b" (without quotes) (
, ).
After the query, read an integer
- If the simple path between nodes
and does not contain node , you will get . - If the simple path between nodes
and contains node , you will get . - In case you make more than
queries or make an invalid query, you will get . 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 "!
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表示不存在。最多交互 次.
最后依次输出
大致思路:
- 记数组
为节点 的父节点,本题输出结果便是 . - 由树的结构可以得出每一层的节点从左到右依次为:
- 对于有序询问 "
",从 开始,如果返回值为 ,则表示节点 无子节点, 。如果返回值为 ,则表示节点 为 的父节点,则 , , . 时," "时, 节点便会多一个子节点,直到 .
代码如下:
#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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架