zoukankan      html  css  js  c++  java
  • Codeforces Round #664 (Div. 2) 题解

    比赛网址:https://codeforces.com/contest/1395

    A. Boboniu Likes to Color Balls

    网址:https://codeforces.com/contest/1395/problem/A

    Boboniu gives you

    • r red balls,

    • g green balls,

    • b blue balls,

    • w white balls.
      He allows you to do the following operation as many times as you want:

    • Pick a red ball, a green ball, and a blue ball and then change their color to white.

    You should answer if it's possible to arrange all the balls into a palindrome after several (possibly zero) number of described operations.

    Input

    The first line contains one integer (T) ((1≤T≤100)) denoting the number of test cases.

    For each of the next (T) cases, the first line contains four integers (r), (g), (b) and (w) ((0≤r,g,b,w≤10^9)).

    Output

    For each test case, print "Yes" if it's possible to arrange all the balls into a palindrome after doing several (possibly zero) number of described operations. Otherwise, print "No".

    Example

    input

    4
    0 1 1 1
    8 1 9 3
    0 0 0 0
    1000000000 1000000000 1000000000 1000000000
    

    output

    No
    Yes
    Yes
    Yes
    

    Note

    In the first test case, you're not able to do any operation and you can never arrange three balls of distinct colors into a palindrome.

    In the second test case, after doing one operation, changing ((8,1,9,3)) to ((7,0,8,6)), one of those possible palindromes may be "rrrwwwbbbbrbbbbwwwrrr".

    A palindrome is a word, phrase, or sequence that reads the same backwards as forwards. For example, "rggbwbggr", "b", "gg" are palindromes while "rgbb", "gbbgr" are not. Notice that an empty word, phrase, or sequence is palindrome.


    我们想:如果不改变珠子的颜色,什么条件可以使得可以构成回文串?大抵是这样:奇数个珠子的个数小于等于(1)即可。
    同理,改变奇偶的情况只会让奇数变为偶数,偶数变为奇数,(0)个时要特判。

    C ++ AC代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int r, g, b, w; 
    int main()
    {
    	int T;
    	scanf("%d", &T);
    	while(T --)
    	{
    		int odd_num = 0, even_num = 0;
    		scanf("%d %d %d %d", &r, &g, &b, &w);
    		if(r & 1) ++ odd_num;
    		else ++ even_num;
    		if(g & 1) ++ odd_num;
    		else ++ even_num;
    		if(b & 1) ++ odd_num;
    		else ++ even_num;
    		if(w & 1) ++ odd_num;
    		else ++ even_num;
    		if(odd_num == 2 || (odd_num == 3 && (r == 0 ||g == 0 || b == 0))) puts("No");
    		else puts("Yes");
    	}
    	return 0;
    }
    

    B. Boboniu Plays Chess

    网址:https://codeforces.com/contest/1395/problem/B

    Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round.

    You are a new applicant for his company. Boboniu will test you with the following chess question:

    Consider a (n×m) grid (rows are numbered from (1) to (n), and columns are numbered from (1) to (m)). You have a chess piece, and it stands at some cell ((Sx,Sy)) which is not on the border (i.e. (2≤Sx≤n−1) and (2≤Sy≤m−1)).

    From the cell ((x,y)), you can move your chess piece to ((x,y′)) ((1≤y′≤m,y′≠y)) or ((x′,y)) ((1≤x′≤n,x′≠x)). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column.

    Your goal is to visit each cell exactly once. Can you find a solution?

    Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point.

    Input

    The only line of the input contains four integers $n, (m), (Sx) and (Sy) ((3≤n,m≤100), (2≤Sx≤n−1), (2≤Sy≤m−1)) — the number of rows, the number of columns, and the initial position of your chess piece, respectively.

    Output

    You should print (n⋅m) lines.

    The (i)-th line should contain two integers (x_i) and (y_i) ((1≤x_i≤n, 1≤y_i≤m)), denoting the (i)-th cell that you visited. You should print exactly (nm) pairs ((x_i,y_i)), they should cover all possible pairs ((x_i,y_i)), such that (1≤x_i≤n, 1≤y_i≤m).

    We can show that under these constraints there always exists a solution. If there are multiple answers, print any.

    Examples

    input

    3 3 2 2
    

    output

    2 2
    1 2
    1 3
    2 3
    3 3
    3 2
    3 1
    2 1
    1 1
    

    input

    3 4 2 2
    

    output

    2 2
    2 1
    2 3
    2 4
    1 4
    3 4
    3 3
    3 2
    3 1
    1 1
    1 2
    1 3
    

    Note

    Possible routes for two examples:

    image


    我的方案是:先将起点所在的列从上至下来一遍,再重头蛇形来一遍。

    C ++ AC代码

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int n, m, Sx, Sy;
    int main()
    {
    	scanf("%d %d %d %d", &n, &m, &Sx, &Sy);
    	cout << Sx << " "<< Sy << endl;
    	for(int i = n; i >= 1; -- i)
    	{
    		if(i == Sx) continue;
    		printf("%d %d
    ", i, Sy);
    	}
    	int pt = 1;
    	for(int i = 1; i < m; ++ i, ++ pt)
    	{
    		if(pt == Sy) ++ pt;
    		if(i & 1)
    		{
    			for(int j = 1; j <= n; ++ j)
    			{
    				printf("%d %d
    ", j, pt);
    			}
    		}
    		else
    		{
    			for(int j = n; j; -- j)
    			{
    				printf("%d %d
    ", j, pt);
    			}
    		}
    	}
    	return 0;
    }
    

    C. Boboniu and Bit Operations

    网址:https://codeforces.com/contest/1395/problem/C

    Boboniu likes bit operations. He wants to play a game with you.

    Boboniu gives you two sequences of non-negative integers (a_1,a_2,…,a_n) and (b_1,b_2,…,b_m).

    For each (i) ((1≤i≤n)), you're asked to choose a (j) ((1≤j≤m)) and let (c_i) = (a_i) & (b_j), where & denotes the bitwise AND operation. Note that you can pick the same (j) for different (i)'s.

    Find the minimum possible (c_1|c_2|…|c_n), where | denotes the bitwise OR operation.

    Input

    The first line contains two integers (n) and (m) ((1≤n,m≤200)).

    The next line contains (n) integers (a_1,a_2,…,a_n) ((0≤a_i<2^9)).

    The next line contains (m) integers (b_1,b_2,…,b_m) ((0≤b_i<2^9)).

    Output

    Print one integer: the minimum possible (c_1|c_2|…|c_n).

    Examples

    input

    4 2
    2 6 4 0
    2 4
    

    output

    2
    

    input

    7 6
    1 9 1 9 8 1 0
    1 1 4 5 1 4
    

    output

    0
    

    input

    8 5
    179 261 432 162 82 43 10 38
    379 357 202 184 197
    

    output

    147
    

    Note

    For the first example, we have (c_1) = (a_1) & (b_2=0), (c_2) = (a_2) & (b_1=2), (c_3) = (a_3) & (b_1=0), (c_4) = (a_4) & (b_1=0).Thus (c_1|c_2|c_3|c_4=2), and this is the minimal answer we can get.


    此题我的做法是:首先,我预处理所有的AND值,然后对于(a_i),找出最小的数,构成一个集合(s),在集合中求出最大值的最高位;
    把所有高于该位数的数排除,重复上述直至位数为零。

    其实还可以这样做:由于(c_i|A=A),根据这条性质,最终答案一定是(A),使得对于每一个(i),存在((a_i) & (b_j)) | (A=A),由于可能有位数是空的,最小的那一个即为所求。

    C ++ AC代码

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int SIZE = 200 + 10, INF = 1 << 30;
    int n, m, a[SIZE], b[SIZE], Case[SIZE][SIZE], l = 0;
    bool vis[SIZE][SIZE] = {};
    int cnt(int x)
    {
    	int num = 0;
    	while(x)
    	{
    		++ num;
    		x >>= 1;
    	}
    	return num;
    }
    int main()
    {
    	scanf("%d %d", &n, &m);
    	for(int i = 1; i <= n; ++ i) scanf("%d", &a[i]);
    	for(int i = 1; i <= m; ++ i) scanf("%d", &b[i]);
    	for(int i = 1; i <= n; ++ i)
    	{
    		int tmp = 0x3f3f3f3f;
    		for(int j = 1; j <= m; ++ j)
    		{
    			Case[i][j] = a[i] & b[j];
    			tmp = min(tmp, Case[i][j]);
    		}
    		l = max(l, tmp);
    	}
    	l = cnt(l);
    	for(int i = 1; i <= n; ++ i)
    		for(int j = 1; j <= m; ++ j)
    			vis[i][j] = true;
    	if(!l) 
    	{
    		puts("0");
    		return 0;
    	}
    	int ans = 1 << l - 1;
    	for(int i = 1; i <= n; ++ i)
    	{
    		for(int j = 1; j <= m; ++ j)
    		{
    			if(Case[i][j] >= 1 << l) vis[i][j] = false;
    			else
    			{
    				Case[i][j] %= 1 << l - 1;
    			}
    		}
    	}
    	while(l)
    	{
    		l = 0;
    		for(int i = 1; i <= n; ++ i)
    		{
    			int tmp = INF;
    			for(int j = 1; j <= m; ++ j)
    			{
    				if(vis[i][j]) tmp = min(tmp, Case[i][j]);
    			}
    			if(tmp == INF) tmp = 0;
    			l = max(l, tmp);
    		}
    		l = cnt(l);
    		if(l == 0) break;
    		ans += 1 << l - 1;
    		for(int i = 1; i <= n; ++ i)
    		{
    			for(int j = 1; j <= m; ++ j)
    			{
    				if(Case[i][j] >= 1 << l) vis[i][j] = false;
    				else
    				{
    					Case[i][j] %= 1 << l - 1;
    				}
    			}
    		}
    	}
    	printf("%d
    ", ans);
    	return 0;
    }
    

    A. Boboniu Chats with Du

    网址:https://codeforces.com/contest/1394/problem/A

    Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.

    In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.

    Du will chat in the group for n days. On the (i)-th day:

    • If Du can speak, he'll make fun of Boboniu with fun factor (a_i). But after that, he may be muzzled depending on Boboniu's mood.

    • Otherwise, Du won't do anything.
      Boboniu's mood is a constant (m). On the (i)-th day:

    • If Du can speak and (a_i>m), then Boboniu will be angry and muzzle him for d days, which means that Du won't be able to speak on the (i+1,i+2,⋯,min(i+d,n))-th days.

    • Otherwise, Boboniu won't do anything.
      The total fun factor is the sum of the fun factors on the days when Du can speak.

    Du asked you to find the maximum total fun factor among all possible permutations of (a).

    Input

    The first line contains three integers (n), (d) and (m) ((1≤d≤n≤10^5,0≤m≤10^9)).

    The next line contains (n) integers (a_1,a_2,…,a_n) ((0≤a_i≤10^9)).

    Output

    Print one integer: the maximum total fun factor among all permutations of (a).

    Examples

    input

    5 2 11
    8 10 15 23 5
    

    output

    48
    

    input

    20 2 16
    20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7
    

    output

    195
    

    Note

    In the first example, you can set (a′=[15,5,8,10,23]). Then Du's chatting record will be:

    1. Make fun of Boboniu with fun factor (15).
    2. Be muzzled.
    3. Be muzzled.
    4. Make fun of Boboniu with fun factor (10).
    5. Make fun of Boboniu with fun factor (23).
      Thus the total fun factor is (48).

    不看时间复杂度其实是个(0-1)背包问题,只不过要考虑顺序。
    分两个类型:大于(m)和小于等于(m)
    从大到小排序,我们需要贪心求最优解。扫描更新答案。

    C ++ AC代码

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int maxn = 100000 + 5;
    struct cmp
    {
    	bool operator () (const long long& x, const long long& y)
    	{
    		return x > y;
    	}
    };
    int n, d, m, k = 0, p = 0;
    long long a[maxn], b[maxn], c[maxn];
    int main()
    {
    	scanf("%d %d %d", &n, &d, &m);
    	for(int i = 1; i <= n; ++ i) scanf("%lld", &a[i]);
    	sort(a + 1, a + n + 1, cmp());
    	for(int i = 1; i <= n; ++ i)
    	{
    		if(a[i] > m) b[++ k] = a[i];
    		else c[++ p] = a[i];
    	}
    	for(int i = 2; i <= k; ++ i) b[i] += b[i - 1];
    	for(int i = 2; i <= p; ++ i) c[i] += c[i - 1];
    	long long res, sum = 0;
    	if(p == n)
    	{
    		printf("%lld
    ", c[p]);
    		return 0;
    	}
    	fill(c + p + 1, c + n + 1, c[p]);
    	res = 0;
    	for(int i = 1; i <= k; ++ i)
    	{
    		if(1ll * (i - 1) * (d + 1) + 1 <= n) res = max(res, b[i] + c[n - 1ll * (i - 1) * (d + 1) - 1]);
    	}
    	printf("%lld
    ", res);
    	return 0;
    }
    

    总结

    这次比赛感觉一般,可能是C题有些钻牛角尖了,D题我连背包都没有想出。还是得勤加练习啊!

    参考文献

  • 相关阅读:
    prometheus+alertmanage+grafana安装部署
    HAproxy
    redis安装部署
    rsync+inotify实现实时同步
    简单的计算功能,还需要优化
    python3配置文件的增删改查,记录一下
    一个简单的购物商城,记录一下。
    python函数参数
    python list内部功能记录
    python3 str各个功能记录
  • 原文地址:https://www.cnblogs.com/zach20040914/p/13585159.html
Copyright © 2011-2022 走看看