hdu_2124 Flying to the Mars & hdu_1800 Repair the Wall 贪心水题

hdu_1800

简单排一下序,从大开始把比他小的都访问一遍,ans++;

#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

struct dat
{
    int level;
    int visit;
}data[3200];

bool cmp(dat a, dat b)
{
    return a.level > b.level;

}

int main()
{
    int n;

    while(scanf("%d", &n)!=-1)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d", &data[i].level);
            data[i].visit=0;
        }

        sort(data, data+n, cmp);

        int ans=0;
        for(int i=0; i<n; i++)
        {
            if(!data[i].visit)
            {
                data[i].visit=1;
                int temp=i;
                ans++;
                for(int j=0; j<n; j++)
                {
                    if(!data[j].visit && data[temp].level>data[j].level)
                    {
                        data[j].visit=1;
                        temp=j;

                    }
                }
            }
        }
        printf("%d\n", ans);
    }

    return 0;
}


hdu_2124

排个序,用 l 从大开始减去block的大小,记录用的block块数, 最后 l > 0 , 则输出impossible,否则输出块数。

#include <iostream>
#include <stdio.h>
#include <algorithm>

using namespace std;

bool cmp(int a, int b)
{
    return a>b;
}

int main()
{
    int l, n, block[1000];
    while(scanf("%d%d", &l, &n)!=-1)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d", &block[i]);
        }
        sort(block, block+n, cmp);

        int ans=0;
        for(int i=0; i<n && l!=0; i++)
        {
            if(l>=block[i])
            {
                l-=block[i];
                ans++;
            }
            else
            {
                l=0;
                ans++;
                break;
            }
        }
        if(l!=0)
            printf("impossible\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Dawn-bin/p/10801743.html

时间: 2024-10-17 15:07:08

hdu_2124 Flying to the Mars & hdu_1800 Repair the Wall 贪心水题的相关文章

hdu1800 Flying to the Mars(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14340    Accepted Submission(s): 4572 Problem Description In the year 8888

hdu---(1800)Flying to the Mars(trie树)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11228    Accepted Submission(s): 3619 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1800 Flying to the Mars (水题)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11099    Accepted Submission(s): 3572 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1800 Flying to the Mars(字典树)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12767    Accepted Submission(s): 4048 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popu

HDU 1800:Flying to the Mars

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10817    Accepted Submission(s): 3469 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popu

杭电 HDU ACM 1800 Flying to the Mars

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12471    Accepted Submission(s): 3963 Problem Description In the year 8888,  the Earth is ruled by the PPF Empire . As the pop

杭电 1800 Flying to the Mars(贪心)

http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10798    Accepted Submission(s): 3461 Problem Description In the year 8888, the

hdu 1800 Flying to the Mars(水 ,贪心)

其实就是求最大的相同的数的多少.. 我是把它当字符串输入..解决前导0的问题.. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> using namespace std; int main() { char s[35]; int w[3500]; __int64 qq[3500]; int a; while(~scanf("%d",&a

hdu 1800 Flying to the Mars(简单模拟,string,字符串)

题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于string的修改增加的用法 #include <cstdio> #include<iostream> #include <cstring> #include <algorithm> #include<string> using namespace std