Codeforces Round #273 (Div. 2)

A. Initial Bet
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are five people playing a game called "Generosity". Each person gives some non-zero number of coins b as an initial bet. After all players make their bets of b coins, the following operation is repeated for several times: a coin is passed from one player to some other player.

Your task is to write a program that can, given the number of coins each player has at the end of the game, determine the size b of the initial bet or find out that such outcome of the game cannot be obtained for any positive number of coins b in the initial bet.

Input

The input consists of a single line containing five integers c1, c2, c3, c4 and c5 — the number of coins that the first, second, third, fourth and fifth players respectively have at the end of the game (0 ≤ c1, c2, c3, c4, c5 ≤ 100).

Output

Print the only line containing a single positive integer b — the number of coins in the initial bet of each player. If there is no such value of b, then print the only value "-1" (quotes for clarity).

Sample test(s)
input
2 5 4 0 4
output
3
input
4 5 9 2 1
output
-1
Note

In the first sample the following sequence of operations is possible:

  1. One coin is passed from the fourth player to the second player;
  2. One coin is passed from the fourth player to the fifth player;
  3. One coin is passed from the first player to the third player;

  1. One coin is passed from the fourth player to the second player.

这么水的题目竟然wa了两次才过,主要是题目没看清楚,如果sum=0的话输出不是0,而是-1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1000;
int main()
{

    int a,b,c,d,e;
    while(   cin>>a>>b>>c>>d>>e)
    {
         int sum = 0;
    sum += (a+b+c+d+e);

     if(sum % 5 || sum == 0)  cout<<"-1"<<endl;
    else cout<<sum/5<<endl;
    }
    
    return 0;
}

B. Random Teams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends.

Your task is to write a program that will find the minimum and the maximum number of pairs of friends that could have formed by the end of the competition.

Input

The only line of input contains two integers n and m, separated by a single space (1 ≤ m ≤ n ≤ 109) — the number of participants and the number of teams respectively.

Output

The only line of the output should contain two integers kmin and kmax — the minimum possible number of pairs of friends and the maximum possible number of pairs of friends respectively.

Sample test(s)
input
5 1
output
10 10
input
3 2
output
1 1
input
6 3
output
3 6
Note

In the first sample all the participants get into one team, so there will be exactly ten pairs of friends.

In the second sample at any possible arrangement one team will always have two participants and the other team will always have one participant. Thus, the number of pairs of friends will always be equal to one.

In the third sample minimum number of newly formed friendships can be achieved if participants were split on teams consisting of 2 people, maximum number can be achieved if participants were split on teams of 11 and 4 people.


简单的贪心,不过细节还是要注意下,求最小的时候尽量使得每个房间人数最少
 int n,m;
 while(cin>>n>>m)
 {
     int  temp = n/m;
     long long  ans_min = 0;

         int mod = n%m;
           ans_min = (long long )(temp+1)*temp/2*mod + (long long )(temp)*(temp-1)/2*(m-mod);

     long long ans_max;
     n -= m-1;
     ans_max =  (long long )n*(n-1)/2;
     cout<<ans_min<<" "<<ans_max<<endl;
 }

C. Table Decorations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have r red, g green and b blue balloons. To decorate a single table for the banquet you need exactly three balloons. Three balloons attached to some table shouldn't have the same color. What maximum number t of tables can be decorated if we know number of balloons of each color?

Your task is to write a program that for given values rg and b will find the maximum number t of tables, that can be decorated in the required manner.

Input

The single line contains three integers rg and b (0 ≤ r, g, b ≤ 2·109) — the number of red, green and blue baloons respectively. The numbers are separated by exactly one space.

Output

Print a single integer t — the maximum number of tables that can be decorated in the required manner.

Sample test(s)
input
5 4 3
output
4
input
1 1 1
output
1
input
2 3 3
output
2
Note

In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively.

搞了好久才发现题目读错了,题目要求是每桌三个气球的颜色不能全部一样

给出r,b,g分别代表红色,蓝色,绿色气球的数量,每三个气球可以布置一张桌子,要求同一个桌子上的三个气球颜色不能全一样,求最多能布置多少张桌子

