码迷,mamicode.com
首页 > 其他好文 > 详细

【贪心专题】HDU 1800 Flying to the Mars (寻找最大重复元素) && HDU 2124 Repair the Wall (贪心)

时间:2015-04-07 15:36:23      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:贪心   acm   

链接:click here~~

题意:

         有n个士兵每个人有一个水平值,水平高的的人可以教低的人,意思就是求最合适的组合使花费最小
【解题思路】

刚看到此题,竟没有思路。。想 了一会,其实找到最大重复元素的次数即可,因为相同的人肯定不能共用一个,所以求得最少即为最大的重复次数,跟前面一道题差不多,做完看了别人思路,发现用map容器来做很方便:map容器的内部是一个红黑树,我们是在对它的叶节点进行操作,一共有两个数,第二个数是作为计数用的。

代码:

#include <stdio.h>
#include <map>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=3005;
int magic[maxn];
int main()
{
    int t,a,b,n,m,i,j;
    while(~scanf("%d",&t)){
        int maxx=0;
         map<int ,int >aa;
        for(i=0; i<t; i++){
          scanf("%d",&a);
         aa[a]++;
          if(aa[a]>maxx)
          maxx=aa[a];
        }
        if(t==0) puts("0");
     else printf("%d\n",maxx);
    }
    return 0;
}
链接:click here~~

题意:用木头来修墙,求用的最少数量,记住木头还可以距断的。所以只有当所有木头的总SIZE和小于墙时才是impossible,其他就从大到小求和,直到Len>=墙就跳出!

比较简单~~很奇怪用sort就会WA?

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include<algorithm>
using namespace std;
int blocks[10000];
int main()
{
    int l,n;
    int sum;
    while(scanf("%d%d",&l,&n)!=EOF)
    {
        sum=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&blocks[i]);
            if(sum<l)
                sum+=blocks[i];
        }
        if(sum<l)
        {
            printf("impossible\n");
            continue;
        }
        //sort(blocks,blocks+n);
        for(int x=1; x<n; x++)
            for(int y=x+1; y<=n; y++)
            {
                if(blocks[y]>blocks[x])
                swap(blocks[y],blocks[x]);
            }
        sum=0;
        for(int j=1; j<=n; j++)
        {
            sum+=blocks[j];
            if(sum>=l)
            {
                printf("%d\n",j);
                break;
            }
        }
    }
    return 0;
}


【贪心专题】HDU 1800 Flying to the Mars (寻找最大重复元素) && HDU 2124 Repair the Wall (贪心)

标签:贪心   acm   

原文地址:http://blog.csdn.net/u013050857/article/details/44920547

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!