zoukankan      html  css  js  c++  java
  • CF #318 A. Bear and Elections

    A. Bear and Elections
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland.

    There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate would getai votes. Limak is candidate number 1. To win in elections, he must get strictly more votes than any other candidate.

    Victory is more important than everything else so Limak decided to cheat. He will steal votes from his opponents by bribing some citizens. To bribe a citizen, Limak must give him or her one candy - citizens are bears and bears like candies. Limak doesn't have many candies and wonders - how many citizens does he have to bribe?

    Input

    The first line contains single integer n (2 ≤ n ≤ 100) - number of candidates.

    The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000) - number of votes for each candidate. Limak is candidate number 1.

    Note that after bribing number of votes for some candidate might be zero or might be greater than 1000.

    Output

    Print the minimum number of citizens Limak must bribe to have strictly more votes than any other candidate.

    Sample test(s)
    input
    5
    5 1 11 2 8
    output
    4
    input
    4
    1 8 8 8
    output
    6
    input
    2
    7 6
    output
    0
    Note

    In the first sample Limak has 5 votes. One of the ways to achieve victory is to bribe 4 citizens who want to vote for the third candidate. Then numbers of votes would be 9, 1, 7, 2, 8 (Limak would have 9 votes). Alternatively, Limak could steal only 3 votes from the third candidate and 1 vote from the second candidate to get situation 9, 0, 8, 2, 8.

    In the second sample Limak will steal 2 votes from each candidate. Situation will be 7, 6, 6, 6.

    In the third sample Limak is a winner without bribing any citizen.

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int n;
     9     int a[1005];
    10     int i,j,k;
    11     scanf("%d",&n);
    12     for(i=0;i<n;i++)
    13         scanf("%d",&a[i]);
    14     for(k=0;k<=1000;k++)
    15     {
    16         int x=a[0]+k,y=k;
    17         for(i=1;i<n;i++)
    18         {
    19             if(a[i]>=x)
    20                 y=y-(a[i]-x+1);
    21             if(y<0)
    22                 break;
    23         }
    24         if(y>=0)
    25             break;
    26     }
    27     printf("%d
    ",k);
    28     return 0;
    29 }
    View Code
  • 相关阅读:
    SVN错误:Attempted to lock an already-locked dir
    DecimalFormat 中的 # 与 0 的区别(中文帮助文档中翻译可能是错误的)
    Logger.getLogger和LogFactory.getLog的区别
    在做excel导出时如何将excel直接写在输出流中
    10 -- 深入使用Spring -- 5... 实现任务的自动调度
    8 -- 深入使用Spring -- 8...2 管理Hibernate的SessionFactory
    8 -- 深入使用Spring -- 8...1 Spring提供的DAO支持
    8 -- 深入使用Spring -- 8... Spring整合Hibernate
    8 -- 深入使用Spring -- 7...4 使用自动装配
    8 -- 深入使用Spring -- 7...3 让Spring管理控制器
  • 原文地址:https://www.cnblogs.com/cyd308/p/4770797.html
Copyright © 2011-2022 走看看