先把r,g,b从小到达排序,假设a<b<c,如果c>=2*(a+b),那么我们不断从c中取出2个,a,b中随便取出一个,则a+b可以全部用完,没有最优的了,这种情况就是a+b种,如果c < 2*(a+b),假设现在只有a,b两种气球,那么我们假有x种rgg,y种rrg,那么有(2x+y) + (2y+x) = a+b,解得x+y = (a+b)/3,那么我们大胆假设如果是三种的话则答案是(a+b+c)/3,其实这是成立的,可以把最少的那种加到中间的那种中变成两种

#include <iostream>
#include <cstdio>
#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    long long a[3];
    scanf("%I64d%I64d%I64d",&a[0],&a[1],&a[2]);
    sort(a,a+3);
    long long ans=0;
    if((a[0]+a[1])<=a[2]/2)
    {
       ans=a[0]+a[1];
    }
    else
    {
        ans=(a[0]+a[1]+a[2])/3;
    }
    printf("%I64d\n",ans);
    return 0;
}

D. Red-Green Towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are r red and g green blocks for construction of the red-green tower. Red-green tower can be built following next rules:

  • Red-green tower is consisting of some number of levels;

  • Let the red-green tower consist of n levels, then the first level of this tower should consist of n blocks, second level — of n - 1 blocks, the third one — of n - 2 blocks, and so on — the last level of such tower should consist of the one block. In other words, each successive level should contain one block less than the previous one;

  • Each level of the red-green tower should contain blocks of the same color.

Let h be the maximum possible number of levels of red-green tower, that can be built out of r red and g green blocks meeting the rules above. The task is to determine how many different red-green towers having h levels can be built out of the available blocks.

Two red-green towers are considered different if there exists some level, that consists of red blocks in the one tower and consists of green blocks in the other tower.

You are to write a program that will find the number of different red-green towers of height h modulo 109 + 7.

Input

The only line of input contains two integers r and g, separated by a single space — the number of available red and green blocks respectively (0 ≤ r, g ≤ 2·105r + g ≥ 1).

Output

Output the only integer — the number of different possible red-green towers of height h modulo 109 + 7.

Sample test(s)
input
4 6
output
2
input
9 7
output
6
input
1 1
output
2
Note

The image in the problem statement shows all possible red-green towers for the first sample.

建造一个有r个红木块,g个绿木块的“红绿塔”。红绿塔的最下层有k个木块,上一层有k-1个木块……直到最上层有一个木块,每层的木块颜色要相同,求这些木块能堆出的最高的“红绿塔”的种数。(r+g个木块不一定全都要用上)

设dp[i][j]为前i层使用了j个红色砖块时的方案数,则dp[i][j]=dp[i-1][j](即新的一层全部为绿色)+dp[i-1][j-i](即新的一层全部为红色),答案为所以dp[h]的和

i最多到900,j最多到2·105,显然数组不能开到这么大,所以需要使用滚动数组(这个地方类似01背包的将二维数组缩小为一维数组)

上面二维可以转换成dp[j] = dp[j] + dp[j-i],如果不懂的可以先去看看01背包那里是如何将二维转一维的

include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#define MOD 1000000007
int dp[200005];
using namespace std;
typedef long long LL;

int main()
{

    int r,g;
    while(cin>>r>>g)
    {
        memset(dp,0,sizeof(dp));
        int h;
        for( h = 1; h*(h+1)/2 <= r+g; h++);//先求最多有多少层
        h--;
        dp[0] = 1;//这代表没有红色方块的1-h层的方案永远只有1种,即全部都是绿色的必定是1种了
        for(int i = 1; i <= h ; i++)
        {
            for(int j = r; j >= 0; j --)
            {
                if(j - i>= 0)
                {
                    dp[j]  = (dp[j]  + dp[j -i])%MOD;
                }
            }
        }
        LL Max = h*(h+1)/2 , ans = 0;
        for(int i = 0; i <= r; i++)
        {
            if(Max - i <= g)     ans = (ans + dp[i])%MOD;//Max-i的意思是里面除去红色的方块总共有多少绿色方块,如果小于g,则该方案不陈立
        }
        cout<<ans<<endl;

    }
    return 0;
}



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值