HDU 4864(Task)

题意:略。

思路:贪心。题目很特别的给出了获得金钱的计算公式为500*x+2*y,y<=100,所以可以按任务时间从大到小排序,

然后每个任务找出大于该任务难度且与难度最接近的机器完成该任务。

#include 
#include 
#include 
#include 
using namespace std;

typedef long long LL;
const int N = 100005;

struct Task {
    int x, y;

    bool operator<(const Task &rhs) const {
        if(x == rhs.x)
            return y > rhs.y;
        return x > rhs.x;
    }
} task[N];

int main() {
    int n, m;
    while(cin >> n >> m) {
        multiset S[105];
        LL ans = 0;
        int maxLevel = 0, count = 0;
        for(int i = 0; i < n; i++) {
            int x, y;
            scanf("%d%d", &x, &y);
            S[y].insert(x);
            maxLevel = max(maxLevel, y);
        }
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &task[i].x, &task[i].y);
        }
        sort(task, task + m);
        for(int i = 0; i < m; i++) {
            int x = task[i].x;
            int y = task[i].y;
            for(int j = y; j <= maxLevel; j++) {
                if(S[j].empty())
                    continue;
                multiset::iterator iter = S[j].lower_bound(x);
                if(iter == S[j].end() || *iter < x) {
                    continue;
                } else {
                    count++;
                    ans += 500ll * x + 2ll * y;
                    S[j].erase(iter);
                    break;
                }
            }
        }
        cout << count << " " << ans << endl;
    }
    return 0;
}


你可能感兴趣的:(ACM,数据结构)