zoukankan      html  css  js  c++  java
  • 水题都切不动了

    每日一练 ——反思(明明好简单的思路) 我却想不出来 是我想复杂了还是。。。

    题目

    A - Bear and Elections
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    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 get ai 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 Input

    Input
    5
    5 1 11 2 8
    Output
    4
    Input
    4
    1 8 8 8
    Output
    6
    Input
    2
    7 6
    Output
    0

    Hint

    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.

    猪脚想获得最多的选票 减少对手的选票 花最少的糖 so每次减少对手(票最多的)一票 自己增加一票。

    下面是思路错误wrong代码

    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    int a[200];
    bool cmp(int a,int b)
    {
      return a>b;
    }
    int main(){
        int n;
        while(scanf("%d",&n)!=EOF){
            memset(a,0,sizeof(a));
            for(int i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            int sum=0,mk=0;
            a[n+1]=0; 
            std::sort(a+2,a+n+1,cmp);
            for(int i=1;i<=n;i++)
            {
                sum+=a[i];
                if((sum/i+sum%i)>a[i+1]){
                    mk=(sum/i+sum%i)-a[1];
                    break;
                } 
                else{
                    if((sum/i+sum%i)==a[i+1])
                    {    
                        mk=(sum/i+sum%i)-a[1]+1;break;
                    }
                } 
            }
            printf("%d
    ",mk); 
        }
    }

    AC代码 虽然跑得慢。。。

    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    int a[200];
    bool cmp(int a,int b)
    {
      return a>b;
    }
    int main(){
    	int n;
    	while(scanf("%d",&n)!=EOF){
    		memset(a,0,sizeof(a));
    		for(int i=1;i<=n;i++){
    			scanf("%d",&a[i]);
    		}
    		int mk=0; 
    		std::sort(a+2,a+n+1,cmp);
    		while(a[1]<=a[2])
    		{
    			a[2]--;
    			a[1]++;
    			std::sort(a+2,a+n+1,cmp);
    			mk++;
    		}
    		printf("%d
    ",mk); 
    	}
    	return 0;
    }
    

     看下题解。。。

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    
    int n;
    int a[1005];
    
    int main(){
        while(scanf("%d",&n) != EOF){
            priority_queue<int> q;
            for(int i = 1;i <= n;i++) {
                scanf("%d",&a[i]);;
                if(i != 1) q.push(a[i]);
            }
            
            int ans = 0;
            while(1){
                int  x = q.top();q.pop();
            //    printf("x = %d  a[1] = %d
    ",x,a[1]);
                if(a[1] > x ) break;
                x--;
            //    printf("---x = %d 
    ",x);
                a[1]++;
                q.push(x);
                ans++;
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    

     虽然看不懂 但是赶脚好厉害

  • 相关阅读:
    使用 Eclipse 调试 Java 程序的 10 个技巧
    oracle9i,10g再谈优化模式参数问题.
    oracle 索引
    解决IE不能在新窗口中向父窗口的下拉框添加项的问题
    获取文档的尺寸:利用Math.max的另一种方式
    揭开constructor属性的神秘面纱
    测试杂感:Windows8也许需要Account Hub
    探索式测试:探索是为了学习
    一次有教益的程序崩溃调试 (下)
    软件测试读书列表 (2013.8)
  • 原文地址:https://www.cnblogs.com/Geek-xiyang/p/5144785.html
Copyright © 2011-2022 走看看