Codeforces Round #575 (Div. 3)(ABCD1D2E)

Three Piles of Candies CodeForces - 1196A
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
其实题意啥意思也没看明白,反正加起来除以二就对了(_
代码如下:

#include<bits/stdc++.h>
#define ll unsigned long long
using namespace std;

ll a,b,c;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld%lld",&a,&b,&c);
		cout<<((a+b+c)>>1)<<endl;
	}
}

Odd Sum Segments CodeForces - 1196B
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≤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;r1],[r1+1;r2],[r2+1,r3],…,[rk−1+1,n]. Note that rk is always n but you should print it anyway.

Example
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
n个数分成k段,每一段的数字之和都是奇数。输出分割的位置。
奇数的个数小于k或者奇数个数减去k之后还是奇数的话,就不可能实现,剩下的就直接遍历一遍出去就好了,最后一个数字一定是n!!
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=2e5+100;
int a[maxx];
int b[maxx];
int n,k;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int cnt=0;
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]&1) b[cnt++]=i+1;
		}
		if(cnt<k||(cnt-k)&1) puts("NO");
		else
		{
			puts("YES");
			for(int i=0;i<k-1;i++) cout<<b[i]<<" ";
			cout<<n<<endl;
		}
	}
}

Robot Breakout CodeForces - 1196C
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-th 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个机器人,以及他们可以上下左右可以向哪个方向移动。问他们可以共同移动到的点是啥,任意一个就行。
我们知道它不可以向哪个方向移动,那么我们就可以确定一个这个机器人移动的边界。那么我们就能通过这n个机器人构造出一个矩形。如果矩形不合乎规矩就不成功。边界一开始设置为1e5!!!
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;

int x,y,r1,r2,r3,r4;
int n;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int lx=-1e5,rx=1e5,ty=1e5,by=-1e5;
		while(n--)
		{
			cin>>x>>y>>r1>>r2>>r3>>r4;
			if(!r1) lx=max(lx,x);
			if(!r2) ty=min(ty,y);
			if(!r3) rx=min(rx,x);
			if(!r4) by=max(by,y);
		}
		if(lx>rx||by>ty) puts("0");
		else cout<<1<<" "<<lx<<" "<<by<<endl; 
	}
}

RGB Substring (easy version) CodeForces - 1196D1
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”.
这个D1顶多算是个div2B题。。
反正就不到2000的字符串,直接暴力遍历就行
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

string ss[10]={"RGB","GBR","BRG"};
int n,m;
string s;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		cin>>s;
		if(m==1) 
		{
			printf("0\n");
			continue;
		}
		else if(m==2)
		{
			if(s.find("RG")!=-1||s.find("GB")!=-1||s.find("BR")!=-1) printf("0\n");
			else printf("1\n");
			continue;
		}
		int ans=inf;
		int cnt=0;
		string f;
		for(int i=0;i<=n-m;i++)
		{
			f=s.substr(i,m);
			for(int k=0;k<3;k++)
			{
				for(int j=0,l=0;j<m;l++,j++)
				{
					l%=3;
					if(f[j]!=ss[k][l]) cnt++;
				}
				ans=min(ans,cnt);
				cnt=0;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

RGB Substring (hard version) CodeForces - 1196D2
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”.
很明显,直接暴力不行了(这不是废话嘛)。。
那咋整呢???
其实和上面那道题的主题思路一样,还是那几个字符串。但是我们需要记录一下每一个位置不同个数的前缀和,然后固定了长度,遍历字符串就行O(3*n)的时间复杂度。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
string ss[3]={"RGB","GBR","BRG"};
int sum[maxx][3];
string s;
int n,m;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		cin>>s;
		for(int j=0;j<3;j++)
		{
			for(int i=1,l=0;i<=n;l++,i++)
			{
				l%=3;
				if(s[i-1]!=ss[j][l]) sum[i][j]=sum[i-1][j]+1;
				else sum[i][j]=sum[i-1][j];
			}
		}
		int ans=inf;
		for(int j=0;j<3;j++)
		{
			for(int i=m;i<=n;i++)
			{
				ans=min(ans,sum[i][j]-sum[i-m][j]);
			}
		}
		printf("%d\n",ans);
	}
}

E题很烦,不知道哪儿错了,第一个样例就不对。我是构造出来之后就先输出黑色的,再输出白色的,就不对。。哪位大佬指教一下。
错误代码!!!!!!!

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int n,m;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		if(max(n,m)>min(n,m)*3+1) 
		{
			puts("NO");
		}
		else
		{
			puts("YES");
			if(n==m)
			{
				int cnt=1;
				for(int i=1;i<=n;i++) cout<<3<<" "<<cnt<<endl,cnt+=2;
				cnt=2;
				for(int i=1;i<=n;i++) cout<<3<<" "<<cnt<<endl,cnt+=2;
			}
			else if(n>m)
			{
				int cnt=1;
				for(int i=1;i<=m+1;i++) cout<<10<<" "<<cnt<<endl,cnt+=2;
				n-=m;n--;
				if(n>m) 
				{
					cnt=2;
					for(int i=1;i<=m;i++) cout<<9<<" "<<cnt<<endl,cnt+=2;
					n-=m;cnt=2;
					for(int i=1;i<=n;i++) cout<<11<<" "<<cnt<<endl,cnt+=2;
				}
				else 
				{
					cnt=2;
					for(int i=1;i<=n;i++) cout<<9<<" "<<cnt<<endl,cnt+=2;
				}
				cnt=2;
				for(int i=1;i<=m;i++) cout<<10<<" "<<cnt<<endl,cnt+=2;
			}
			else if(n<m)
			{
				int cnt=2;
				for(int i=1;i<=n;i++) cout<<10<<" "<<cnt<<endl,cnt+=2;
				cnt=1;
				for(int i=1;i<=n+1;i++) cout<<10<<" "<<cnt<<endl,cnt+=2;
				m-=n;m--;
				if(m>n)
				{
					cnt=2;
					for(int i=1;i<=n;i++) cout<<9<<" "<<cnt<<endl,cnt+=2;
					m-=n;cnt=2;
					for(int i=1;i<=m;i++) cout<<11<<" "<<cnt<<endl,cnt+=2;
				}
				else 
				{
					cnt=2;
					for(int i=1;i<=m;i++) cout<<9<<" "<<cnt<<endl,cnt+=2;
				}
			} 
		}
	}
}

正确代码:

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int w,b;
        scanf("%d%d",&b,&w);//b黑色  w白色
        if(max(w,b)>min(w,b)*3+1)
        {
            printf("NO\n");
            continue;
        }
        if(w==b)
        {
            printf("YES\n");
            for(int i=3; i<=w+b+2; ++i)
                printf("5 %d\n",i);
        }
        else if(b<w)
        {
            printf("YES\n");
            w=w-b-1;//将在一行的减掉
            for(int i=4; i<=b*2+4; i++)
            {
                printf("4 %d\n",i);
                if(w>0 && i&1)
                {
                    printf("3 %d\n",i);
                    w--;
                }
                if(w>0  && i&1 )
                {
                    printf("5 %d\n",i);
                    w--;
                }
            }
        }
        else if(b>w)
        {
            swap(b,w);
            printf("YES\n");
            w=w-b-1;//将在一行的减掉
            for(int i=4; i<=b*2+4; i++)
            {
                printf("3 %d\n",i);
                if(w>0 && i&1)
                {
                    printf("2 %d\n",i);
                    w--;
                }
                if(w>0  && i&1 )
                {
                    printf("4 %d\n",i);
                    w--;
                }
            }
        }
    }
    return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值