Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)
题目链接:http://codeforces.com/contest/828
1 second
256 megabytes
standard input
standard output
In a small restaurant there are a tables for one person and b tables for two persons.
It it known that n groups of people come today, each consisting of one or two people.
If a group consist of one person, it is seated at a vacant one-seater table. If there are none of them, it is seated at a vacant two-seater table. If there are none of them, it is seated at a two-seater table occupied by single person. If there are still none of them, the restaurant denies service to this group.
If a group consist of two people, it is seated at a vacant two-seater table. If there are none of them, the restaurant denies service to this group.
You are given a chronological order of groups coming. You are to determine the total number of people the restaurant denies service to.
The first line contains three integers n, a and b (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ 2·105) — the number of groups coming to the restaurant, the number of one-seater and the number of two-seater tables.
The second line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 2) — the description of clients in chronological order. If ti is equal to one, then the i-th group consists of one person, otherwise the i-th group consists of two people.
Print the total number of people the restaurant denies service to.
- 4 1 2
1 2 1 1
- 0
- 4 1 1
1 1 2 1
- 2
In the first example the first group consists of one person, it is seated at a vacant one-seater table. The next group occupies a whole two-seater table. The third group consists of one person, it occupies one place at the remaining two-seater table. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, all clients are served.
In the second example the first group consists of one person, it is seated at the vacant one-seater table. The next group consists of one person, it occupies one place at the two-seater table. It's impossible to seat the next group of two people, so the restaurant denies service to them. The fourth group consists of one person, he is seated at the remaining seat at the two-seater table. Thus, the restaurant denies service to 2 clients.
题解:
有a张单人座、b张双人桌。有n组人依次来到餐厅,当为一个人时,首先考虑单人座,如果没有单人座,则去有两个空位的双人桌,如果没有,则去有一个空位的双人桌,如果还没有,则走人。当为双人时,考虑是否有两个空位的双人桌,如果没有,则走人。问走了多少个人?
一开始想法是:对于单人,首先考虑是否有单人座,如果没有,则去找有两个空位的双人桌并坐其中一个位置,然后还剩一个空位就变成了单人桌。后来发现是错误的,因为单人在没有单人桌的时候首先考虑有两个空位的双人桌,而如果把还剩一个空位的双人桌看成单人桌,那么单人就会主动挑这个位置,这样就会影响到了双人的选择(双人桌变多)。所以还是得开三个变量来模拟:a代表单人桌、b代表有两个空位的双人桌,c代表有一个空位的双人桌。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const double eps = 1e-;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = 2e5+;
- int main()
- {
- int n, a, b;
- while(scanf("%d%d%d",&n,&a,&b)!=EOF)
- {
- int sum = , c = ;
- for(int i = ; i<=n; i++)
- {
- int x;
- scanf("%d",&x);
- if(x==)
- {
- if(a) a--;
- else if(b) b--, c++;
- else if(c) c--;
- else sum++;
- }
- else
- {
- if(b) b--;
- else sum += ;
- }
- }
- printf("%d\n", sum);
- }
- }
1 second
256 megabytes
standard input
standard output
Polycarp has a checkered sheet of paper of size n × m. Polycarp painted some of cells with black, the others remained white. Inspired by Malevich's "Black Square", Polycarp wants to paint minimum possible number of white cells with black so that all black cells form a square.
You are to determine the minimum possible number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. The square's side should have positive length.
The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the sizes of the sheet.
The next n lines contain m letters 'B' or 'W' each — the description of initial cells' colors. If a letter is 'B', then the corresponding cell is painted black, otherwise it is painted white.
Print the minimum number of cells needed to be painted black so that the black cells form a black square with sides parallel to the painting's sides. All the cells that do not belong to the square should be white. If it is impossible, print -1.
- 5 4
WWWW
WWWB
WWWB
WWBB
WWWW
- 5
- 1 2
BB
- -1
- 3 3
WWW
WWW
WWW
- 1
In the first example it is needed to paint 5 cells — (2, 2), (2, 3), (3, 2), (3, 3) and (4, 2). Then there will be a square with side equal to three, and the upper left corner in (2, 2).
In the second example all the cells are painted black and form a rectangle, so it's impossible to get a square.
In the third example all cells are colored white, so it's sufficient to color any cell black.
题解:
求一个矩形,里面包含了所有的黑格,且矩形里面的白格数最少。
只需要记录黑格上下左右最大能达到位置,然后计算出两条边,接着看短的那条边是否能补到与长边一样的长度(不能超出界限),如果能,则白格数 = 边长*边长-总黑个数;如果不能,则输出-1.注意:当没有黑格时,输出1,因为最少可以是1个。
写法一:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const double eps = 1e-;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = +;
- char a[MAXN][MAXN];
- int sum[MAXN][MAXN];
- int main()
- {
- int n,m;
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- for(int i = ; i<=n; i++)
- scanf("%s", a[i]+);
- memset(sum, , sizeof(sum));
- for(int i = ; i<=n; i++)
- for(int j = ; j<=m; j++)
- sum[i][j] = sum[i-][j]+sum[i][j-]-sum[i-][j-]+(a[i][j]=='B');
- int ans = INF;
- for(int len = ; len<=min(n,m); len++)
- {
- for(int i = len; i<=n; i++)
- for(int j = len; j<=m; j++)
- {
- int black_cnt = sum[i][j]-sum[i][j-len]-sum[i-len][j]+sum[i-len][j-len];
- if(black_cnt==sum[n][m])
- ans = min(ans, len*len-black_cnt);
- }
- }
- printf("%d\n", ans==INF?-:ans);
- }
- }
写法二:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const double eps = 1e-;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = +;
- char a[MAXN][MAXN];
- int main()
- {
- int n,m;
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- int cnt = , ans = -;
- int u = INF, d = -INF, l = INF, r = -INF;
- for(int i = ; i<=n; i++)
- {
- scanf("%s", a[i]+);
- for(int j = ; j<=m; j++)
- if(a[i][j]=='B')
- {
- cnt++;
- u = min(u,i);
- d = max(d,i);
- l = min(l,j);
- r = max(r,j);
- }
- }
- if(u==INF)
- ans = ;
- else if((r-l<d-u&&d-u+>m)||(d-u<r-l&&r-l+>n))
- ans = -;
- else
- ans = max(r-l+,d-u+)*max(r-l+,d-u+)-cnt;
- printf("%d\n", ans==INF?-:ans);
- }
- }
2 seconds
512 megabytes
standard input
standard output
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.
Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.
Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.
The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.
Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.
In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.
If there are multiple answers, print any of them.
- 3 2
- 2
1 2
2 3
- 5 3
- 3
1 2
2 3
3 4
3 5
In the first example the only network is shown on the left picture.
In the second example one of optimal networks is shown on the right picture.
Exit-nodes are highlighted.
题解:
无根树有n个结点,其中k个为叶子结点,构造一棵无根树,使得最长叶子距离最短。
不难发现,可以先把k个叶子结点和一个根节点固定起来,为了最长叶子距离最短,我们需要做的就是尽可能把剩下的点都均匀地分到每一个叶子结点的路径上。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <cstdlib>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <queue>
- #include <sstream>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const double eps = 1e-;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = 2e5+;
- int fa[MAXN];
- int main()
- {
- int n, k;
- while(scanf("%d%d",&n,&k)!=EOF)
- {
- memset(fa, , sizeof(fa));
- for(int i = ; i<=k; i++)
- fa[i] = k+;
- int p = k+, t = ;
- while(p<=n)
- {
- int tmp = fa[t];
- fa[t] = p;
- fa[p] = tmp;
- p++; t = t%k+;
- }
- int len = (n-k-)/k*;
- if((n-k-)%k==) len++;
- else if((n-k-)%k>) len += ;
- len += ;
- printf("%d\n", len);
- for(int i = ; i<=k; i++) printf("%d %d\n", i, fa[i]);
- for(int i = k+; i<=n; i++) printf("%d %d\n", i, fa[i]);
- }
- }
Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)的更多相关文章
- Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals)
Codeforces Round #423 (Div. 1, rated, based on VK Cup Finals) A.String Reconstruction B. High Load C ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) E. DNA Evolution 树状数组
E. DNA Evolution 题目连接: http://codeforces.com/contest/828/problem/E Description Everyone knows that D ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块
Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load 构造
D. High Load 题目连接: http://codeforces.com/contest/828/problem/D Description Arkady needs your help ag ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction 并查集
C. String Reconstruction 题目连接: http://codeforces.com/contest/828/problem/C Description Ivan had stri ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) A,B,C
A.题目链接:http://codeforces.com/contest/828/problem/A 解题思路: 直接暴力模拟 #include<bits/stdc++.h> using ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B
Pronlem A In a small restaurant there are a tables for one person and b tables for two persons. It i ...
随机推荐
- Json格式化工具 JsonViewer下载
免安装版,分享链接永久有效~! 云盘下载地址: http://cloud.suning.com/cloud-web/share/link.htm?sk=401f784782751055ddc21cdb ...
- 转: 学习Javascript闭包(Closure) (阮一峰)
from: http://www.ruanyifeng.com/blog/2009/08/learning_javascript_closures.html
- JAVA Eclipse创建的Android程序如何不显示标题栏
在manifest.xml文件的application节点添加下面一行即可 android:theme="@android:style/Theme.NoTitleBar" 如果要恢 ...
- Win2003 IIS 安装方法 图文教程
最近水一水 质量不高 见谅 一般大家先安装好win2003系统,图文教程 Win2003 服务器系统安装图文教程要通过控制面板来安装.具体做法为: 1. 进入“控制面板”. 2. 双击“添加或删除程序 ...
- 【Excle数据透视表】如何创建非共享缓存的数据透视表
一般情况下,利用同一个数据源创建多个数据表时,默认创建的是共享缓存的数据透视表.刷新一个数据透视表时会影响其他数据透视表的展示结果. 解决方案 创建非共享缓存的多个数据透视表 步骤一 单击工作表数据任 ...
- 将Cocos2d-x游戏打包成Android应用程序
1. 打开Eclipse(已经装好CDT.ADT和NDK),导入cocos2d-x的Android项目. 2. 导入后java的源码会出现编译错误,打开cocos2d-x引擎的根文件夹\cocos2d ...
- static修饰内部类
创建内容类的方式通过外部类的实例对象来创建 public class AA { int a =1; class BB { int b=3 ; } public static void main(Str ...
- Oracle -- Create User
CREATE USER hibernate IDENTIFIED BY "123" DEFAULT TABLESPACE "HIBERNATE" TEMPORA ...
- phthon 基础 7.3 logging 日志模块
一. logging 的使用 日志是我们排查问题的关键利器,写好日志记录,当我们发生问题时,可以快速定位代码范围进行修改.python有给我们开发者提供好的日志模块,下面我们就来介绍一下logging ...
- Python 单元测试 之setUP() 和 tearDown()
setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在 ...