【比赛题解】Codeforces Round #664 (Div. 2) Problem A~D

目录

  • 前言
  • 题解
      • A. Boboniu Likes to Color Balls
          • Description
          • Example
          • Note
          • Solution
          • Code
      • B. Boboniu Plays Chess
          • Description
          • Input
          • Output
          • Examples
          • Note
          • Solution
          • Code
      • C. Boboniu and Bit Operations
          • Description
          • Input
          • Output
          • Examples
          • Note
          • Solution
          • Code
      • D. Boboniu Chats with Du
          • Description
          • Input
          • Output
          • Examples
          • Note
          • Solution
          • Code

前言

clbtxdy!
唯一一个能上LGM的机构老板。
sto boboniu!
【比赛题解】Codeforces Round #664 (Div. 2) Problem A~D_第1张图片

比赛传送门

题解

A. Boboniu Likes to Color Balls

Description

Boboniu gives you

  • r r r red balls,
  • g g g green balls,
  • b b b blue balls,
  • w w 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 ) T (1≤T≤100) T(1T100) denoting the number of test cases.

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

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 ) (8,1,9,3) (8,1,9,3) to ( 7 , 0 , 8 , 6 ) (7,0,8,6) (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.

给出红,绿,蓝,白,四种颜色球的数量,要求这四种数量的球排成一条直线,能做到回文对称,若不能做到,可以多次操作,同时让一个红,绿,蓝球变成白色球,如果能做到回文对称,输出Yes,如果还是不能做到,输出No.

Solution
  • 水题。
  • 如果 r , g , b , w r,g,b,w r,g,b,w 中有 1 1 1 个或 0 0 0 个偶数,那么我们可以直接将它们排成回文串。
  • 否则,我们进行 1 1 1 次操作,再次进行判断。(要注意是否可以进行 1 1 1 次操作)
  • 再继续进行操作是没有意义的,因为我们只关心四个数的奇偶性,第 2 2 2 次操作与开始时的奇偶性是相同的。
Code
#include 
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define mem(a, x) memset(a, x, sizeof a)
#define pb push_back
#define umap unordered_map
#define pqueue priority_queue
//#define int long long

using namespace std;
typedef long long ll;

template <typename _T>
void rd(_T &x) {
    int f = 1; x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {if (s == '-') f = -1; s = getchar();}
    while (s >= '0' && s <= '9') x = (x<<3)+(x<<1)+(s-'0'), s = getchar();
    x *= f;
}

int main() {
    int _;
    for (rd(_); _; _--) {
        ll a[4];
        rd(a[0]), rd(a[1]), rd(a[2]), rd(a[3]);
        int sum = 0;
        rep(i, 0, 3) if (a[i]&1) sum++;
        if (sum <= 1) {puts("Yes"); continue;}
        a[0]--, a[1]--, a[2]--, a[3] += 3;
        sum = 0;
        bool flg = 0;
        rep(i, 0, 3) {
            if (a[i] < 0) flg = 1;
            if (a[i]&1) sum++;
        }
        if (sum <= 1 && flg == 0) {puts("Yes"); continue;}
        puts("No");
    }
    return 0;
}


B. Boboniu Plays Chess

Description

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 n×m n×m grid (rows are numbered from 1 1 1 to n n n, and columns are numbered from 1 1 1 to m m m). You have a chess piece, and it stands at some cell ( S x , S y ) (S_x,S_y) (Sx,Sy) which is not on the border (i.e. 2 ≤ S x ≤ n − 1 2≤S_x≤n−1 2Sxn1 and 2 ≤ S y ≤ m − 1 2≤S_y≤m−1 2Sym1).

From the cell ( x , y ) (x,y) (x,y), you can move your chess piece to ( x , y ′ ) (x,y′) (x,y) ( 1 ≤ y ′ ≤ m , y ′ ≠ y ) (1≤y′≤m,y′≠y) (1ym,y=y) or ( x ′ , y ) (x′,y) (x,y) ( 1 ≤ x ′ ≤ n , x ′ ≠ x ) (1≤x′≤n,x′≠x) (1xn,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 n n, m m m, S x S_x Sx and S y S_y Sy ( 3 ≤ n , m ≤ 100 , 2 ≤ S x ≤ n − 1 , 2 ≤ S y ≤ m − 1 ) (3≤n,m≤100, 2≤S_x≤n−1, 2≤S_y≤m−1) (3n,m100,2Sxn1,2Sym1) — the number of rows, the number of columns, and the initial position of your chess piece, respectively.

Output

You should print n × m n \times m n×m lines.

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

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:

【比赛题解】Codeforces Round #664 (Div. 2) Problem A~D_第2张图片
一个 n × m n \times m n×m 大的棋盘,在某个位置有一个棋子,棋子的走法和象棋中的“车”一样(从一个格子可以移动到在它同行或同列的任意一个格子),要求输出棋子遍历完整个棋盘所有位置经过的位置。

Solution
  • 一行一行遍历。
  • 如果当前行没有遍历,就遍历改行。
  • 否则寻找没有被遍历过的行。
  • 记录每行的遍历是从 1 1 1 开始还是从 m m m 开始。
  • 特殊判断一下最开始遍历的那行。
Code
#include 
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define mem(a, x) memset(a, x, sizeof a)
#define pb push_back
#define umap unordered_map
#define pqueue priority_queue
//#define int long long

using namespace std;
typedef long long ll;
int h[110];

template <typename _T>
void rd(_T &x) {
    int f = 1; x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {if (s == '-') f = -1; s = getchar();}
    while (s >= '0' && s <= '9') x = (x<<3)+(x<<1)+(s-'0'), s = getchar();
    x *= f;
}

int main() {
    int n, m, sx, sy; rd(n), rd(m), rd(sx), rd(sy);
    int step = 0, x = sx, y = 1;
    while (true) {
        if (step == n*m) break;
        h[x] = 1;
        if (x == sx && sy > 1) {
            rep(i, sy, m) printf("%d %d\n", sx, i), step++;
            per(i, sy-1, 1) printf("%d %d\n", sx, i), step++;
        } else {
            if (y == 1) {rep(i, 1, m) printf("%d %d\n", x, i), step++; y = m;}
            else {per(i, m, 1) printf("%d %d\n", x, i), step++; y = 1;}
        }
        rep(i, 1, n) if (h[i] == 0) {x = i; break;}
    }
    return 0;
}

C. Boboniu and Bit Operations

Description

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 a_1,a_2,…,a_n a1,a2,,an and b 1 , b 2 , … , b m b_1,b_2,…,b_m b1,b2,,bm.

For each i i i ( 1 ≤ i ≤ n ) (1≤i≤n) (1in), you’re asked to choose a j j j ( 1 ≤ j ≤ m ) (1≤j≤m) (1jm) and let c i = a i    &    b j ci=ai \;\&\; bj ci=ai&bj, where & \& & denotes the bitwise AND operation. Note that you can pick the same j j j for different i i i's.

Find the minimum possible c 1    ∣    c 2    ∣    …    ∣    c n c_1\;|\;c_2\;|\;…\;|\;c_n c1c2cn, where ∣ | denotes the bitwise OR operation.

Input

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

The next line contains n n n integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an ( 0 ≤ a i < 29 ) (0≤a_{i}<29) (0ai<29).

The next line contains m m m integers b 1 , b 2 , … , b m b_1,b_2,…,b_m b1,b2,,bm ( 0 ≤ b i < 29 ) (0≤b_i<29) (0bi<29).

Output

Print one integer: the minimum possible c 1    ∣    c 2    ∣    …    ∣    c n c_1\;|\;c_2\;|\;…\;|\;c_n c1c2cn.

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 c1=a1\;\&\;b2=0 c1=a1&b2=0, c 2 = a 2    &    b 1 = 2 c2=a2\;\&\;b1=2 c2=a2&b1=2, c 3 = a 3    &    b 1 = 0 c3=a3\;\&\;b1=0 c3=a3&b1=0, c 4 = a 4    &    b 1 = 0 c4=a4\;\&\;b1=0 c4=a4&b1=0. Thus c 1    ∣    c 2    ∣    c 3    ∣    c 4 = 2 c1\;|\;c2\;|\;c3\;|\;c4=2 c1c2c3c4=2, and this is the minimal answer we can get.

给你两个数列 a , b a,b a,b ,必须对每一个 a i ai ai b j bj bj 中任选一个数得 c i = a i    &    b j ci=ai\;\&\;bj ci=ai&bj ,最后使得 c 1    ∣    c 2    ∣    . . .    ∣    c n c_1\;|\;c_2\;|\;...\;|\;c_n c1c2...cn最小

Solution
  • 显然,答案的最大值为 2 9 − 1 2^9-1 291
  • 那么我们可以用 f o r for for循环暴力枚举答案。
  • 对于每个答案,我们两层 f o r for for循环暴力枚举 a , b a,b a,b,得到 & \& & 之后的结果 c c c
  • 由于是或运算,在二进制下,如果 c c c 当中某一位为 1 1 1,那么答案的那一位也一定为 1 1 1
  • 所以,如果 c c c 的某一位为 1 1 1,而答案中并不存在,说明这个 a i    &    b i a_i \;\&\;b_i ai&bi 是不合法的。
  • 只要有一个 a i    &    b i a_i \;\&\;b_i ai&bi 是合法的,我们就可以输出答案了。
Code
#include 
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define mem(a, x) memset(a, x, sizeof a)
#define pb push_back
#define umap unordered_map
#define pqueue priority_queue
//#define int long long

using namespace std;
typedef long long ll;
int a[1010], b[1010];

template <typename _T>
void rd(_T &x) {
    int f = 1; x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {if (s == '-') f = -1; s = getchar();}
    while (s >= '0' && s <= '9') x = (x<<3)+(x<<1)+(s-'0'), s = getchar();
    x *= f;
}

int main() {
    int n, m; rd(n), rd(m);
    rep(i, 1, n) rd(a[i]);
    rep(i, 1, m) rd(b[i]);
    rep(ans, 0, (1<<9)) {
        bool flg = 0;
        rep(i, 1, n) {
            int sum = 0;
            rep(j, 1, m) {
                int c = a[i]&b[j];
                rep(k, 0, 9) if ((c&(1<<k)) && !(ans&(1<<k))) {sum++; break;}
            }
            if (sum == m) {flg = 1; break;}
        }
        if (flg == 0) return printf("%d", ans), 0;
    }
    return 0;
}

D. Boboniu Chats with Du

Description

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 n n days. On the i i i-th day:

  • If Du can speak, he’ll make fun of Boboniu with fun factor a i a_i ai. 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 m m. On the i i i-th day:

  • If Du can speak and ai>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 , ⋯ , m i n ( i + d , n ) i+1,i+2,⋯,min(i+d,n) 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 a a.

Input

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

The next line contains n n n integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an ( 0 ≤ a i ≤ 1 0 9 ) (0≤a_i≤10^9) (0ai109).

Output

Print one integer: the maximum total fun factor among all permutations of a a 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 ] a′=[15,5,8,10,23] a=[15,5,8,10,23]. Then Du’s chatting record will be:

Make fun of Boboniu with fun factor 15 15 15.
Be muzzled.
Be muzzled.
Make fun of Boboniu with fun factor 10 10 10.
Make fun of Boboniu with fun factor 23 23 23.
Thus the total fun factor is 48 48 48.

给定一个长度为 n n n 的数列 a a aduyi 当天说话的有趣值,如果 a i > m a_i > m ai>m,那么在 i i i 之后有 d d d 天被 clb 禁言。否则可以每天都说话。你需要找到一个排列使得 n n n 天说话有趣值总和最大,问有趣值总和的最大值是多少。

Solution
  • 这题其实并不是很难。
  • 先把有趣值 < m <m 的放在 A A A 数组, < m <m 的放在 B B B 数组。
  • 然后我们枚举选多少个 B B B 数组中的数。
  • 假设我们选了 i i i B B B 数组中的数。
  • 既然都会被禁言,那我们一定是会选择 B B B 数组中最大的 i i i 个数。
  • 其中有一个数我们放在第 n n n 天,这样之后就不会被禁言了。
  • 那么消耗的总天数为 ( x − 1 ) × ( d + 1 ) + 1 (x-1) \times (d+1)+1 (x1)×(d+1)+1
  • 其余还剩的天数我们就尽量在 A A A 数组中选最大的,反正也不会被禁言,前缀和处理即可。
Code
#include 
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define mem(a, x) memset(a, x, sizeof a)
#define pb push_back
#define umap unordered_map
#define pqueue priority_queue
#define int long long

using namespace std;
typedef long long ll;
int dm[100010], xm[100010], S[100010], a[100010];

template <typename _T>
void rd(_T &x) {
    int f = 1; x = 0;
    char s = getchar();
    while (s > '9' || s < '0') {if (s == '-') f = -1; s = getchar();}
    while (s >= '0' && s <= '9') x = (x<<3)+(x<<1)+(s-'0'), s = getchar();
    x *= f;
}

inline bool cmp(const int &x, const int &y) {
    return x > y;
}

signed main() {
    int n, d, m; rd(n), rd(d), rd(m);
    int tot1 = 0, tot2 = 0;
    rep(i, 1, n) rd(a[i]), dm[++tot1] += (a[i] > m)*a[i], xm[++tot2] += (a[i] <= m)*a[i];
    sort(dm+1, dm+1+tot1, cmp);
    sort(xm+1, xm+1+tot2, cmp);
    rep(i, 1, n) S[i] = S[i-1]+xm[i];
    int ans = S[n], sum = 0;
    rep(i, 1, tot1) {
        sum += dm[i];
        if ((i-1)*(d+1)+1 > n) break;
        ans = max(ans, sum+S[n-((i-1)*(d+1)+1)]);
    }
    printf("%lld", ans);
    return 0;
}

你可能感兴趣的:(比赛题解)