博客
关于我
Codeforces Round #652 (Div. 2) 题解
阅读量:478 次
发布时间:2019-03-06

本文共 10739 字,大约阅读时间需要 35 分钟。

A. FashionabLEE

网址:

Lee is going to fashionably decorate his house for a party, using some regular convex polygons...

Lee thinks a regular n-sided (convex) polygon is beautiful if and only if he can rotate it in such a way that at least one of its edges is parallel to the OX-axis and at least one of its edges is parallel to the OY-axis at the same time.

Recall that a regular n-sided polygon is a convex polygon with n vertices such that all the edges and angles are equal.

Now he is shopping: the market has t regular polygons. For each of them print YES if it is beautiful and NO otherwise.

Input

The first line contains a single integer t (\(1≤t≤10^4\)) — the number of polygons in the market.

Each of the next t lines contains a single integer \(n_i\) (\(3 ≤ n_i ≤ 10^9\)): it means that the i-th polygon is a regular \(n_i\)-sided polygon.

Output

For each polygon, print YES if it's beautiful or NO otherwise (case insensitive).

Example
input
434121000000000
output
NOYESYESYES
Note

In the example, there are 4 polygons in the market. It's easy to see that an equilateral triangle (a regular 3-sided polygon) is not beautiful, a square (a regular 4-sided polygon) is beautiful and a regular 12-sided polygon (is shown below) is beautiful as well.

啥都不用说了。
代码如下:

#include
#include
using namespace std;int main(){ int T; scanf("%d", &T); while(T --) { int n; scanf("%d", &n); if(n % 4) puts("NO"); else puts("YES"); } return 0;}

B. AccurateLee

网址:

Lee was cleaning his house for the party when he found a messy string under the carpets. Now he'd like to make it clean accurately and in a stylish way...

The string s he found is a binary string of length n (i. e. string consists only of 0-s and 1-s).

