zoukankan      html  css  js  c++  java
  • Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals)

    题目链接: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代表有一个空位的双人桌。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <string>
     7 #include <vector>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 typedef long long LL;
    15 const double eps = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 9e18;
    18 const int MOD = 1e9+7;
    19 const int MAXN = 2e5+10;
    20 
    21 int main()
    22 {
    23     int n, a, b;
    24     while(scanf("%d%d%d",&n,&a,&b)!=EOF)
    25     {
    26         int sum = 0, c = 0;
    27         for(int i = 1; i<=n; i++)
    28         {
    29             int x;
    30             scanf("%d",&x);
    31             if(x==1)
    32             {
    33                 if(a) a--;
    34                 else if(b) b--, c++;
    35                 else if(c) c--;
    36                 else sum++;
    37             }
    38             else
    39             {
    40                 if(b) b--;
    41                 else sum += 2;
    42             }
    43         }
    44         printf("%d
    ", sum);
    45     }
    46 }
    View Code
    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个。

    写法一:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <string>
     7 #include <vector>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 typedef long long LL;
    15 const double eps = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 9e18;
    18 const int MOD = 1e9+7;
    19 const int MAXN = 200+10;
    20 
    21 char  a[MAXN][MAXN];
    22 int sum[MAXN][MAXN];
    23 int main()
    24 {
    25     int n,m;
    26     while(scanf("%d%d",&n,&m)!=EOF)
    27     {
    28         for(int i = 1; i<=n; i++)
    29             scanf("%s", a[i]+1);
    30 
    31         memset(sum, 0, sizeof(sum));
    32         for(int i = 1; i<=n; i++)
    33         for(int j = 1; j<=m; j++)
    34             sum[i][j] = sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(a[i][j]=='B');
    35 
    36         int ans = INF;
    37         for(int len = 1; len<=min(n,m); len++)
    38         {
    39             for(int i = len; i<=n; i++)
    40             for(int j = len; j<=m; j++)
    41             {
    42                 int black_cnt = sum[i][j]-sum[i][j-len]-sum[i-len][j]+sum[i-len][j-len];
    43                 if(black_cnt==sum[n][m])
    44                     ans = min(ans, len*len-black_cnt);
    45             }
    46         }
    47         printf("%d
    ", ans==INF?-1:ans);
    48     }
    49 }
    View Code

    写法二:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <string>
     7 #include <vector>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 typedef long long LL;
    15 const double eps = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 9e18;
    18 const int MOD = 1e9+7;
    19 const int MAXN = 200+10;
    20 
    21 char  a[MAXN][MAXN];
    22 int main()
    23 {
    24     int n,m;
    25     while(scanf("%d%d",&n,&m)!=EOF)
    26     {
    27         int cnt = 0, ans = -1;
    28         int u = INF, d = -INF, l = INF, r = -INF;
    29         for(int i = 1; i<=n; i++)
    30         {
    31             scanf("%s", a[i]+1);
    32             for(int j = 1; j<=m; j++)
    33             if(a[i][j]=='B')
    34             {
    35                 cnt++;
    36                 u = min(u,i);
    37                 d = max(d,i);
    38                 l = min(l,j);
    39                 r = max(r,j);
    40             }
    41         }
    42 
    43         if(u==INF)
    44             ans = 1;
    45         else if((r-l<d-u&&d-u+1>m)||(d-u<r-l&&r-l+1>n))
    46             ans = -1;
    47         else
    48             ans = max(r-l+1,d-u+1)*max(r-l+1,d-u+1)-cnt;
    49         printf("%d
    ", ans==INF?-1:ans);
    50     }
    51 }
    View Code
    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个叶子结点和一个根节点固定起来,为了最长叶子距离最短,我们需要做的就是尽可能把剩下的点都均匀地分到每一个叶子结点的路径上。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <string>
     7 #include <vector>
     8 #include <map>
     9 #include <set>
    10 #include <queue>
    11 #include <sstream>
    12 #include <algorithm>
    13 using namespace std;
    14 typedef long long LL;
    15 const double eps = 1e-8;
    16 const int INF = 2e9;
    17 const LL LNF = 9e18;
    18 const int MOD = 1e9+7;
    19 const int MAXN = 2e5+10;
    20 
    21 int fa[MAXN];
    22 int main()
    23 {
    24     int n, k;
    25     while(scanf("%d%d",&n,&k)!=EOF)
    26     {
    27         memset(fa, 0, sizeof(fa));
    28         for(int i = 1; i<=k; i++)
    29             fa[i] = k+1;
    30 
    31         int p = k+2, t = 1;
    32         while(p<=n)
    33         {
    34             int tmp = fa[t];
    35             fa[t] = p;
    36             fa[p] = tmp;
    37             p++; t = t%k+1;
    38         }
    39 
    40         int len = (n-k-1)/k*2;
    41         if((n-k-1)%k==1) len++;
    42         else if((n-k-1)%k>1) len += 2;
    43         len += 2;
    44         printf("%d
    ", len);
    45         for(int i = 1; i<=k; i++) printf("%d %d
    ", i, fa[i]);
    46         for(int i = k+2; i<=n; i++) printf("%d %d
    ", i, fa[i]);
    47     }
    48 }
    View Code
  • 相关阅读:
    2021年7月国产数据库排行榜:openGauss成绩依旧亮眼,Kingbase向Top 10发起冲刺
    在SQL SERVER 中通过ADSI接口查询活动目录AD信息!
    PowerPivot作为多维数据源在PerformancePoint Services的使用!
    自助式微软BI工具PowerPivot入门篇(一)!
    一种在PowerPivot客户端中把SharePoint的PowerPivot作为数据源的方法!
    自助式微软BI工具PowerPivot使用介绍!
    利用开源SharePoint Permission Extension插件对SharePoint 的列表进行权限控制!
    使用SQL Server 集成服务中结合SharePoint的客户端模型进行SharePoint列表的同步实践!
    在SQL Server 集成服务中使用开源SharePoint List组件对SharePoint列表进行ETL操作!
    自助式微软BI工具PowerPivot入门篇(二)!
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8909572.html
Copyright © 2011-2022 走看看