SCAU2024ACM队暑期训练_3 题解(部分,有待补充)
一边补题一边写题解~
接下来是第三场暑训
题目 | 来源(含原题链接) | 是否完成题解 | tag |
---|---|---|---|
A | CodeForces 1495A | 已完成 | geometry ; greedy ; math ; sortings ; *1200 |
B | Gym 101239L | Huffman | |
C | 黑暗爆炸 1982 | 已完成 | 枚举 ; 二分 ; 思维 |
D | CodeForces 981C | 已完成 | implementation ; trees ; *1400 |
E | CodeForces 485D | binary search ; sortings ; *2100 | |
F | 黑暗爆炸 3832 | 权值线段树 ; 拓扑排序 | |
G | CodeForces 1480A | 已完成 | games ; greedy ; strings ; *800 |
H | AtCoder abc350_g | ||
I | CodeForces 727C | 已完成 | constructive algorithms ; interactive ; math ; *1400 |
J | CodeForces 1381C | 暂时复制了一份官方题解的翻译 | constructive algorithms ; graph matchings ; greedy ; implementation ; sortings ; two pointers ; *2500 |
K | 黑暗爆炸 1915 | dp | |
L | CodeForces 1516C |
PS:由于Gym题目只能显示PDF,所以我给的是洛谷的链接,如果要在CF Gym上提交的话可以点这里
正文
A CodeForces 1495A
A. Diamond Miner
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Diamond Miner is a game that is similar to Gold Miner, but there are \(n\) miners instead of \(1\) in this game.
The mining area can be described as a plane. The \(n\) miners can be regarded as \(n\) points on the y-axis. There are \(n\) diamond mines in the mining area. We can regard them as \(n\) 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 \((a,b)\) uses his hook to mine a diamond mine at the point \((c,d)\), he will spend \(\sqrt{(a-c)^2+(b-d)^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 \(t\) (\(1\le t\le 10\)) — 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 10^5\)) — the number of miners and mines.
Each of the next \(2n\) lines contains two space-separated integers \(x\) (\(-10^8 \le x \le 10^8\)) and \(y\) (\(-10^8 \le y \le 10^8\)), which represent the point \((x,y)\) to describe a miner's or a diamond mine's position. Either \(x = 0\), meaning there is a miner at the point \((0, y)\), or \(y = 0\), meaning there is a diamond mine at the point \((x, 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 \(n\) and the number of points on the y-axis is equal to \(n\).
It's guaranteed that the sum of \(n\) 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 \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-9}\).
Example
点击查看样例
input
3
2
0 1
1 0
0 -1
-2 0
4
1 0
3 0
-5 0
6 0
0 3
0 1
0 2
0 4
5
3 0
0 4
0 -3
4 0
2 0
1 0
-3 0
0 -10
0 -2
0 -10
output
3.650281539872885
18.061819283610362
32.052255376143336
Note
In the first test case, the miners are at \((0,1)\) and \((0,-1)\), while the diamond mines are at \((1,0)\) and \((-2,0)\). If you arrange the miners to get the diamond mines in the way, shown in the picture, you can get the sum of the energy \(\sqrt2 + \sqrt5\).
我的解法
这题其实只要把所有的坐标转换成正数
然后排序贪心一下求和就可以了
为什么呢?
关于两个矿工和两个钻石矿连线的情况,我们可以转化成这样一个图形
分成两种情况
\(a+b\) 和 \(c+d+e+f\)
由于三角形的性质:两边之和大于第三边
所以:
\(e+d>a\)
\(c+f>b\)
因此
\(c+d+e+f>a+b\)
总结一下,如果矿石和矿工都有大有小
那么只要大对大,小对小,就是最优解了
由于我们要求最小值
因此,只要我们对所有的点分类并取正数,然后对其排序
排序后一一对应求和一下就是答案了
我的代码
点击查看代码
#include <bits/stdc++.h>
#define int long long
int t,n;
const int N = 1e8;
void solve()
{
std::cin >> n;
std::vector<int> work,stone;
for(int i = 0 ; i < n*2 ; i ++)
{
int x,y;
std::cin >> x >> y;
if(x == 0) work.push_back(std::abs(y));
else stone.push_back(std::abs(x));
}
std::sort(work.begin(),work.end());
std::sort(stone.begin(),stone.end());
double ans = 0;
for(int i = 0 ; i < n ; i ++)
{
double d_x = work[i];
double d_y = stone[i];
ans += sqrt(d_x * d_x + d_y * d_y);
}
printf("%.15f\n",ans);
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
//std::cout.tie(nullptr);
std::cin >> t;
while(t--)
{
solve();
}
return 0;
}
B ICPC2015 WF L
[ICPC2015 WF] Weather Report
题面翻译
给定4种天气情况出现的概率,你需要将n天的所有可能的 \(4^{n}\) 种情况已某种方式编码为01串,使得:
- 编码长度的期望最短
- 任何一个编码不是另一个编码的前缀
求期望长度。
题目描述
You have been hired by the Association for Climatological Measurement, a scientific organization interested in tracking global weather trends over a long period of time. Of course, this is no easy task. They have deployed many small devices around the world, designed to take periodic measurements of the local weather conditions. These are cheap devices with somewhat restricted capabilities. Every day they observe which of the four standard kinds of weather occurred: Sunny, Cloudy, Rainy, or Frogs. After every \(n\) of these observations have been made, the results are reported to the main server for analysis. However, the massive number of devices has caused the available communication bandwidth to be overloaded. The Association needs your help to come up with a method of compressing these reports into fewer bits.
For a particular device’s location, you may assume that the weather each day is an independent random event, and you are given the predicted probabilities of the four possible weather types. Each of the \(4^ n\) possible weather reports for a device must be encoded as a unique sequence of bits, such that no sequence is a prefix of any other sequence (an important property, or else the server would not know when each sequence ends). The goal is to use an encoding that minimizes the expected number of transmitted bits.
输入格式
The first line of input contains an integer \(1 \le n \le 20\), the number of observations that go into each report. The second line contains four positive floating-point numbers, \(p_{\text {sunny}}\), \(p_{\text {cloudy}}\), \(p_{\text {rainy}}\), and \(p_{\text {frogs}}\), representing the respective weather probabilities. These probabilities have at most 6 digits after the decimal point and sum to 1.
输出格式
Display the minimum expected number of bits in the encoding of a report, with an absolute or relative error of at most \(10^{-4}\).
样例 #1
样例输入 #1
2
0.9 0.049999 0.05 0.000001
样例输出 #1
1.457510
样例 #2
样例输入 #2
20
0.25 0.25 0.25 0.25
样例输出 #2
40.000000
提示
Time limit: 1000 ms, Memory limit: 1048576 kB.
International Collegiate Programming Contest (ACM-ICPC) World Finals 2015
这题我还不会,可以先去洛谷看题解
C 黑暗爆炸 1982
Description
2021.Moving Pebbles Two players play the followinggame. At the beginning of the game they start with \(n(1<=n<=100000)\) piles of stones. At each step of thegame, the player chooses a pile and remove at leastone stone from this pile and move zero or morestones from this pile to any other pile that still hasstones.A player loses if he has no more possiblemoves. Given the initial piles.
给你 \(N\) 堆石头,两个人玩游戏.每次任选1个非空堆
- 首先拿掉至少 \(1\) 个石头,
- 移动这堆任意个( \(0\) 个也可以)石子到非空堆中。
谁不能移动石子了,谁就输了。
Input
Each line ofinput has integers \(0<n\le 100000\),followed by n positive integers denoting the initialpiles.
Output
For each line of input, output “first player” if first player can force a win, or “second player”, if the second player can force a win.
Sample
点击查看样例
Input
3 2 1 3
Output
first player
我的题解
首先:我认为这题的题面是有问题的!!!
题面说的是不能移动石子就输
实际上这里的“移动”是指第一个操作:拿走石子
这样看题面和数据结果才合理
如果这样理解题目的话,那么:
如果有奇数堆,那么先手必胜
如果有偶数堆,那就分类讨论
- 如果堆中的石子数量能形成对称,只要后手和先手做对应的操作,那么后手必胜
- 如果石子数量不能形成对称,那么先手必胜
我的代码
点击查看代码
#include <bits/stdc++.h>
#define int long long
signed main()
{
int n;
std::cin >> n;
std::vector<int> a;
for(int i = 0 ; i < n ; i ++)
{
int num;
std::cin >> num;
if(num) a.push_back(num);
}
if(a.size()%2) std::cout << "first player";
else
{
int jud = 1;
std::sort(a.begin(),a.end());
for(int i = 0 ; i < n && jud; i += 2)
{
if(a[i] != a[i+1])
{
jud = 0;
}
}
std::cout << (jud ? "second player" : "first player");
}
return 0;
}
D CodeForces 981C
C. Useful Decomposition
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)!
He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help!
The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path.
Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition.
Input
The first line contains a single integer \(n\) (\(2 \leq n \leq 10^{5}\)) the number of nodes in the tree.
Each of the next \(n - 1\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\), \(a_i \neq b_i\)) — the edges of the tree. It is guaranteed that the given edges form a tree.
Output
If there are no decompositions, print the only line containing "No".
Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition \(m\).
Each of the next \(m\) lines should contain two integers \(u_i\), \(v_i\) (\(1 \leq u_i, v_i \leq n\), \(u_i \neq v_i\)) denoting that one of the paths in the decomposition is the simple path between nodes \(u_i\) and \(v_i\).
Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order.
If there are multiple decompositions, print any.
Examples
input 1
4
1 2
2 3
3 4
output 1
Yes
1
1 4
input 2
6
1 2
2 3
3 4
2 5
3 6
output 2
No
input 3
5
1 2
1 3
1 4
1 5
output 3
Yes
4
1 2
1 3
1 4
1 5
Note
The tree from the first example is shown on the picture below:
The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
The tree from the second example is shown on the picture below:
We can show that there are no valid decompositions of this tree.
The tree from the third example is shown on the picture below:
The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions.
我的题解
本题看似是与图相关的题目
实际上只是运用了图的一个形式
本质上还是考察思维
给图中的点分成三类
- 有1个度
- 有2个度
- 有3个或以上的度
我们首先收集所有节点的度并给他们排序
点的序号和其度的数量要一一对应
如果图中只有一条通路的话,那么度最多的点一定只有两个度
这种情况下直接输出度最小的两个点的序号就可以了
如果图中有多条通路的话
那么度最多的点一定有超过两个度
而且第二多度的点的度的数量一定不超过2
(如果超过2说明这个图不符合题目描述的情形)
然后直接输出这个点的序号和所有度的数量为1的点的序号就好了
(这个数量也就是度最多的点的度的数量)
我的代码
#include <bits/stdc++.h>
#define int long long
int n;
const int N = 1e8;
int index = 0;
bool cmp(std::pair<int,int> a,std::pair<int,int> b)
{
if(a.first != b.first) return a.first > b.first;
else return a.second > b.second;
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n;
std::vector<std::pair<int,int> > um(n);
for(int i = 0 ; i < n ; i ++) num[i].second = i+1;
for(int i = 0 ; i < n-1 ; i ++)
{
std::cin >> a >> b;
num[a-1].first ++;
num[b-1].first ++;
}
std::sort(num.begin(),num.end(),cmp);
if(num[1].first > 2)
{
std::cout << "No";
return 0;
}
if(num[0].first <= 2)
{
std::cout << "Yes\n";
std::cout << 1 << "\n";
std::cout << num[n-1].second << " " << num[n-2].second;
return 0;
}
else
{
std::cout << "Yes\n";
std::cout << num[0].first << "\n";
for(int i = 1 ; i <= num[0].first ; i ++)
{
std::cout << num[0].second << " " << num[n-i].second << "\n";
}
return 0;
}
return 0;
}
E CodeForces 485D
D. Maximum Value
time limit per test: 1 second
memory limit per test: 256 megabytes
inputstandard input
outputstandard output
You are given a sequence \(a\) consisting of n integers. Find the maximum possible value of (integer remainder of \(a_i\) divided by \(a_j\)), where \(1 ≤ i, j ≤ n\) and \(a_i ≥ a_j\).
Input
The first line contains integer \(n\) — the length of the sequence \((1 ≤ n ≤ 2·10^5)\).
The second line contains n space-separated integers \(a_i (1 ≤ a_i ≤ 10^6)\).
Output
Print the answer to the problem.
Examples
input
3
3 4 5
output
2
(大佬的题解)[https://blog.csdn.net/qq_41289920/article/details/87735497]
F 黑暗爆炸 3832
题面
Description
An annual bicycle rally will soon begin in Byteburg. The bikers of Byteburg are natural long distance cyclists. Local representatives of motorcyclists, long feuding the cyclists, have decided to sabotage the event.
There are intersections in Byteburg, connected with one way streets. Strangely enough, there are no cycles in the street network - if one can ride from intersection \(U\) to intersection \(V\) , then it is definitely impossible to get from \(V\) to \(U\).
The rally's route will lead through Byteburg's streets. The motorcyclists plan to ride their blazing machines in the early morning of the rally day to one intersection and completely block it. The cyclists' association will then of course determine an alternative route but it could happen that this new route will be relatively short, and the cyclists will thus be unable to exhibit their remarkable endurance. Clearly, this is the motorcyclists' plan - they intend to block such an intersection that the longest route that does not pass through it is as short as possible.
给定一个 \(N\) 个点 \(M\) 条边的有向无环图,每条边长度都是 \(1\) 。
请找到一个点,使得删掉这个点后剩余的图中的最长路径最短。
Input
In the first line of the standard input, there are two integers, \(N\) and \(M(2 \le N \le 500 000,1 \le M \le 1 000 000)\), separated by a single space, that specify the number of intersections and streets in Byteburg. The intersections are numbered from to . The lines that follow describe the street network: in the -th of these lines, there are two integers, \(A_i\), \(B_i(1 \le A_i,B_i \le N,A_i!= B_i)\), separated by a single space, that signify that there is a one way street from the intersection no. \(A_i\) to the one no. \(B_i\).
第一行包含两个正整数 \(N\) , \(M(2 \le N \le 500 000,1 \le M \le 1 000 000)\),表示点数、边数。
接下来M行每行包含两个正整数 \(A[i]\) , \(B[i](1 \le A[i],B[i] \le N,A[i] != B[i])\),表示\(A[i]\)到\(B[i]\)有一条边。
Output
The first and only line of the standard output should contain two integers separated by a single space. The first of these should be the number of the intersection that the motorcyclists should block, and the second - the maximum number of streets that the cyclists can then ride along in their rally. If there are many solutions, your program can choose one of them arbitrarily.
包含一行两个整数 \(x\) , \(y\) ,用一个空格隔开,\(x\) 为要删去的点,\(y\) 为删除 \(x\) 后图中的最长路径的长度,如果有多组解请输出任意一组。
Sample Input
6 5
1 3
1 4
3 6
3 4
4 5
Sample Output
1 2
权值线段树 + 拓扑排序
大佬的题解
G CodeForces 1480A
A. Yet Another String Game
time limit per test: 2 seconds
memory limit per test: 512 megabytes
input: standard input
output: standard output
Homer has two friends Alice and Bob. Both of them are string fans.
One day, Alice and Bob decide to play a game on a string \(s = s_1 s_2 \dots s_n\) of length \(n\) consisting of lowercase English letters. They move in turns alternatively and Alice makes the first move.
In a move, a player must choose an index \(i\) (\(1 \leq i \leq n\)) that has not been chosen before, and change \(s_i\) to any other lowercase English letter \(c\) that \(c \neq s_i\).
When all indices have been chosen, the game ends.
The goal of Alice is to make the final string lexicographically as small as possible, while the goal of Bob is to make the final string lexicographically as large as possible. Both of them are game experts, so they always play games optimally. Homer is not a game expert, so he wonders what the final string will be.
A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds:
- \(a\) is a prefix of \(b\), but \(a \ne b\);
- in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
Input
Each test contains multiple test cases. The first line contains \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Description of the test cases follows.
The only line of each test case contains a single string \(s\) (\(1 \leq |s| \leq 50\)) consisting of lowercase English letters.
Output
For each test case, print the final string in a single line.
Example
input
3
a
bbbb
az
output
b
azaz
by
我的题解
使用贪心的思想
为了使字符串最接近自己的期望
Alice和Bob都对最前面的可操作字符进行操作
(以免字符被对方操作)
当Alice操作时
- 若该字符为a,则把该字符变为b
- 若该字符不为a,则把该字符变为a
当Bob操作时
- 若该字符为z,则把该字符变为y
- 若该字符不为z,则把该字符变为z
遍历操作即可
我的代码
#include <bits/stdc++.h>
#define int long long
int t;
void solve()
{
std::string s;
std::cin >> s;
for(int i = 0 ; i < s.size() ; i ++)
{
if(i%2 == 0) s[i] = (s[i] == 'a' ? 'b' : 'a');
else s[i] = (s[i] == 'z' ? 'y' : 'z');
}
std::cout << s << "\n";
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> t;
while(t--)
{
solve();
}
return 0;
}
H AtCoder abc350_g
G - Mediator
Time Limit: 3 sec / Memory Limit: 256 MB
Problem Statement
Beware the special input format and the smaller memory limit than usual.
There is an undirected graph with vertices \(1, 2, \dots, N\), initially without edges.
You need to process the following \(Q\) queries on this graph:
1 u v
Type \(1\): Add an edge between vertices \(u\) and \(v\).
Before adding the edge, \(u\) and \(v\) belong to different connected components (i.e., the graph always remains a forest).
2 u v
Type \(2\): If there is a vertex adjacent to both \(u\) and \(v\), report its vertex number; otherwise, report \(0\).
Given that the graph always remains a forest, the answer to this query is uniquely determined.
The queries are given in an encrypted form.
The original query is defined by three integers \(A, B, C\), and the encrypted query is given as three integers \(a, b, c\).
Let \(X_k\) be the answer to the \(k\)-th type-\(2\) query. Define \(X_k = 0\) for \(k = 0\).
Restore \(A, B, C\) from the given \(a, b, c\) as follows:
- Let \(l\) be the number of type-\(2\) queries given before this query (not counting the query itself). Then, use the following:
- \(A = 1 + (((a \times (1+X_l)) \mod 998244353) \mod 2)\)
- \(B = 1 + (((b \times (1+X_l)) \mod 998244353) \mod N)\)
- \(C = 1 + (((c \times (1+X_l)) \mod 998244353) \mod N)\)
Constraints
- All input values are integers.
- \(2 \le N \le 10^5\)
- \(1 \le Q \le 10^5\)
- \(1 \le u \lt v \le N\)
- \(0 \le a,b,c \lt 998244353\)
Input
The input is given from Standard Input in the following format:
N Q
Query 1
Query 2
...
Query Q
Output
Let \(k\) be the number of type-\(2\) queries. Print \(k\) lines.
The \(i\)-th line should contain the answer to the \(i\)-th type-\(2\) query.
Sample Input 1
6 12
143209915 123840720 97293110
89822758 207184717 689046893
67757230 270920641 26993265
952858464 221010240 871605818
730183361 147726243 445163345
963432357 295317852 195433903
120904472 106195318 615645575
817920568 27584394 770578609
38727673 250957656 506822697
139174867 566158852 412971999
205467538 606353836 855642999
159292205 319166257 51234344
Sample Output 1
0
2
0
2
6
0
1
After decrypting all queries, the input is as follows:
6 12
2 1 3
1 2 6
1 2 4
1 1 3
2 4 6
2 1 4
1 5 6
1 1 2
2 1 4
2 2 5
2 3 4
2 2 3
This input has a \(6\)-vertex graph and \(12\) queries.
- The first query is
2 1 3
.- No vertex is adjacent to both vertex \(1\) and vertex \(3\), so report \(0\).
- The second query is
1 2 6
.- Add an edge between vertices \(2\) and \(6\).
- The third query is
1 2 4
.- Add an edge between vertices \(2\) and \(4\).
- The fourth query is
1 1 3
.- Add an edge between vertices \(1\) and \(3\).
- The fifth query is
2 4 6
.- The vertex adjacent to both vertices \(4\) and \(6\) is vertex \(2\).
- The sixth query is
2 1 4
.- No vertex is adjacent to both vertices \(1\) and \(4\), so report \(0\).
- The seventh query is
1 5 6
.- Add an edge between vertices \(5\) and \(6\).
- The eighth query is
1 1 2
.- Add an edge between vertices \(1\) and \(2\).
- The ninth query is
2 1 4
.- The vertex adjacent to both vertices \(1\) and \(4\) is vertex \(2\).
- The tenth query is
2 2 5
.- The vertex adjacent to both vertices \(2\) and \(5\) is vertex \(6\).
- The eleventh query is
2 3 4
.- No vertex is adjacent to both vertices \(3\) and \(4\), so report \(0\).
- The twelfth query is
2 2 3
.- The vertex adjacent to both vertices \(2\) and \(3\) is vertex \(1\).
Sample Input 2
2 1
377373366 41280240 33617925
Sample Output 2
The output may be empty.
我们可以参考atcoder的题解
这是该题解的提交方案
I CodeForces - 727C
C. Guess the Array
time limit per test: 1 second
memory limit per test: 256 megabytes
inputstandard input
outputstandard output
This is an interactive problem. You should use flush operation after each printed line. For example, in C++ you should use fflush(stdout)
, in Java you should use System.out.flush()
, and in Pascal — flush(output)
.
In this problem you should guess an array a which is unknown for you. The only information you have initially is the length \(n\) of the array \(a\).
The only allowed action is to ask the sum of two elements by their indices. Formally, you can print two indices \(i\) and \(j\) (the indices should be distinct). Then your program should read the response: the single integer equals to \(a_i + a_j\).
It is easy to prove that it is always possible to guess the array using at most \(n\) requests.
Write a program that will guess the array a by making at most \(n\) requests.
Interaction
In each test your program should guess a single array.
The input starts with a line containing integer \(n (3 ≤ n ≤ 5000)\) — the length of the array. Your program should read it at first.
After that your program should print to the standard output the requests about the sum of two elements or inform that the array is guessed.
In case your program is making a request to ask the sum of two elements, it should print line in the format " \(?\) \(i\) \(j\) " ( \(i\) and \(j\) are distinct integers between \(1\) and \(n\)), where \(i\) and \(j\) are indices in the array \(a\).
In case your program informs that the array is guessed, it should print line in the format " \(!\) \(a_1\) \(a_2\) \(...\) \(a_n\) " (it is guaranteed that all ai are positive integers not exceeding \(10^5\)), where \(a_i\) is the \(i\)-th element of the array \(a\).
The response on a request is a single integer equal to \(a_i\) + \(a_j\), printed on a separate line.
Your program can do at most n requests. Note that the final line «\(!\) \(a_1\) \(a_2\) ... \(a_n\)» is not counted as a request.
Do not forget about flush operation after each printed line.
After you program prints the guessed array, it should terminate normally.
Example
input
5
9
7
9
11
6
output
? 1 5
? 2 3
? 4 1
? 5 2
? 3 4
! 4 6 1 5 5
Note
The format of a test to make a hack is:
The first line contains an integer number \(n (3 ≤ n ≤ 5000)\) — the length of the array.
The second line contains n numbers \(a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^5)\) — the elements of the array to guess.
我的题解
这是一题交互题
题意是:
有一个长度为n的数组
让你猜出这个数组的所有数字
它首先会给你n——这个数组的长度
然后你可以做两个操作,打印“? 数字1 数字2”
代表询问下标为数字1和下标为数字2的两个元素的和是多少
破题的关键点是:
只要知道三个数相互的和,我们就能得到这三个数本身是多少
\(x + y = a\)
\(x + z = b\)
\(y + z = c\)
\(a + b + c = 2 × (x+y+z)\)
得:
\(x = \frac{a+b+c}{2} - c\)
\(y = \frac{a+b+c}{2} - b\)
\(z = \frac{a+b+c}{2} - a\)
比如,只要我们得到了下标为1、2、3的元素的值
那么我们只要一个一个询问下标为1的元素和下标为k的元素的和 \((3 \lt k \le n)\)
再用这个值减去下标为1的元素的值,就能得到下标为k的元素的值了
只要遍历就可以了
最后输出 ' ! ' 后再输出数组内容就可以了
我的代码
#include <bits/stdc++.h>
#define int long long
signed main()
{
int n;
std::cin >> n;
std::vector<int> ans(n+1),ask(n+1);
int a,b,c,x;
std::cout << "? 1 2" << std::endl;
fflush(stdout);
std::cin >> a;
std::cout << "? 1 3" << std::endl;
fflush(stdout);
std::cin >> b;
std::cout << "? 2 3" << std::endl;
fflush(stdout);
std::cin >> c;
ans[1] = (a+b+c)/2 - c;
ans[2] = (a+b+c)/2 - b;
ans[3] = (a+b+c)/2 - a;
for(int i = 4 ; i <= n ; i ++)
{
std::cout << "? 1 " << i << std::endl;
fflush(stdout);
std::cin >> x;
ans[i] = x - ans[1];
}
std::cout << "! ";
for(int i = 1 ; i <= n ; i ++) std::cout << ans[i] << " ";
return 0;
}
J
C. Mastermind
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
In the game of Mastermind, there are two players — Alice and Bob. Alice has a secret code, which Bob tries to guess. Here, a code is defined as a sequence of \(n\) colors. There are exactly \(n+1\) colors in the entire universe, numbered from \(1\) to \(n+1\) inclusive.
When Bob guesses a code, Alice tells him some information about how good of a guess it is, in the form of two integers \(x\) and \(y\).
The first integer \(x\) is the number of indices where Bob's guess correctly matches Alice's code. The second integer \(y\) is the size of the intersection of the two codes as multisets. That is, if Bob were to change the order of the colors in his guess, \(y\) is the maximum number of indices he could get correct.
For example, suppose \(n=5\), Alice's code is \([3,1,6,1,2]\), and Bob's guess is \([3,1,1,2,5]\). At indices \(1\) and \(2\) colors are equal, while in the other indices they are not equal. So \(x=2\). And the two codes have the four colors \(1,1,2,3\) in common, so \(y=4\).
Solid lines denote a matched color for the same index. Dashed lines denote a matched color at a different index. \(x\) is the number of solid lines, and \(y\) is the total number of lines.
You are given Bob's guess and two values \(x\) and \(y\). Can you find one possibility of Alice's code so that the values of \(x\) and \(y\) are correct?
Input
The first line contains a single integer \(t\) (\(1\le t\le 1000\)) — the number of test cases. Next \(2t\) lines contain descriptions of test cases.
The first line of each test case contains three integers \(n,x,y\) (\(1\le n\le 10^5, 0\le x\le y\le n\)) — the length of the codes, and two values Alice responds with.
The second line of each test case contains \(n\) integers \(b_1,\ldots,b_n\) (\(1\le b_i\le n+1\)) — Bob's guess, where \(b_i\) is the \(i\)-th color of the guess.
It is guaranteed that the sum of \(n\) across all test cases does not exceed \(10^5\).
Output
For each test case, on the first line, output "YES" if there is a solution, or "NO" if there is no possible secret code consistent with the described situation. You can print each character in any case (upper or lower).
If the answer is "YES", on the next line output \(n\) integers \(a_1,\ldots,a_n\) (\(1\le a_i\le n+1\)) — Alice's secret code, where \(a_i\) is the \(i\)-th color of the code.
If there are multiple solutions, output any.
Example
input
7
5 2 4
3 1 1 2 5
5 3 4
1 1 2 1 2
4 0 4
5 5 3 3
4 1 4
2 3 2 3
6 1 2
3 2 1 1 1 1
6 2 4
3 3 2 1 1 1
6 2 6
1 1 3 2 1 1
output
YES
3 1 6 1 2
YES
3 1 1 1 2
YES
3 3 5 5
NO
YES
4 4 4 4 3 1
YES
3 1 3 1 7 7
YES
2 3 1 1 1 1
Note
The first test case is described in the statement.
In the second test case, \(x=3\) because the colors are equal at indices \(2,4,5\). And \(y=4\) because they share the colors \(1,1,1,2\).
In the third test case, \(x=0\) because there is no index where the colors are the same. But \(y=4\) because they share the colors \(3,3,5,5\).
In the fourth test case, it can be proved that no solution exists.
官方题解:可以移步原链接中的1382E
假设我们已经确定了哪些 \(x\) 索引的颜色是一致的。我们应该对剩余的 \(n-x\) 索引进行洗牌,以尽量减少匹配的数量。我们还应该将 \(n-y\) 个索引替换为不影响多集交集的颜色。因为有 \(n+1 \gt n\) 种颜色,所以有 \(c\) 种颜色不会出现在 \(a\) 中,我们可以用它来填充这些 \(n-y\) 个索引。假设 \(n-y\) 大于或等于我们不得不进行的最小过量匹配数,那么我们就有了解决方案。
假设 \(f\) 是 \(n-x\) 索引中出现频率最高的颜色的个数。那么被迫匹配的次数显然至少为 \(2f-(n-x)\) 。具体方法如下。让我们重新编排索引,这样就有了相同颜色的连续图块。然后将所有图块旋转 \(\left\lfloor \frac{n-x}{2}\right\rfloor\) 个索引。
现在我们需要确定能使 \(f\) 最小化的 \(x\) 索引。要做到这一点,只需始终选择剩余颜色中最常见的一种即可。我们可以通过 \(O(n\log n)\) 中的优先级队列,或者 \(O(n)\) 中的计数排序来实现。
为了更清楚地说明解决方案,让我们看看样本中的第六个测试用例是如何工作的: \([3,3,2,1,1,1]\) 与 \(x=2,y=4\) 。首先,我们贪婪地选择最频繁出现的颜色两次: \([3,\_,\_,1,\_,\_]\) .第一次选择 \(1\) 后, \(1\) 和 \(3\) 之间出现了平局。因此,我们也可以两次选择 \(1\) 。
那么剩下的索引就有颜色 \(3,2,1,1\) 。旋转 \((n-x)/2=2\) 个索引,我们可以得到 \(1,1,2,3\) 个颜色,从而得到 \([3,1,1,1,2,3]\) 个颜色。 \(4\) 不会出现在 \(a\) 中,因此我们应该用 \(4\) 填充 \(n-y=2\) 索引。(例如,我们得到的解是 \([3,4,1,1,4,3]\) 。
K
题面
Description
奶牛们正在回味童年,玩一个类似跳格子的游戏,在这个游戏里,奶牛们在草地上画了一行 \(N\) 个格子,\((3 \le N \le 250,000)\),编号为\(1..N\)。就像任何一个好游戏一样,这样的跳格子游戏也有奖励!第i个格子标有一个数字\(V_i(-2,000,000,000 \le V_i \le 2,000,000,000)\)表示这个格子的钱。奶牛们想看看最后谁能得到最多的钱。规则很简单:
- 每个奶牛从 \(0\) 号格子出发。( \(0\) 号格子在 \(1\) 号之前,那里没钱)
- 她向 \(N\) 号格子进行一系列的跳跃(也可以不跳),每次她跳到的格子最多可以和前一个落脚的格子差 \(K\) 格\((1 \le K\le N)\)(比方说,当前在 \(1\) 号格,\(K=2\), 可以跳到2号和3号格子)
- 在任何时候,她都可以选择回头往 \(0\) 号格子跳,直到跳到 \(0\) 号格子。
另外,除了以上规则之外,回头跳的时候还有两条规则:
- 不可以跳到之前停留的格子。
- 除了 \(0\) 号格子之外,她在回来的时候,停留的格子必须是恰巧过去的时候停留的某个格子的前一格(当然,也可以跳过某些过去时候停留的格子)。简单点说,如果i号格子是回来 停留的格子,\(i+1\) 号就必须是过去停留的格子,如果 \(i+1\) 号格子是过去停留的格子,\(i\) 号格子不一定要是回来停留的格子。(如果这里不太清楚的可以去看英文原文)她得到的钱就是所有停留过的格子中的数字的和,请你求出最多奶牛可以得到的钱数。
在样例中,\(K=2\),一行 \(5\) 个格子。
一个合法的序列Bessie可以选择的是 \(0[0], 1[0], 3[2], 2[1], 0[0]\)。(括号里的数表示钱数) 这样,可以得到的钱数为\(0+0+2+1+0 = 3\)。 如果Bessie选择一个序列开头为 \(0, 1, 2, 3, ...\),那么她就没办法跳回去了,因为她没办法再跳到一个之前没跳过的格子。序列 \(0[0], 2[1], 4[-3], 5[4], 3[2], 1[0], 0[0]\) 是最大化钱数的序列之一,最后的钱数为 \((0+1-3+4+2+0 = 4)\)。
Input
- 第 \(1\) 行 : 两个用空格隔开的整数: \(N\) 和 \(K\)
- 第 \(2\) 到 \(N+1\) 行: 第 \(i+1\) 行有一个整数: \(V_i\)
Output
- 第一行: 一个单个的整数表示最大的钱数是多少。
Sample Input
5 2
0
1
2
-3
4
Sample Output
4
OUTPUT DETAILS:
还有一种可能的最大化钱数的序列是: \(0\) \(2\) \(4\) \(5\) \(3\) \(1\) \(0\)
解法:单调队列优化dp
L
C. Baby Ehab Partitions Again
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
Baby Ehab was toying around with arrays. He has an array \(a\) of length \(n\). He defines an array to be good if there's no way to partition it into \(2\) subsequences such that the sum of the elements in the first is equal to the sum of the elements in the second. Now he wants to remove the minimum number of elements in \(a\) so that it becomes a good array. Can you help him?
A sequence \(b\) is a subsequence of an array \(a\) if \(b\) can be obtained from \(a\) by deleting some (possibly zero or all) elements. A partitioning of an array is a way to divide it into \(2\) subsequences such that every element belongs to exactly one subsequence, so you must use all the elements, and you can't share any elements.
Input
The first line contains an integer \(n\) (\(2 \le n \le 100\)) — the length of the array \(a\).
The second line contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_{n}\) (\(1 \le a_i \le 2000\)) — the elements of the array \(a\).
Output
The first line should contain the minimum number of elements you need to remove.
The second line should contain the indices of the elements you're removing, separated by spaces.
We can show that an answer always exists. If there are multiple solutions, you can print any.
Examples
input
4
6 3 9 12
output
1
2
input
2
1 2
output
0
Note
In the first example, you can partition the array into \([6,9]\) and \([3,12]\), so you must remove at least \(1\) element. Removing \(3\) is sufficient.
In the second example, the array is already good, so you don't need to remove any elements.
我只能说,真就1700的签到题
大佬的题解
有什么出现纰漏的地方还请大家在评论区指出!谢谢!
posted on 2024-07-14 16:41 Jiejiejiang 阅读(4) 评论(0) 编辑 收藏 举报