NC53681 「土」巨石滚滚(贪心)

巨石滚滚 https://ac.nowcoder.com/acm/problem/53681

思路

上来感觉是贪心,只不过自己找了个假的贪心策略,一开始按照净回复量=回复量-消耗量大的排序,没过,然后又按照消耗小的排前面,回复量大的排前面都没过。。。
题解:如果可以回血,优先选择消耗最小的,如果回血小于消耗,先选择回血较多的,其他情况先选取净回复量较多的

代码

#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef double dd;
const int N = 5e5+5;
const dd eps = 1e-8;
const int mod = 1e9+7;
 
int t,n;
ll m;
 
struct node{
  ll a,b,c;
}p[N];
 
bool cmp(node p1,node p2){
  if(p1.c >= 0 and p2.c >= 0){
    return p1.a < p2.a;
  }
  else if(p1.c < 0 and p2.c < 0){
    return p1.b > p2.b;
  }
  else return p1.c > p2.c;
}
 
int main(){
  ios::sync_with_stdio(false);
  // freopen("input.txt","r",stdin);
  cin>>t;
  while(t--){
    cin>>n>>m;
    bool haveans = true;
    for(int i = 1;i <= n;i++) {cin>>p[i].a>>p[i].b;p[i].c = p[i].b-p[i].a;}
    sort(p+1,p+1+n,cmp);
    for(int i = 1;i <= n;i++){
      m -= p[i].a;
      if(m < 0){haveans = false;break;}
      m += p[i].b;
    }
    if(haveans) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
  }
  return 0;
}
原文地址:https://www.cnblogs.com/all-taker/p/12906917.html