In one move he can choose two consecutive characters si and si+1, and if si is 1 and si+1 is 0, he can erase exactly one of them (he can choose which one to erase but he can't erase both characters simultaneously). The string shrinks after erasing.

Lee can make an arbitrary number of moves (possibly zero) and he'd like to make the string s as clean as possible. He thinks for two different strings x and y, the shorter string is cleaner, and if they are the same length, then the lexicographically smaller string is cleaner.

Now you should answer t test cases: for the i-th test case, print the cleanest possible string that Lee can get by doing some number of moves.

Small reminder: if we have two strings x and y of the same length then x is lexicographically smaller than y if there is a position i such that x1=y1, x2=y2,..., xi−1=yi−1 and xi<yi.

Input

The first line contains the integer t (\(1≤t≤10^4\)) — the number of test cases.

Next 2t lines contain test cases — one per two lines.

The first line of each test case contains the integer n (\(1≤n≤10^5\)) — the length of the string s.

The second line contains the binary string s. The string s is a string of length n which consists only of zeroes and ones.

It's guaranteed that sum of n over test cases doesn't exceed \(10^5\).

Output

Print t answers — one per test case.

The answer to the i-th test case is the cleanest string Lee can get after doing some number of moves (possibly zero).

Example
input
51000011111114010181100110110111000000011
output
00011111110010101
Note

In the first test case, Lee can't perform any moves.

In the second test case, Lee should erase s2.

In the third test case, Lee can make moves, for example, in the following order: 11001101 → 1100101 → 110101 → 10101 → 1101 → 101 → 01.

这道题我用数据结构(栈)模拟了整个过程,其实可以更优秀。

考虑:如果1在该序列最右边,那么这些1是消不掉;如果0在该序列最左边,那么同理,这些0怎么消也消不掉。

对于中间的那一子段,一定可以消只剩下一个数字,0或1。显然0更优。

两种代码(我的和standard)如下:

#include
#include
#include
using namespace std;const int maxn = 100000 + 15;int n, a[maxn];int main(){ int T; stack
s; scanf("%d", &T); while(T --) { while(!s.empty()) s.pop(); scanf("%d", &n); for(int i = 1; i <= n; ++ i) scanf("%1d", &a[i]); for(int i = n; i; -- i) { if(s.empty() || s.top() == 1) { s.push(a[i]); continue; } if(a[i] == 0) s.push(a[i]); else { while(!s.empty() && !s.top()) { s.pop(); } s.push(a[i] - 1); } } while(!s.empty()) { printf("%d", s.top()); s.pop(); } puts(""); } return 0; }/*version of std:#include
#include
#include
using namespace std;const int maxn = 100000 + 5;int n, s[maxn];int main(){ int T; scanf("%d", &T); while(T --) { scanf("%d", &n); for(int i = 0; i < n; ++ i) scanf("%1d", &s[i]); int l = 0, r = 0; for(int i = 0; i < n; ++ i) { if(s[i] == 1) break; ++ l; } for(int i = n - 1; i >= 0; -- i) { if(s[i] == 0) break; ++ r; } if(l + r == n) { for(int i = 0; i < l; ++ i) putchar('0'); for(int i = 0; i < r; ++ i) putchar('1'); } else { for(int i = 0; i < l; ++ i) putchar('0'); putchar('0'); for(int i = 0; i < r; ++ i) putchar('1'); } puts(""); } return 0;}*/

C. RationalLee

网址:

Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it's time to distribute them between his friends rationally...

Lee has n integers a1,a2,…,an in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly wi integers and each integer will be handed over to exactly one friend.

Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get.

Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness.

Input

The first line contains one integer t (\(1 ≤ t ≤ 10^4\)) — the number of test cases.

Next 3t lines contain test cases — one per three lines.

The first line of each test case contains two integers n and k (\(1 ≤ n ≤ 2⋅10^5; 1 ≤ k ≤ n\)) — the number of integers Lee has and the number of Lee's friends.

The second line of each test case contains n integers a1,a2,…,an (\(−10^9 ≤ ai ≤ 10^9\)) — the integers Lee has.

The third line contains k integers w1,w2,…,wk (\(1≤wi≤n; w1 + w2 + … + wk=n\)) — the number of integers Lee wants to give to each friend.

It's guaranteed that the sum of n over test cases is less than or equal to \(2⋅10^5\).

Output

For each test case, print a single integer — the maximum sum of happiness Lee can achieve.

Example
input
34 21 13 7 171 36 210 10 10 10 11 113 34 41000000000 1000000000 1000000000 10000000001 1 1 1
output
48428000000000
Note

In the first test case, Lee should give the greatest integer to the first friend (his happiness will be \(17+17\)) and remaining integers to the second friend (his happiness will be \(13+1\)).

In the second test case, Lee should give \({10,10,11}\) to the first friend and to the second friend, so the total happiness will be equal to \((11+10)+(11+10)\)

In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.

先考虑最大值。显然,最大值顺次分配即可。这时候,我们先将每个人的分配个数按从小到大的顺序排序,优先满足少的。

对于最小值,只分配一个的人最大值等于最小值,因此我们可以不必考虑了。其他的,利用贪心问题得解。

过程真的很复杂,但很妙,

具体证明见Tutorial:

代码如下:

#include
#include
#include
#include
#include
using namespace std;const int SIZE = 2000000;long long n, a[SIZE], k, w[SIZE];int main(){ int t; scanf("%d", &t); while(t --) { scanf("%d %d", &n, &k); for(int i = 0; i < n; ++ i) scanf("%lld", &a[i]); sort(a, a + n); for(int i = 0; i < k; ++ i) { scanf("%lld", &w[i]); } sort(w, w + k); int l = n, head = 0; long long ans = 0; for(int i = 0; i < k; ++ i) { -- l; if(w[i] > 1) ans += a[l]; else { ans += a[l] * 2; head = i + 1; } } for(int i = 0; i < k; ++ i) -- w[i]; for(int i = head; i < k; ++ i) { l -= w[i]; ans += a[l]; } printf("%lld\n", ans); } return 0;}

D. TediousLee

网址:

Lee tried so hard to make a good div.2 D problem to balance his recent contest, but it still doesn't feel good at all. Lee invented it so tediously slow that he managed to develop a phobia about div.2 D problem setting instead. And now he is hiding behind the bushes...

Let's define a Rooted Dead Bush (RDB) of level n as a rooted tree constructed as described below.

A rooted dead bush of level 1 is a single vertex. To construct an RDB of level i we, at first, construct an RDB of level i−1, then for each vertex u:

  • if u has no children then we will add a single child to it;
  • if u has one child then we will add two children to it;
  • if u has more than one child, then we will skip it.
    Rooted Dead Bushes of level 1, 2 and 3.

Let's define a claw as a rooted tree with four vertices: one root vertex (called also as center) with three children. It looks like a claw:

The center of the claw is the vertex with label 1.

Lee has a Rooted Dead Bush of level n. Initially, all vertices of his RDB are green.

In one move, he can choose a claw in his RDB, if all vertices in the claw are green and all vertices of the claw are children of its center, then he colors the claw's vertices in yellow.

He'd like to know the maximum number of yellow vertices he can achieve. Since the answer might be very large, print it modulo \(10^9+7\).

Input

The first line contains one integer t (\(1≤t≤10^4\)) — the number of test cases.

Next t lines contain test cases — one per line.

The first line of each test case contains one integer n (\(1≤n≤2⋅10^6\)) — the level of Lee's RDB.

Output

For each test case, print a single integer — the maximum number of yellow vertices Lee can make modulo \(10^9+7\).

Example
input
7123451002000000
output
004412990998587804665184
Note

It's easy to see that the answer for RDB of level 1 or 2 is 0.

The answer for RDB of level 3 is 4 since there is only one claw we can choose: \({1,2,3,4}\).

The answer for RDB of level 4 is 4 since we can choose either single claw \({1,3,2,4}\) or single claw \({2,7,5,6}\). There are no other claws in the RDB of level 4 (for example, we can't choose \({2,1,7,6}\), since 1 is not a child of center vertex 2).

Rooted Dead Bush of level 4.

这是我见过最好的一道树形DP题。

首先明确一点:等级i的树是由等级i-1和等级i-2的组成的。

不妨设\(dp[i]\)代表等级i的树最大值为多少。

很显然,如果等级i的根节点不染色,那么它最大值一定来源于三棵子树。于是有:\(dp[i] = dp[i - 1] + dp[i - 2] * 2\)

如果根节点染色,那么三棵子树的所有根节点一定不能染色。考虑到只有3的倍数符合。由此:\(dp[i] = dp[i - 1] + dp[i - 2] * 2 + (i % 3) ? 0 : 4;\)

代码如下:

#include
#include
#include
#include
using namespace std;const int maxn = 2000000 + 1, mod = 1000000007;int n;long long dp[maxn] = {};int main(){ int T; scanf("%d", &T); for(int i = 3; i < maxn; ++ i) dp[i] = (dp[i - 1] + dp[i - 2] * 2 + (!(i % 3) ? 4 : 0)) % mod; while(T --) { scanf("%d", &n); printf("%d\n", dp[n]); } return 0;}

转载地址:http://nohbz.baihongyu.com/

你可能感兴趣的文章
NIFI1.21.0最新版本安装_配置使用HTTP登录_默认是用HTTPS登录的_Https登录需要输入用户名密码_HTTP不需要---大数据之Nifi工作笔记0051
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增加修改实时同步_使用JsonPath及自定义Python脚本_03---大数据之Nifi工作笔记0055
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_插入修改删除增量数据实时同步_通过分页解决变更记录过大问题_01----大数据之Nifi工作笔记0053
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表或全表增量同步_实现指定整库同步_或指定数据表同步配置_04---大数据之Nifi工作笔记0056
查看>>
NIFI1.23.2_最新版_性能优化通用_技巧积累_使用NIFI表达式过滤表_随时更新---大数据之Nifi工作笔记0063
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现update数据实时同步_实际操作05---大数据之Nifi工作笔记0044
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_根据binlog实现数据实时delete同步_实际操作04---大数据之Nifi工作笔记0043
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置binlog_使用处理器抓取binlog数据_实际操作01---大数据之Nifi工作笔记0040
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_实现数据插入数据到目标数据库_实际操作03---大数据之Nifi工作笔记0042
查看>>
NIFI从MySql中增量同步数据_通过Mysql的binlog功能_实时同步mysql数据_配置数据路由_生成插入Sql语句_实际操作02---大数据之Nifi工作笔记0041
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_03_来吧用NIFI实现_数据分页获取功能---大数据之Nifi工作笔记0038
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_不带分页处理_01_QueryDatabaseTable获取数据_原0036---大数据之Nifi工作笔记0064
查看>>
NIFI从MySql中离线读取数据再导入到MySql中_无分页功能_02_转换数据_分割数据_提取JSON数据_替换拼接SQL_添加分页---大数据之Nifi工作笔记0037
查看>>
NIFI从Oracle11G同步数据到Mysql_亲测可用_解决数据重复_数据跟源表不一致的问题---大数据之Nifi工作笔记0065
查看>>
NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
查看>>
nifi使用过程-常见问题-以及入门总结---大数据之Nifi工作笔记0012
查看>>
NIFI分页获取Mysql数据_导入到Hbase中_并可通过phoenix客户端查询_含金量很高的一篇_搞了好久_实际操作05---大数据之Nifi工作笔记0045
查看>>
NIFI分页获取Postgresql数据到Hbase中_实际操作---大数据之Nifi工作笔记0049
查看>>
NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
查看>>