ZOJ 3519 Who is the Smartest Man(贪心)

题目链接:ZOJ 3519 Who is the Smartest Man

简单的贪心,战斗力比曹操低的什么时候打都行,所以先把战斗力比曹操高的按从小到大排序,一个一个打,碰见比曹操低的先不打,最后把剩下那些战斗力低的打了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX_N = 500 + 50;

bool cmp(int a, int b)
{
    return a < b;
}
int n, k;
int arr[MAX_N];

int main()
{
    while(scanf("%d%d", &n, &k) != EOF)
    {
        int temp, cnt1 = 0, cnt2 = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &temp);
            if(temp <= k)
                cnt1++;
            else
                arr[cnt2++] = temp;
        }
        sort(arr, arr + cnt2, cmp);
        for(int i = 0; i < cnt2; i++)
        {
            if(k < arr[i])
                k += 2;
            else if(k >= arr[i])
                cnt1++;
        }
        k += cnt1;
        printf("%d\n", k);
    }
    return 0;
}


你可能感兴趣的:(ZOJ 3519 Who is the Smartest Man(贪心))