CodeForces #379(734A|734B|734C|734D|734E|734F)|二分查找|模拟|树的半径|位运算

49 篇文章 1 订阅
21 篇文章 0 订阅

734A: Anton and Danik

题目大意

给定字符串中,D多输出Danik,A多输出Anton,一样多输出Friendship

题解

#include <cstdio>
char s[100005];
int main() {
    int n, i, d = 0, a = 0;
    scanf("%d%s", &n, s);
    for (i = 0; i < n; ++i)
        if (s[i] == 'A') ++a;
        else if (s[i] == 'D') ++d;
    if (a > d) puts("Anton");
    else if (a < d) puts("Danik");
    else puts("Friendship");
    return 0;
}

734B: Anton and Digits

题目大意

你有k2个2,k3个3,k5个5和k6个6,你可以用这些数字组成256或32,问你能组成的数字的和的最大值。

题解

注意到256和32共用了一个2,但很显然2用在256上更优。

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
    int k2, k3, k5, k6, k;
    scanf("%d%d%d%d", &k2, &k3, &k5, &k6);
    k = min(min(k2, k5), k6);
    k2 -= k; k5 -= k; k6 -= k;
    printf("%d", 256 * k + min(k2, k3) * 32);
    return 0;
}

Description

Recently Anton found a box with digits in his room. There are k2 digits 2, k3 digits 3, k5 digits 5 and k6 digits 6.
Anton’s favorite integers are 32 and 256. He decided to compose this integers from digits he has. He wants to make the sum of these integers as large as possible. Help him solve this task!
Each digit can be used no more than once, i.e. the composed integers should contain no more than k2 digits 2, k3 digits 3 and so on. Of course, unused digits are not counted in the sum.

Input

The only line of the input contains four integers k2 , k3 , k5 and k6 — the number of digits 2, 3, 5 and 6 respectively ( 0k2,k3,k5,k65106 ).

Output

Print one integer — maximum possible sum of Anton’s favorite integers that can be composed using digits from the box.

Examples

Input

5 1 3 4

Output

800

Input

1 1 1 1

Output

256

Note

In the first sample, there are five digits 2, one digit 3, three digits 5 and four digits 6. Anton can compose three integers 256 and one integer 32 to achieve the value 256 + 256 + 256 + 32 = 800. Note, that there is one unused integer 2 and one unused integer 6. They are not counted in the answer.
In the second sample, the optimal answer is to create on integer 256, thus the answer is 256.

734C: Anton and Making Potions

题目大意

你要做n杯饮料,分别需要x的时间,你有能力点数s个,有m个咒语(?),第i个咒语可以使所有饮料的制作时间变为a[i],总共消耗b[i]个能力点数;有另外k个咒语,第i个咒语可以使c[i]个饮料瞬间完成,总共消耗d[i]个能力点数,其中输入时c和d是单调递增的。两种咒语,每种咒语至多使用一个。问最小总制作时间。

题解

注意到题目提示:c和d是单调递增的,这暗示我们使用二分查找。考虑到如果枚举第一种咒语,那么已知剩下的能力点数,显然尽可能用光更优。

#include <cstdio>
#include <algorithm>
using namespace std;
#define rep(i,j,k) for(i=j;i<k;++i)
typedef long long ll;
const ll inf = 0x7f7f7f7f7f7f7f7fll;
const int N = 200005;
int a[N], b[N], c[N], d[N];
int main() {
    int n, m, k, x, s, i, j;
    ll ans = inf;
    scanf("%d%d%d%d%d", &n, &m, &k, &x, &s);
    rep(i,0,m) scanf("%d", a + i); a[m] = x;
    rep(i,0,m) scanf("%d", b + i); b[m++] = 0; ++k;
    rep(i,1,k) scanf("%d", c + i); c[0] = 0;
    rep(i,1,k) scanf("%d", d + i); d[0] = 0;
    rep(i,0,m) {
        int j = upper_bound(d, d + k, s - b[i]) - d;
        if (!j) continue;
        ans = min(ans, 1ll * a[i] * (n - c[j - 1]));
    }
    printf("%I64d", ans == inf ? -1 : ans);
    return 0;
}

Description

Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare n potions.
Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two types that can faster the process of preparing potions.
Spells of this type speed up the preparation time of one potion. There are m spells of this type, the i -th of them costs bi manapoints and changes the preparation time of each potion to ai instead of x .
Spells of this type immediately prepare some number of potions. There are k such spells, the i -th of them costs di manapoints and instantly create ci potions.
Anton can use no more than one spell of the first type and no more than one spell of the second type, and the total number of manapoints spent should not exceed s . Consider that all spells are used instantly and right before Anton starts to prepare potions.
Anton wants to get to the next level as fast as possible, so he is interested in the minimum number of time he needs to spent in order to prepare at least n potions.

