1. 程式人生 > >hdu_2124 Flying to the Mars & hdu_1800 Repair the Wall 貪心水題

hdu_2124 Flying to the Mars & hdu_1800 Repair the Wall 貪心水題

bool clu 訪問 eve iostream ios return () The

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;
}

hdu_2124 Flying to the Mars & hdu_1800 Repair the Wall 貪心水題