题目链接:http://codeforces.com/contest/828

A. Restaurant Tables
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

The first line contains three integers na 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.

Output

Print the total number of people the restaurant denies service to.

Examples
input

Copy
4 1 2
1 2 1 1
output

Copy
0
input

Copy
4 1 1
1 1 2 1
output

Copy
2
Note

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);
}
}
B. Black Square
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input

Copy
5 4
WWWW
WWWB
WWWB
WWBB
WWWW
output

Copy
5
input

Copy
1 2
BB
output

Copy
-1
input

Copy
3 3
WWW
WWW
WWW
output

Copy
1
Note

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);
}
}
D. High Load
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input

Copy
3 2
output

Copy
2
1 2
2 3
input

Copy
5 3
output

Copy
3
1 2
2 3
3 4
3 5
Note

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)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Flak快速上手

    本文介绍如何上手 Flask . 这里假定你已经安装好了 Flask ,否则请先阅读< 安装>. 如果已安装好Flask,通过以下命令查看 一个简单的例子: from flask impo ...

  2. ID3算法Java实现

    ID3算法java实现 1 ID3算法概述 1.1 信息熵 熵是无序性(或不确定性)的度量指标.假如事件A的全概率划分是(A1,A2,...,An),每部分发生的概率是(p1,p2,...,pn).那 ...

  3. 重装系统后恢复wubi安装的Ubuntu(未实测)

     wubi安装成功,但是后来windows系统重装了,如何修复ubuntu系统的引导?[另外完全可以复制别人的wubi安装的ubuntu,但是要放在同一个盘符下]  将X:/ubuntu/winboo ...

  4. HBase——完全分布

    实际上,在真实环境中你需要使用完全分布配置完整测试HBase.在一个分布式配置中,集群有多个节点,每个节点运行一个或多个HBase守护进程.其中包括主Master和备份Master实例,多个Zooke ...

  5. 介绍JSON

    0x00 介绍JSON 介绍JSON :http://www.json.org/json-zh.html Introducing JSON :http://www.json.org/

  6. Pandoc PDF 中文

    最近终于又决定(^_^)使用reStructuredText写文档了,输出PDF时的中文问题必须要解决下. 安装环境 sudo apt install texlive texlive-latex-ex ...

  7. java中的双重锁定检查(Double Check Lock)

    原文:http://www.infoq.com/cn/articles/double-checked-locking-with-delay-initialization#theCommentsSect ...

  8. 04 redis list结构及命令详解

    一:link 链表结构 lpush key value 作用: 把值插入到链接头部[右边] 注意:rpush key value 插入到左边 rpop key 作用: 返回并删除链表尾元素 rpush ...

  9. Yii2 跨库orm实现

    近期在对公司的Yii2项目进行子系统拆分,过度阶段难免会有一些跨库操作,原生语句还好,加下库名前缀就可以了,可是到了orm问题就来了,特别是用到model做查询的时候,现在来记录一下跳过的坑, 像下面 ...

  10. UIWebView使用中的内存相关问题

    本文转载至: http://blog.csdn.net/musou_ldns/article/details/7675589   applicationios5webkit测试cacheios 在iO ...