Input

The first line of the input contains three integers n, m , k ( 1n2109 ,  1m,k2105 ) — the number of potions, Anton has to make, the number of spells of the first type and the number of spells of the second type.
The second line of the input contains two integers x and s ( 2x2109 ,  1s2109 ) — the initial number of seconds required to prepare one potion and the number of manapoints Anton can use.
The third line contains m integers ai ( 1ai<x ) — the number of seconds it will take to prepare one potion if the i -th spell of the first type is used.
The fourth line contains m integers bi ( 1bi2109 ) — the number of manapoints to use the i -th spell of the first type.
There are k integers ci ( 1cin ) in the fifth line — the number of potions that will be immediately created if the i -th spell of the second type is used. It’s guaranteed that ci are not decreasing, i.e. cicj if i<j .
The sixth line contains k integers di ( 1di2109 ) — the number of manapoints required to use the i-th spell of the second type. It’s guaranteed that di are not decreasing, i.e. didj if i<j .

Output

Print one integer — the minimum time one has to spent in order to prepare n potions.

Examples

Input

20 3 2
10 99
2 4 3
20 10 40
4 15
10 80

Output

20

Input

20 3 2
10 99
2 4 3
200 100 400
4 15
100 800

Output

200

Note

In the first sample, the optimum answer is to use the second spell of the first type that costs 10 manapoints. Thus, the preparation time of each potion changes to 4 seconds. Also, Anton should use the second spell of the second type to instantly prepare 15 potions spending 80 manapoints. The total number of manapoints used is 10 + 80 = 90, and the preparation time is 4·5 = 20 seconds (15 potions were prepared instantly, and the remaining 5 will take 4 seconds each).
In the second sample, Anton can’t use any of the spells, so he just prepares 20 potions, spending 10 seconds on each of them and the answer is 20·10 = 200.

734D: Anton and Chess

题目大意

棋盘中,车横或竖着走,象斜着走,皇后既能横或竖着也能斜着走。问这些棋子是否能一步吃到国王。

题解

模拟。。代码写的比较丑

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector<pair<int, int> > v, h, dl, dr;
int main() {
    int n, x, y, i, a, b; char c[3];
    vector<pair<int, int> >::iterator it;
    scanf("%d%d%d", &n, &x, &y);
    for (i = 0; i < n; ++i) {
        scanf("%s%d%d", c, &a, &b);
        bool rook = c[0] == 'R' || c[0] == 'Q';
        bool shop = c[0] == 'B' || c[0] == 'Q';
        if (a == x) v.push_back(make_pair(b, rook));
        if (b == y) h.push_back(make_pair(a, rook));
        if (a + b == x + y) dl.push_back(make_pair(a, shop));
        if (a - b == x - y) dr.push_back(make_pair(a, shop));
    }
    #define check(p,v) {\
        v.push_back(p);\
        sort(v.begin(), v.end());\
        it = lower_bound(v.begin(), v.end(), p);\
        if (it != v.end() - 1 && (it + 1)->second) return puts("YES"), 0;\
        if (it != v.begin() && (it - 1)->second) return puts("YES"), 0;}
    check(make_pair(y, 0), v)
    check(make_pair(x, 0), h)
    check(make_pair(x, 0), dl)
    check(make_pair(x, 0), dr)

    return puts("NO"), 0;
}

Description

Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.
The first task he faced is to check whether the king is in check. Anton doesn’t know how to implement this so he asks you to help.
Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move.
Help Anton and write the program that for the given position determines whether the white king is in check.
Remainder, on how do chess pieces move:
Bishop moves any number of cells diagonally, but it can’t “leap” over the occupied cells.
Rook moves any number of cells horizontally or vertically, but it also can’t “leap” over the occupied cells.
Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can’t “leap”.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of black pieces.
The second line contains two integers x0 and y0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king.
Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — type of the i-th piece and its position. Character ‘B’ stands for the bishop, ‘R’ for the rook and ‘Q’ for the queen. It’s guaranteed that no two pieces occupy the same position.

Output

The only line of the output should contains “YES” (without quotes) if the white king is in check and “NO” (without quotes) otherwise.

Examples

Input

2
4 2
R 1 1
B 1 5

Output

YES

Input

2
4 2
R 3 3
B 1 5

Output

NO

Note

Picture for the first sample:
这里写图片描述
White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is “YES”.
Picture for the second sample:
这里写图片描述
Here bishop can’t reach the cell with the white king, because his path is blocked by the rook, and the bishop cant “leap” over it. Rook can’t reach the white king, because it can’t move diagonally. Hence, the king is not in check and the answer is “NO”.

734E: Anton and Tree

题目大意

给出一颗黑白树,定义paint(i)改变i以及通过与i同颜色节点相连的同色的其他节点,问最少paint多少次就可以把黑白树变成单一的颜色。

