CodeForces #379(734A|734B|734C|734D|734E|734F)|二分查找|模拟|树的半径|位运算
2016-11-16 17:58
344 查看
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 (0 ≤ k2, k3, k5, k6 ≤ 5⋅106).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 4Output
800Input
1 1 1 1Output
256Note
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 , b , c , d ; 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 (1 ≤ n ≤ 2⋅109, 1 ≤ m, k ≤ 2⋅105) — 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 (2 ≤ x ≤ 2⋅109, 1 ≤ s ≤ 2⋅109) — 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 (1 ≤ ai < 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 (1 ≤ bi ≤ 2⋅109) — the number of manapoints to use the i-th spell of the first type.
There are k integers ci (1 ≤ ci ≤ n) 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. ci ≤ cj if i < j.
The sixth line contains k integers di (1 ≤ di ≤ 2⋅109) — 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. di ≤ dj 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
24 2
R 1 1
B 1 5
Output
YESInput
24 2
R 3 3
B 1 5
Output
NONote
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
110 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
2Input
40 0 0 0
1 2
2 3
3 4
Output
0Note
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=n⋅ai+a1+a2+⋯+an
d1+d2+⋯+dn=2n(a1+a2+⋯+an)
ai=di−(a1+a2+⋯+an)n=di−d1+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 [32], bitsum[32], b2 , c2 , b , c , d , a ; 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 (1 ≤ n ≤ 200 000) — the size of arrays b and c.The second line contains n integers bi (0 ≤ bi ≤ 109) — elements of the array b.
Third line contains n integers ci (0 ≤ ci ≤ 109) — 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. If there are multiple possible solutions, you may print any of them.
Examples
Input
46 8 4 4
16 22 10 10
Output
3 5 1 1Input
58 25 14 7 16
19 6 9 4 25
Output
-1相关文章推荐
- 51NOD 1467 旋转绳 二分查找 模拟
- 0基础学编程之猜数字游戏,折半二分查找,模拟输入密码及大小写互换
- 算法导论_二分查找
- 折半,二分查找问题
- 最长递增子序列 O(NlogN)算法 ( DP + 二分查找)
- 二分查找的递归和非递归实现
- 【数据结构与算法】二分查找
- hdu1597(二分查找)
- 九度OJ 1349 数字在排序数组中出现的次数 -- 二分查找
- 二分查找
- 二分查找
- leetcode 33. Search in Rotated Sorted Array (复杂二分查找)
- 在MySQL中实现二分查找的详细教程
- 浅谈数据的查找(二分查找)
- 一维数组 冒泡排序 shell 排序 二分查找 C++
- 二分查找示例
- 二分查找的扩展
- 二分查找
- HDU 2199 Can you solve this equation(二分查找)
- 图方法:二分无向图的联通子图查找