貪心 之 hdu 4864
阿新 • • 發佈:2019-01-22
// [7/23/2014 Sjm] /* 又坑在TLE上了。。。。 Each machine can only complete a task one day. Each task can only be completed by one machine.所以想到了貪心。。。 發現 dollars = 500*xi+2*yi, 由於 xi(0<xi<1440),yi(0=<yi<=100), 500*1 > 2*100,所以 dollars 的決定因素是需要的時間, 那麼就儘可能先解決需要時間多的任務,同時選擇的時間和難度水平儘可能接近任務的機器,如此即可求得最優解。 不過,先前寫的程式碼一直TLE,後來用 Judge[101][1441] 做一個預處理,AC。。。。*/
#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef __int64 int64; const int MAX = 100005; int N, M, len; struct node { int time; int lev; }; bool Cmp(const node n1, const node n2) { if (n1.time == n2.time) { return n1.lev > n2.lev; } else { return n1.time > n2.time; } } node task[MAX]; int Judge[101][1441]; void Solve() { int num = 0; int64 money = 0; for (int i = 0; i < M; ++i) { for (int j = task[i].lev; j <= 100; ++j) { int k; for (k = task[i].time; k <= 1440; ++k) { if (Judge[j][k]) { Judge[j][k]--; num++; money += (500 * task[i].time + 2 * task[i].lev); break; } } if (k != 1441) { break; } } } printf("%d %I64d\n", num, money); } int main() { //freopen("input.txt", "r", stdin); while (~scanf("%d %d", &N, &M)) { memset(Judge, 0, sizeof(Judge)); int x, y; for (int i = 0; i < N; ++i) { scanf("%d %d", &x, &y); Judge[y][x]++; } for (int i = 0; i < M; ++i) { scanf("%d %d", &task[i].time, &task[i].lev); } sort(task, task + M, Cmp); Solve(); } return 0; }