洛谷 4823 [TJOI2013]拯救小矮人
题目链接-> 噔楞
题解:
贪心
按个高+臂长排序。
个矮臂长的先走,个高臂短的后走
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 2007 using namespace std; int f[N], n, flag, now, ans; struct node{ int x, y; }edge[N << 1]; bool cmp(node x, node y){ return x.x + x.y > y.x + y.y; } int main(){ scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d%d", &edge[i].x, &edge[i].y), now += edge[i].x; scanf("%d", &flag); sort(edge + 1, edge + 1 + n, cmp); memset(f, -1, sizeof(f)); f[0] = now; for(int i = n; i >= 1; i--){ for(int j = ans; j >= 0; j--) if(f[j] + edge[i].y >= flag) f[j + 1] = max(f[j + 1], f[j] - edge[i].x); if(f[ans + 1] >= 0) ans++; } printf("%d", ans); return 0; }
一世安宁