# Codeforces Round #575 (Div. 3)

## A. Three Piles of Candies

time limit per test1 second memory limit per test256 megabytes

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][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 44 candies, and Bob has 44 candies.

Another example is [1,10,100][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 5454 candies, and Alice takes 4646 candies. Now Bob has 5555 candies, and Alice has 5656 candies, so she has to discard one candy — and after that, she has 5555 candies too.

Input

The first line of the input contains one integer 𝑞q (1≤𝑞≤10001≤q≤1000) — the number of queries. Then 𝑞q queries follow.

The only line of the query contains three integers 𝑎,𝑏a,b and 𝑐c (1≤𝑎,𝑏,𝑐≤10161≤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


#### 签到

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e5+100;
void solve(){
ll a,b,c;scanf("%lld%lld%lld",&a,&b,&c);
printf("%lld\n",(a+b+c)>>1);
}
int main(){
int T;cin>>T;
while(T--) solve();
}


## B. Odd Sum Segments

time limit per test3 seconds memory limit per test256 megabytes

You are given an array 𝑎a consisting of 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛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 55 into 33 subsegments (not necessarily with odd sums): [1,2,3,4,5][1,2,3,4,5] is the initial array, then all possible ways to divide it into 33 non-empty non-intersecting subsegments are described below:

• ,,[3,4,5],,[3,4,5];
• ,[2,3],[4,5],[2,3],[4,5];
• ,[2,3,4],,[2,3,4],;
• [1,2],,[4,5][1,2],,[4,5];
• [1,2],[3,4],[1,2],[3,4],;
• [1,2,3],,[1,2,3],,.

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≤𝑞≤2⋅1051≤q≤2⋅105) — the number of queries. Then 𝑞q queries follow.

The first line of the query contains two integers 𝑛n and 𝑘k (1≤𝑘≤𝑛≤2⋅1051≤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 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1091≤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⋅1052⋅105 (∑𝑛≤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 𝑟1r1, 𝑟2r2, …, 𝑟𝑘rk such that 1≤𝑟1<𝑟2<⋯<𝑟𝑘=𝑛1≤r1<r2<⋯<rk=n, where 𝑟𝑗rj is the right border of the 𝑗j-th segment (the index of the last element that belongs to the 𝑗j-th segment), so the array is divided into subsegments [1;𝑟1],[𝑟1+1;𝑟2],[𝑟2+1,𝑟3],…,[𝑟𝑘−1+1,𝑛][1;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n]. Note that 𝑟𝑘rk is always 𝑛n but you should print it anyway.

Exampleinput

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

#### 前缀和随便判一下就好了，签到

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
int n,k,ans[maxn];
ll a[maxn];
void solve(){
scanf("%d%d",&n,&k);
rep(i,1,n) scanf("%lld",&a[i]),a[i]+=a[i-1];
int cnt=0,pre=0;
rep(i,1,n) if((a[i]-a[pre])%2) pre=ans[++cnt]=i;
if(cnt<k||(cnt-k)%2) {puts("NO");return;}
puts("YES");
rep(i,1,k-1) printf("%d ",ans[i]);
printf("%d\n",n);
}
int main(){
int T;cin>>T;
while(T--) solve();
}


## C. Robot Breakout

time limit per test3 seconds memory limit per test256 megabytes

𝑛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:

1. the first action allows it to move from (𝑥𝑐xc, 𝑦𝑐yc) to (𝑥𝑐−1xc−1, 𝑦𝑐yc);
2. the second action allows it to move from (𝑥𝑐xc, 𝑦𝑐yc) to (𝑥𝑐xc, 𝑦𝑐+1yc+1);
3. the third action allows it to move from (𝑥𝑐xc, 𝑦𝑐yc) to (𝑥𝑐+1xc+1, 𝑦𝑐yc);
4. the fourth action allows it to move from (𝑥𝑐xc, 𝑦𝑐yc) to (𝑥𝑐xc, 𝑦𝑐−1yc−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≤𝑞≤1051≤q≤105) — the number of queries.

Then 𝑞q queries follow. Each query begins with one line containing one integer 𝑛n (1≤𝑛≤1051≤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, 𝑓𝑖,1fi,1, 𝑓𝑖,2fi,2, 𝑓𝑖,3fi,3 and 𝑓𝑖,4fi,4 (−105≤𝑥𝑖,𝑦𝑖≤105−105≤xi,yi≤105, 0≤𝑓𝑖,𝑗≤10≤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 (𝑓𝑖,𝑗=1fi,j=1 if the 𝑖i-th robot can use the 𝑗j-th action, and 𝑓𝑖,𝑗=0fi,j=0 if it cannot use the 𝑗j-th action).

It is guaranteed that the total number of robots over all queries does not exceed 105105.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 00 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: 11 𝑋X 𝑌Y, where 𝑋Xand 𝑌Y are the coordinates of the point reachable by all 𝑛n robots. Both 𝑋X and 𝑌Y should not exceed 105105 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 105105 by absolute value.

Exampleinput

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

#### 模拟签到

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e5+100;
int n;
void solve(){
scanf("%d",&n);
int sx=(int)-1e5,sy=(int)1e5,ex=(int)1e5,ey=(int)-1e5;
rep(i,1,n){
int x,y,l,r,u,d;
scanf("%d%d%d%d%d%d",&x,&y,&l,&u,&r,&d);
if(!l) sx=max(sx,x);
if(!r) ex=min(ex,x);
if(!u) sy=min(sy,y);
if(!d) ey=max(ey,y);
}
if(sx<=ex&&sy>=ey) printf("1 %d %d\n",sx,sy);
else puts("0");
}
int main(){
int T;cin>>T;
while(T--) solve();
}


## D2. RGB Substring (hard version)

time limit per test2 seconds memory limit per test256 megabytes

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 𝑎1=𝑏𝑖a1=bi, 𝑎2=𝑏𝑖+1a2=bi+1, 𝑎3=𝑏𝑖+2a3=bi+2, …, 𝑎|𝑎|=𝑏𝑖+|𝑎|−1a|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≤𝑞≤2⋅1051≤q≤2⋅105) — the number of queries. Then 𝑞q queries follow.

The first line of the query contains two integers 𝑛n and 𝑘k (1≤𝑘≤𝑛≤2⋅1051≤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⋅1052⋅105 (∑𝑛≤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 …”.
Exampleinput

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<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
int n,k,dp[maxn];
char s[maxn];
map<int,char> mp;
void solve(){
int ans=0;
scanf("%d%d",&n,&k);getchar();
scanf("%s",s+1);
rep(i,1,n) rep(j,0,2){
dp[j][i]=dp[j][i-1]+(s[i]==mp[(i+j)%3]);
ans=max(ans,dp[j][i]-(i>k?dp[j][i-k]:0));
}
printf("%d\n",k-ans);
}
int main(){
mp='R';mp='G';mp='B';
int T;cin>>T;
while(T--) solve();
}


## E. Connected Component on a Chessboard

time limit per test2 seconds memory limit per test256 megabytes

You are given two integers 𝑏b and 𝑤w. You have a chessboard of size 109×109109×109 with the top left cell at (1;1)(1;1), the cell (1;1)(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: (𝑥−1,𝑦),(𝑥+1,𝑦),(𝑥,𝑦−1),(𝑥,𝑦+1)(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 𝐶1C1 and 𝐶2C2 from this set, there exists a sequence of cells 𝑐1c1, 𝑐2c2, …, 𝑐𝑘ck such that 𝑐1=𝐶1c1=C1, 𝑐𝑘=𝐶2ck=C2, all 𝑐𝑖ci from 11 to 𝑘k are belong to this set of cells and for every 𝑖∈[1,𝑘−1]i∈[1,k−1], cells 𝑐𝑖ci and 𝑐𝑖+1ci+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≤𝑞≤1051≤q≤105) — the number of queries. Then 𝑞q queries follow.

The only line of the query contains two integers 𝑏b and 𝑤w (1≤𝑏,𝑤≤1051≤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⋅1052⋅105 (∑𝑤+∑𝑏≤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][1;109].

Exampleinput

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

#### 判断掉d>3*c+1的情况再随便构造一下就好了

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)2e5+100;
int b,w;
void solve(){
scanf("%d%d",&b,&w);
int c=min(b,w),d=max(b,w);
if(d>3ll*c+1ll){printf("NO\n");return;}
printf("YES\n");
int sx=2,sy=2; if(w>=b) sx++;
rep(i,1,c) printf("%d %d\n%d %d\n",sx,sy,sx-1,sy),sx+=2;
d-=c; sx-=2;
if(d) printf("%d %d\n",sx+1,sy),d--;
while(d){
if(d) printf("%d %d\n",sx,sy+1),d--;
if(d) printf("%d %d\n",sx,sy-1),d--;
sx-=2;
}
}
int main(){
int T;cin>>T;
while(T--) solve();
}


## F. K-th Path

time limit per test2.5 seconds memory limit per test256 megabytes

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 𝑗jto 𝑖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≤𝑖<𝑗≤𝑛1≤i<j≤n), then you need to print the 𝑘k-th element in the sorted array consisting of all 𝑑𝑖,𝑗di,j, where 1≤𝑖<𝑗≤𝑛1≤i<j≤n.Input

The first line of the input contains three integers 𝑛,𝑚n,m and 𝑘k (2≤𝑛≤2⋅1052≤n≤2⋅105, 𝑛−1≤𝑚≤min(𝑛(𝑛−1)2,2⋅105)n−1≤m≤min(n(n−1)2,2⋅105), 1≤𝑘≤min(𝑛(𝑛−1)2,400)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≤𝑥,𝑦≤𝑛1≤x,y≤n, 1≤𝑤≤1091≤w≤109, 𝑥≠𝑦x≠y) denoting an edge between vertices 𝑥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).ExamplesinputCopy

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

#### 容易想到，最坏情况下答案就是第k大的变长，所以我们只需要保留前k长的边然后跑2*k次最短路记录一下答案，最终排序后输出ans[k]就好了，由于k只有400，跑的很快

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e6+100;
int n,m,k,q[maxn],cnt,tot,h[maxn],p,vis[maxn],v,num;
ll dis[maxn],ans[maxn];
queue<int> Q;
struct kk{
int x,y;ll w;
bool operator<(const kk &a)const{return w<a.w;}
}a[maxn];
struct node{int to,next;ll w;}g[maxn<<1];
signed main(){
scanf("%d%d%d",&n,&m,&k);
rep(i,1,m) scanf("%d%d%lld",&a[i].x,&a[i].y,&a[i].w);
sort(a+1,a+m+1);
sort(q+1,q+cnt+1); cnt=unique(q+1,q+cnt+1)-q-1;
rep(i,1,cnt){
int u=q[i];
rep(j,1,cnt) dis[q[j]]=(ll)1e18;
dis[u]=0; Q.push(u);
while (!Q.empty()){
p=Q.front(),Q.pop();vis[p]=0;
for (int t=h[p];t;t=g[t].next)
if(dis[v=g[t].to]>dis[p]+g[t].w){
dis[v]=dis[p]+g[t].w;
if (!vis[v]) Q.push(v),vis[v]=1;
}
}
rep(j,1,cnt) if(i<j) ans[++num]=dis[q[j]];
} sort(ans+1,ans+num+1);
printf("%lld\n",ans[k]);
}