zoukankan      html  css  js  c++  java
  • (中)Educational Codeforces Round 18 E题Colored Balls(简单的数学)解题报告

    E. Colored Balls
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are n boxes with colored balls on the table. Colors are numbered from 1 to ni-th box contains ai balls, all of which have color i. You have to write a program that will divide all balls into sets such that:

    • each ball belongs to exactly one of the sets,
    • there are no empty sets,
    • there is no set containing two (or more) balls of different colors (each set contains only balls of one color),
    • there are no two sets such that the difference between their sizes is greater than 1.

    Print the minimum possible number of sets.

    Input

    The first line contains one integer number n (1 ≤ n ≤ 500).

    The second line contains n integer numbers a1, a2, ... , an (1 ≤ ai ≤ 109).

    Output

    Print one integer number — the minimum possible number of sets.

    Examples
    input
    3
    4 7 8
    output
    5
    input
    2
    2 7
    output
    4
    Note

    In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.

     

    这场educational round 真的学到很多……C题代码极易有缺陷,FST了无数人,D、E都非常简单,但C题这一关真的需要细想才能过去……

    将数据排序之后依照将最小的那个分成的个数的情况来挨个看。

    [个数*(x-1),个数*x]区间内的数都能取得,x为符合“个数*x">=a[1]的最小的x。

    (注意如果a[1]%x==0时,令x+1为x对于a[1]也没有问题,所以这时x+1也要判断下)

    之后再用这个x对后面的下标2——n的数进行检验。检验方式依旧是看a[i]是否在那个区间里,不过,这时是x固定,求个数。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 typedef long long ll;
     8 typedef unsigned long long ull;
     9 using namespace std;
    10 const int MAX=505;
    11 int a[MAX];
    12 int n;
    13 ll an,tem;
    14 int da;
    15 int check(int fen,int shu)
    16 {
    17     int ge=(shu+fen-1)/fen;
    18     if(ge*(fen-1)<=shu)
    19         return ge;
    20     return 0;
    21 }
    22 ll check2(int da)
    23 {
    24     ll re=0;
    25     int tem;
    26     for(int i=2;i<=n;i++)
    27     {
    28         tem=check(da,a[i]);
    29         if(tem==0)
    30             return -1;
    31         else
    32             re+=tem;
    33     }
    34     return re;
    35 }
    36 int main()
    37 {
    38     scanf("%d",&n);
    39     for(int i=1;i<=n;i++)
    40         scanf("%d",&a[i]);
    41     sort(a+1,a+1+n);
    42     for(int i=1;i<=sqrt(a[1]);i++)
    43     {
    44         da=(a[1]+i-1)/i;
    45         if(a[1]%da==0)
    46         {
    47             da++;
    48             an=check(da,a[1]);
    49             tem=check2(da);
    50             if(tem!=-1)
    51             {
    52                 an+=tem;
    53                 cout<<an<<"
    ";
    54                 return 0;
    55             }
    56             da--;
    57         }
    58             an=check(da,a[1]);
    59             tem=check2(da);
    60             if(tem!=-1)
    61             {
    62                 an+=tem;
    63                 cout<<an<<"
    ";
    64                 return 0;
    65             }
    66     }
    67 }
    E. Colored Balls
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    There are n boxes with colored balls on the table. Colors are numbered from 1 to ni-th box contains ai balls, all of which have color i. You have to write a program that will divide all balls into sets such that:

    • each ball belongs to exactly one of the sets,
    • there are no empty sets,
    • there is no set containing two (or more) balls of different colors (each set contains only balls of one color),
    • there are no two sets such that the difference between their sizes is greater than 1.

    Print the minimum possible number of sets.

    Input

    The first line contains one integer number n (1 ≤ n ≤ 500).

    The second line contains n integer numbers a1, a2, ... , an (1 ≤ ai ≤ 109).

    Output

    Print one integer number — the minimum possible number of sets.

    Examples
    input
    3
    4 7 8
    output
    5
    input
    2
    2 7
    output
    4
    Note

    In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.

     

  • 相关阅读:
    Excel2010表格里设置每页打印时都有表头
    新手常见Python运行时错误
    如何查看某个端口被谁占用
    ubuntu更换阿里源
    c# 值类型与引用类型(转)
    vs2015 企业版 专业版 密钥
    csdn中使用Git的一些注意问题
    在notepad++中快速插入当前时间方法
    EF6 code first 新建项目注意问题
    vs2015新建web应用程序空模板和添加webapi的模板生成文件的比较
  • 原文地址:https://www.cnblogs.com/quintessence/p/6652130.html
Copyright © 2011-2022 走看看