qduoj 195 Interesting Game

来源:互联网 发布:海绵城市计算软件 编辑:程序博客网 时间:2024/05/16 14:50

简单的贪心策略。
首先把所有兔子按照血量从大到小排序,依次解决。
如果当前解决的兔子血量为x,那么就从所有攻击力大于等于x的箭中取出价格最低的一个,用来消灭这只兔子。
注意,已经被使用的箭在之后不可再次被取出。
用一个小根堆来维护攻击力大于等于x的箭即可。

#include <queue>#include <cstdio>#include <algorithm>const int mxn = 50005;int n, m, b[mxn];struct data {  int d, p;  inline bool operator < (const data &that) const {    return d > that.d;  }} a[mxn];std::priority_queue< int, std::vector<int>, std::greater<int> > heap;signed main() {  scanf("%d%d", &n, &m);  for (int i = 1; i <= n; ++i)    scanf("%d", b + i);  for (int i = 1; i <= m; ++i)    scanf("%d%d", &a[i].d, &a[i].p);  std::sort(b + 1, b + n + 1);  std::sort(a + 1, a + m + 1);  int tot = 1;  long long ans = 0;  for (int i = n; i >= 1; --i) {    while (tot <= m && a[tot].d >= b[i])      heap.push(a[tot++].p);    if (heap.empty())      return puts("No Solution");    else      ans += heap.top(), heap.pop();  }  printf("%lld\n", ans);  return 0;}
原创粉丝点击