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 ...
随机推荐
- from: Maven实战(九)——打包的技巧
from : http://www.infoq.com/cn/news/2011/06/xxb-maven-9-package 要点: 1. 打出可执行的jar包, 2. 自定义打包
- mongodb管理副本集(持续更新中)
许多维护工作不能在备份节点上完成 因为要写操作,也不能在主节点上进行,这就需要单机模式启动服务器, 是指重启成员服务器,让他成为一个单机运行的服务器,而不再是副本集中的一员(临时的) 在单机 ...
- 使用Python+Selenium过程中中常见的问题汇总
1.提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 542: ordinal not in range( ...
- 每天5道面试题(二)java基础
说出Servlet的生命周期,并说出Servlet和CGI的差别 Servlet被server实例化后,容器执行其init方法,请求到达时执行其service方法,service方法自己主动派遣执行与 ...
- 如何查看在Heroku上部署了那些站点
使用以下命令查看 Heroku 站点地址: $ heroku domains 例如: http://peaceful-springs-94972.herokuapp.com/signu ...
- Unique Binary Search Trees I&II——给定n有多少种BST可能、DP
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Shell脚本之:字符串
字符串可以用单引号,也可以用双引号,也可以不用引号. 单引号 str='this is a string' 单引号字符串的限制: 1.单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的: 2 ...
- JAVA实现KNN分类
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/51064307 http://www.llwjy.com/blogdetail/f ...
- nginx for windows 配置多域名反向代理
调试了很久...哦耶 共享出来吧 其实 nginx反向代理同一ip多个域名,给header加上host就可以了 upstream test.test.cn { server 119. ...
- 给js对象赋值,赋值key
var pastResult = []; pastResult.push(feature.attributes.F_iID); pastResult.push(feature.attributes.F ...