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

• [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];
• [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≤?≤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[3][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[1]='R';mp[2]='G';mp[0]='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]);
}


0 评论