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 ...
随机推荐
- npm run watch-poll 监控css、js 文件更新
后台执行npm run watch-poll 你可以在执行命令的后面接一个&命令就会在后台运行了.完整命令:npm run watch-poll & 就是任务号 文章来源:刘俊涛的博客 ...
- TCP/IP详解 卷一(第二章 链路层)
在TCP/IP协议族中,链路层主要有三个目的: 1.为IP模块发送和接收IP数据报 2.为ARP模块发送ARP请求和接收ARP应答 3.为RARP请求和接收RARP应答 TCP/IP支持多种不同的链路 ...
- css样式控制元素固定在底部
回复固定在底部:css样式用到了 box-sizing属性 box-sizing:border-box; -moz-box-sizing:border-box; /* Firefox */ -webk ...
- .net mvc项目 ajax
经常在后台用一般处理程序(.ashx)来处理前台的ajax请求 using System; using System.Collections.Generic; using System.IO; usi ...
- 微信开源组件WCDB漫谈及Demo
代码地址如下:http://www.demodashi.com/demo/12422.html 前言 移动端的数据库选型一直是一个难题,直到前段时间看到了WeMobileDev(微信前端团队)放出了第 ...
- mBot试用体验
[Arduino话题] [mBot试用体验]1.mBot开箱体验(部分资料合集)http://bbs.elecfans.com/forum.php?mod=viewthread&tid=532 ...
- Nonblocking Memory Refresh&2018ISCA/Security& 非阻塞内存刷新
Abstract 我们提议的非阻塞刷新工作是一次刷新内存块中的一部分数据,并在内存块中使用冗余数据,如RS码,在块中计算块的刷新/不可读数据以满足读取请求.作为概念的证明,我们将非阻塞刷新应用于服务器 ...
- apt-mirror 校验错误文件处理
apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...
- HDFS源码分析心跳汇报之周期性心跳
HDFS源码分析心跳汇报之周期性心跳,近期推出!
- Android模糊效果总结
1. 软件模糊 用软件的方法.利用cpu计算,无sdk版本号要求. 效果图: 关键模糊代码 github链接 原文 链接 译文 链接 演示样例 代码 本文地址 :http://blog.csdn.ne ...