题解

注意一个点可以paint多次
考虑到因为paint一个点总是paint这个点所在的同色区域,故一片区域都可以缩成1个点。那么这棵树就可以分层了:一层白一层黑一层白一层黑……
那么很显然了,如果要全部涂成一个颜色,那么答案就是树的半径。

Description

Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph.
There are n vertices in the tree, each of them is painted black or white. Anton doesn’t like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white).
To change the colors Anton can use only operations of one type. We denote it as paint(v), where v is some vertex of the tree. This operation changes the color of all vertices u such that all vertices on the shortest path from v to u have the same color (including v and u). For example, consider the tree
这里写图片描述
and apply operation paint(3) to get the following:
这里写图片描述
Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of vertices in the tree.
The second line contains n integers colori (0 ≤ colori ≤ 1) — colors of the vertices. colori = 0 means that the i-th vertex is initially painted white, while colori = 1 means it’s initially painted black.
Then follow n - 1 line, each of them contains a pair of integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — indices of vertices connected by the corresponding edge. It’s guaranteed that all pairs (ui, vi) are distinct, i.e. there are no multiple edges.

Output

Print one integer — the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white.

Examples

Input

11
0 0 0 1 1 0 1 0 0 1 1
1 2
1 3
2 4
2 5
5 6
5 7
3 8
3 9
3 10
9 11

Output

2

Input

4
0 0 0 0
1 2
2 3
3 4

Output

0

Note

In the first sample, the tree is the same as on the picture. If we first apply operation paint(3) and then apply paint(6), the tree will become completely black, so the answer is 2.
In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0.

734F: Anton and School

题目大意

已知

{bi=(ai and a1)+(ai and a2)++(ai and an)ci=(ai or a1)+(ai or a2)++(ai or an)

{an}

题解

这道题出OI的话数据不好弄啊。。这题明显-1的判断有点麻烦,又不能多给-1的点。。
注意到, (a and b)+(a or b)=a+b ,那么
di=bi+ci=nai+a1+a2++an
d1+d2++dn=2n(a1+a2++an)
ai=di(a1+a2++an)n=did1+d2++dn2nn
现在只剩下检验如此得出的数组a是否合法。
我们只需要直接算一次 bi ci 即可。
注意到 bi ci 都可以逐位计算,总时间 O(nlogn)
需要注意的是。。d数组和会爆int。。

#include <cstdio>
#define rep(i,j,k) for(i=j;i<k;++i)
const int N = 200005;
int bit[N][32], bitsum[32], b2[N], c2[N], b[N], c[N], d[N], a[N];
int main() {
    int n, i, j; long long sd = 0;
    scanf("%d", &n);
    rep(i,0,n) scanf("%d", &b[i]);
    rep(i,0,n) scanf("%d", &c[i]);
    rep(i,0,n) d[i] = b[i] + c[i], sd += d[i];
    sd /= 2 * n;
    rep(i,0,n) {
        a[i] = (d[i] - sd) / n;
        if (a[i] < 0) return puts("-1");
    }
    rep(i,0,n) rep(j,0,31) bit[i][j] = (a[i] & (1 << j)) ? 1 : 0;
    rep(i,0,n) rep(j,0,31) bitsum[j] += bit[i][j];
    rep(i,0,n) rep(j,0,31) {
        b2[i] += (bit[i][j] ? bitsum[j] : 0) << j;
        c2[i] += (bit[i][j] ? n : bitsum[j]) << j;
    }
    rep(i,0,n) if (b2[i] != b[i] || c2[i] != c[i])
        return puts("-1");
    rep(i,0,n) printf("%d ", a[i]);
    return 0;
}

Description

Anton goes to school, his favorite lessons are arraystudying. He usually solves all the tasks pretty fast, but this time the teacher gave him a complicated one: given two arrays b and c of length n , find array a, such that:

{bi=(ai and a1)+(ai and a2)++(ai and an)ci=(ai or a1)+(ai or a2)++(ai or an)

where a and b means bitwise AND, while a or b means bitwise OR.
Usually Anton is good in arraystudying, but this problem is too hard, so Anton asks you to help.

Input

The first line of the input contains a single integers n (1n200000) — the size of arrays b and c.
The second line contains n integers bi ( 0bi109 ) — elements of the array b .
Third line contains n integers ci ( 0ci109 ) — elements of the array c .

Output

If there is no solution, print  - 1.
Otherwise, the only line of the output should contain n non-negative integers ai — elements of the array a <script id="MathJax-Element-1548" type="math/tex">a</script>. If there are multiple possible solutions, you may print any of them.

Examples

Input

4
6 8 4 4
16 22 10 10

Output

3 5 1 1

Input

5
8 25 14 7 16
19 6 9 4 25

Output

-1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值