A - Three Piles of Candies
Alice and Bob have received three big piles of candies as a gift. Now they want to divide these candies as fair as possible. To do this, Alice takes one pile of candies, then Bob takes one of the other two piles. The last pile is split between Alice and Bob as they want: for example, it is possible that Alice takes the whole pile, and Bob gets nothing from it.
After taking the candies from the piles, if Alice has more candies than Bob, she discards some candies so that the number of candies she has is equal to the number of candies Bob has. Of course, Bob does the same if he has more candies.
Alice and Bob want to have as many candies as possible, and they plan the process of dividing candies accordingly. Please calculate the maximum number of candies Alice can have after this division process (of course, Bob will have the same number of candies).You have to answer q independent queries.
Let’s see the following example: [1,3,4]
. Then Alice can choose the third pile, Bob can take the second pile, and then the only candy from the first pile goes to Bob — then Alice has 4 candies, and Bob has 4 candies.
Another example is [1,10,100]
. Then Alice can choose the second pile, Bob can choose the first pile, and candies from the third pile can be divided in such a way that Bob takes 54 candies, and Alice takes 46 candies. Now Bob has 55 candies, and Alice has 56 candies, so she has to discard one candy — and after that, she has 55 candies too.
Input
The first line of the input contains one integer q(1≤q≤1000) — the number of queries. Then q queries follow.
The only line of the query contains three integers a,b
and c (1≤a,b,c≤1016 ) — the number of candies in the first, second and third piles correspondingly.
Output
Print q lines. The i-th line should contain the answer for the i -th query — the maximum number of candies Alice can have after the division, if both Alice and Bob act optimally (of course, Bob will have the same number of candies).
Example
Input
4
1 3 4
1 10 100
10000000000000000 10000000000000000 10000000000000000
23 34 45
Output
4
55
15000000000000000
51
篇幅很长,而且题意也很是含蓄
说堆有三堆糖果,Alice先拿一堆,然后Bob再拿另外一堆,剩下那一堆Alice拿一些 Bob拿一些,然后要让他们的糖果数量一样,其实就是让求平均数
#include
using namespace std;
typedef long long ll;
int main()
{
int n;
cin>>n;
while(n--)
{
ll a,b,c;
cin>>a>>b>>c;
cout<<(a+b+c)/2<<endl;
}
return 0;
}
B - Odd Sum Segments
You are given an array a consisting of n integers a1,a2,…,an. You want to split it into exactly k non-empty non-intersecting subsegments such that each subsegment has odd sum (i. e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the n elements of the array a must belong to exactly one of the k
subsegments.
Let’s see some examples of dividing the array of length 5 into 3 subsegments (not necessarily with odd sums): [1,2,3,4,5] is the initial array, then all possible ways to divide it into 3 non-empty non-intersecting subsegments are described below:
[1],[2],[3,4,5]
;
[1],[2,3],[4,5]
;
[1],[2,3,4],[5]
;
[1,2],[3],[4,5]
;
[1,2],[3,4],[5]
;
[1,2,3],[4],[5]
Of course, it can be impossible to divide the initial array into exactly k subsegments in such a way that each of them will have odd sum of elements. In this case print “NO”. Otherwise, print “YES” and any possible division of the array. See the output format for the detailed explanation.You have to answer q
independent queries.
Input
The first line contains one integer q(1≤q≤2⋅105) — the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1≤k≤n≤2⋅105) — the number of elements in the array and the number of subsegments, respectively.
The second line of the query contains n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of a
.It is guaranteed that the sum of n over all queries does not exceed 2⋅105 (∑n≤2⋅105 ).
Output
For each query, print the answer to it. If it is impossible to divide the initial array into exactly k
subsegments in such a way that each of them will have odd sum of elements, print “NO” in the first line. Otherwise, print “YES” in the first line and any possible division of the array in the second line. The division can be represented as k integers r1, r2, …, rk such that 1≤r1Example
Input
3
5 3
7 18 3 14 1
5 4
1 2 3 4 5
6 2
1 2 8 4 10 2
Output
YES
1 3 5
NO
NO
题意大致为给出一个数组然后给出一个数字k,看是不是能把这个数组分为K个连续的区间,并且每个区间的和都是奇数,并且输出下标,而且最后一个一定是n;
可以遍历这个数组如果是奇数就记录下来,如果满足count%2==K%2&&count>=K就可以输出yes
如果数组里奇数的个数和要求的k是同寄偶的并且要大于等于K就说明可以
因为偶数+偶数还是偶数,奇数+偶数=奇数,所以要看数组中奇数的个数和K是不是同奇偶的,当然要大于等于K
#include
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
ll n;
cin>>n;
while(n--)
{
ll aa[234567];ll bb[234567];
ll a,b;ll count=0;
cin>>a>>b;
for(ll i=1;i<=a;i++)
{
cin>>bb[i];
if(bb[i]%2) aa[count++]=i;
}
if(count%2==b%2&&count>=b)
{
cout<<"YES"<<endl;
for(ll i=0;i<b-1;i++) cout<<aa[i]<<" ";
cout<<a<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}
C - Robot Breakout
n robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!
Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the i-th robot is currently located at the point having coordinates (xi, yi). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers X and Y, and when each robot receives this command, it starts moving towards the point having coordinates (X, Y). The robot stops its movement in two cases: either it reaches (X, Y);or it cannot get any closer to (X, Y).Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let’s denote the current coordinates of the robot as (xc, yc). Then the movement system allows it to move to any of the four adjacent points: the first action allows it to move from (xc, yc) to (xc−1, yc);the second action allows it to move from (xc, yc) to (xc, yc+1);the third action allows it to move from (xc, yc) to (xc+1, yc);the fourth action allows it to move from (xc, yc) to (xc, yc−1).
Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers X
and Y so that each robot can reach the point (X, Y ). Is it possible to find such a point?
Input
The first line contains one integer q(1≤q≤105) — the number of queries.Then q
queries follow. Each query begins with one line containing one integer n (1≤n≤105) — the number of robots in the query. Then n lines follow, the i-th of these lines describes the i-th robot in the current query: it contains six integer numbers xi, yi, fi,1, fi,2, fi,3 and fi,4 (−105≤xi,yi≤105, 0≤fi,j≤1). The first two numbers describe the initial location of the i-th robot, and the following four numbers describe which actions the i-th robot can use to move (fi,j=1 if the i-th robot can use the j-th action, and fi,j=0 if it cannot use the j-the action).
It is guaranteed that the total number of robots over all queries does not exceed 105
Output
You should answer each query independently, in the order these queries appear in the input.To answer a query, you should do one of the following:
if it is impossible to find a point that is reachable by all n robots, print one number 0 on a separate line;
if it is possible to find a point that is reachable by all n robots, print three space-separated integers on the same line: 1 X Y, where X and Y are the coordinates of the point reachable by all n robots. Both X and Y should not exceed 105 by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding 105 by absolute value.
Example
Input
4
2
-1 -2 0 0 0 0
-1 -2 0 0 0 0
3
1 5 1 1 1 1
2 5 0 1 0 1
3 5 1 0 0 0
2
1337 1337 0 1 1 1
1336 1337 1 1 0 1
1
3 5 1 1 1 1
Output
1 -1 -2
1 2 5
0
1 -100000 -100000
题意为N个机器人在不同的方格里面,他们只能向上下左右这四个方向行走,然后给出f1,f2,f3,f4的值分别表示这个机器人能不能向上下左右走,别人的博客才知道它只能向上下左右走一步,然后就是输入每一个机器人然后处理,
如果他不能向X轴负方向走,就更新能到达的X轴负方向的最大值,
如果他不能向X轴正方向走,就更新能到达的X轴正方向的最小值,
如果他不能向Y轴负方向走,就更新能到达的Y轴负方向的最大值,
如果他不能向Y轴正方向走,就更新能到达的Y轴正方向的最小值,
然后判断一下是不是符合逻辑,如果可以就输出maxx和maxy
#include
#include
using namespace std;
int main()
{
int n;
cin>>n;
while(n--)
{
int num;cin>>num;
int x[234];int y[234];
int ss[123][2345];int xx,yy;
int minx=-1e5;int maxx=1e5;int miny=-1e5;int maxy=1e5;
for(int i=0;i<num;i++)
{
cin>>x[i]>>y[i]>>ss[0][i]>>ss[1][i]>>ss[2][i]>>ss[3][i];
if(ss[0][i]==0) minx=max(x[i],minx);
if(ss[1][i]==0) maxy=min(y[i],maxy);
if(ss[2][i]==0) maxx=min(x[i],maxx);
if(ss[3][i]==0) miny=max(y[i],miny);
}
if(minx>maxx||miny>maxy) cout<<"0"<<endl;
else cout<<"1 "<<maxx<<" "<<maxy<<endl;
}
return 0;
}
D - RGB Substring (easy version)
The only difference between easy and hard versions is the size of the input.You are given a string s
consisting of n characters, each character is ‘R’, ‘G’ or ‘B’. You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string “RGBRGBRGB …”.
A string a is a substring of string b if there exists a positive integer i such that a1=bi, a2=bi+1, a3=bi+2, …, a|a|=bi+|a|−1. For example, strings “GBRG”, “B”, “BR” are substrings of the infinite string “RGBRGBRGB …” while “GR”, “RGR” and “GGG” are not.You have to answer q independent queries.
Input
The first line of the input contains one integer q(1≤q≤2000) — the number of queries. Then q queries follow.The first line of the query contains two integers n and k (1≤k≤n≤2000) — the length of the string s
and the length of the substring.The second line of the query contains a string s consisting of n characters ‘R’, ‘G’ and ‘B’.It is guaranteed that the sum of n over all queries does not exceed 2000 (∑n≤2000 ).
Output
For each query print one integer — the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string “RGBRGBRGB …”.
Example
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
Output
1
0
3
Note
In the first example, you can change the first character to ‘R’ and obtain the substring “RG”, or change the second character to ‘R’ and obtain “BR”, or change the third, fourth or fifth character to ‘B’ and obtain “GB”.
In the second example, the substring is “BRG”.
RGB: red,green,blue
输入一个N和K,N是字符串的长度K是区间长度
输入一个字符串看它的长度为K的字串是RGBRGB或者GBRGBR或者BRGBRG的字串最少需要改变几个字符
刚开始的思路,弄一个dp数组,如果字符串的这一位是R就看前一位是不是B,如果是dp[i]就等于dp[i-1]
如果不是就加一,这一位如果是B或者G的方法一样,后来test2就错了还傻傻的以为思路没错,这个思路错到底了,因为只弄了一个dp数组,并不能兼顾所有情况,而且最后算最小值时也搞错了
说一下正解:开一个二维数组,搞一个循环,把RGB的三种情况都兼顾,求前缀和,最后在以K进行尺取
#include
#include
using namespace std;
int main()
{
int q;int n,m;
string ss;
string t="RGB";
cin>>q;
while(q--)
{
int aa[3][12345];
int minn=9999999999;
cin>>n>>m>>ss;
for(int i=0;i<=2;i++)
{
for(int j=0;j<n;j++)
{
if(ss[j]!=t[(i+j)%3]) aa[i][j+1]=aa[i][j]+1;
else aa[i][j+1]=aa[i][j];
}
}
for(int i=0;i<=2;i++)
{
for(int j=0;j+m<=n;j++)
minn=min(minn,aa[i][j+m]-aa[i][j]);
}
cout<<minn<<endl;
}
return 0;
}
E - RGB Substring (hard version)
The only difference between easy and hard versions is the size of the input.You are given a string s
consisting of n characters, each character is ‘R’, ‘G’ or ‘B’.You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string “RGBRGBRGB …”.A string a is a substring of string b if there exists a positive integer i such that a1=bi, a2=bi+1, a3=bi+2, …, a|a|=bi+|a|−1. For example, strings “GBRG”, “B”, “BR” are substrings of the infinite string “RGBRGBRGB …” while “GR”, “RGR” and “GGG” are not.You have to answer q independent queries.
Input
The first line of the input contains one integer q(1≤q≤2⋅105) — the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1≤k≤n≤2⋅105) — the length of the string s and the length of the substring.The second line of the query contains a string s consisting of n characters ‘R’, ‘G’ and ‘B’.It is guaranteed that the sum of n over all queries does not exceed 2⋅105 (∑n≤2⋅105 ).
Output
For each query print one integer — the minimum number of characters you need to change in the initial string s
so that after changing there will be a substring of length k in s that is also a substring of the infinite string “RGBRGBRGB …”.
Example
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
Output
1
0
3
Note
In the first example, you can change the first character to ‘R’ and obtain the substring “RG”, or change the second character to ‘R’ and obtain “BR”, or change the third, fourth or fifth character to ‘B’ and obtain “GB”.
In the second example, the substring is “BRG”.
题意和上一题差不太多
#include
#include
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int q;int n,m;
string ss;
string t="RGB";
cin>>q;
while(q--)
{
int aa[234567];
int minn=9999999999;
cin>>n>>m>>ss;
for(int i=0;i<=2;i++)
{
for(int j=0;j<n;j++)
{
if(ss[j]!=t[(i+j)%3]) aa[j+1]=aa[j]+1;
else aa[j+1]=aa[j];
}
for(int j=0;j+m<=n;j++)
minn=min(minn,aa[j+m]-aa[j]);
}
cout<<minn<<endl;
}
return 0;
}
F - Connected Component on a Chessboard
You are given two integers b and w. You have a chessboard of size 109×109 with the top left cell at (1;1), the cell (1;1)is painted white.Your task is to find a connected component on this chessboard that contains exactly b black cells and exactly w white cells. Two cells are called connected if they share a side (i.e. for the cell (x,y) there are at most four connected cells: (x−1,y),(x+1,y),(x,y−1),(x,y+1)). A set of cells is called a connected component if for every pair of cells C1 and C2 from this set, there exists a sequence of cells c1, c2, …, ck such that c1=C1, ck=C2, all ci from 1 to k are belong to this set of cells and for ever i∈[1,k−1], cells ci and ci+1 are connected.Obviously, it can be impossible to find such component. In this case print “NO”. Otherwise, print “YES” and any suitable connected component.You have to answer q
independent queries.
Input
The first line of the input contains one integer q(1≤q≤105) — the number of queries. Then q queries follow.The only line of the query contains two integers b and w (1≤b,w≤105) — the number of black cells required and the number of white cells required.
It is guaranteed that the sum of numbers of cells does not exceed 2⋅105(∑w+∑b≤2⋅105 ).
Output
For each query, print the answer to it.If it is impossible to find the required component, print “NO” on the first line.Otherwise, print “YES” on the first line. In the next b+w lines print coordinates of cells of your component in any order. There should be exactly b black cells and w white cells in your answer. The printed component should be connected.If there are several answers, you can print any. All coordinates in the answer should be in the range [1;109]
Example
Input
3
1 1
1 4
2 5
Output
YES
2 2
1 2
YES
2 3
1 3
3 3
2 2
2 4
YES
2 3
2 4
2 5
1 3
1 5
3 3
3 5
https://blog.csdn.net/u011815404/article/details/97284690
这位大佬太强了
#include
#include
using namespace std;
int main()
{
int n,b,w;
cin>>n;
while(n--)
{
cin>>b>>w;
if(min(b,w)*3+1<max(b,w)) cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl;
if(b==w)
{
for(int i=2;i<=2*b+1;i++) cout<<"2 "<<i<<endl;
}
else if(b>w)
{
int sub=b-w;
if(b==3*w+1)
{
sub--;
cout<<"2 1"<<endl;
}
for(int i=2;i<=2*w+1;i++) cout<<"2 "<<i<<endl;
int i=1;
while(sub>1)
{
int temp=2*i;
cout<<"1 "<<temp<<endl<<"3 "<<temp<<endl;
sub-=2;i++;
}
if(sub==1) cout<<"1 "<<2*i<<endl;
}
else if(b<w)
{
int sub=w-b;
if(w==b*3+1)
{
sub--;
cout<<"3 1"<<endl;
}
for(int i=2;i<=2*b+1;i++) cout<<"3 "<<i<<endl;
int i=1;
while(sub>1)
{
int temp=2*i;
cout<<"2 "<<temp<<endl<<"4 "<<temp<<endl;
sub-=2;
i++;
}
if(sub==1) cout<<"2 "<<2*i<<endl;
}
}
}
return 0;
}
G - K-th Path
You are given a connected undirected weighted graph consisting of n vertices and m edges.
You need to print the k-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from i to j and from j to i are counted as one).More formally, if d is the matrix of shortest paths, where di,j is the length of the shortest path between vertices i and j (1≤iInput
The first line of the input contains three integers n,m and k (2≤n≤2⋅105, n−1≤m≤min(n(n−1)2,2⋅105), 1≤k≤min(n(n−1)2,400) — the number of vertices in the graph, the number of edges in the graph and the value of k, correspondingly.Then m lines follow, each containing three integers x, y and w (1≤x,y≤n, 1≤w≤109, x≠y) denoting an edge betweenvertices x and y of weight w It is guaranteed that the given graph is connected (there is a path between any pair of vertices), there are no self-loops (edges connecting the vertex with itself) and multiple edges (for each pair of vertices x and y , there is at most one edge between this pair of vertices in the graph).
Output
Print one integer — the length of the k
-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from i to j and from j to i are counted as one).
Examples
Input
6 10 5
2 5 1
5 3 9
6 2 2
1 3 1
5 1 8
6 5 10
1 6 5
6 4 6
3 6 2
3 4 5
Output
3
Input
7 15 18
2 6 3
5 7 4
6 5 4
3 6 9
6 7 7
1 6 4
7 1 6
7 2 1
4 3 2
3 2 8
5 3 6
2 5 5
3 7 9
4 1 8
2 1 1
Output
9
图论太差,待补;