1. 程式人生 > >HDU 5281【貪心】

HDU 5281【貪心】

\n clu define AC n) string for eve typedef

用最牛逼的槍打最弱的怪物,先給槍和怪物排個序。

#include <iostream>
#include <queue>
#include <algorithm>
#include<cstring>
#include<cstdio>
typedef long long ll;
using namespace std;
#define MAX 100000+10
ll gun[MAX];
ll monster[MAX];
int n, m;
int main(void) {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &gun[i]);
        }
        sort(gun + 1, gun + 1 + n);
        reverse(gun + 1, gun + 1 + n);  //STL的騷操作

        for (int i = 1; i <= m; i++) {
            scanf("%lld", &monster[i]);
        }
        ll sum = 0;
        sort(monster + 1, monster + 1 + m);
        if (n >= m) {
            for (int i = 1; i <= m; i++) {
                if (gun[i] > monster[i]) {
                    sum += gun[i] - monster[i];
                }
                else {
                    break;

                }
            }
            printf("%lld\n", sum);
        }
        else {
            for (int i = 1; i <= n; i++) {
                if (gun[i] > monster[i]) {
                    sum += gun[i] - monster[i];
                }
                else {
                    break;

                }
            }
            printf("%lld\n", sum);

        }



    }
    return 0;

}

HDU 5281【貪心】