/* 返回顶部 */

Luogu P2107 小Z的AK计划

gate

贪心,按(距离+时间)排序。

走一个选一个,若i的距离+(1-i)的时间 > m则不能选了。

记得开long long!

代码如下

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define MogeKo qwq
using namespace std;

const int maxn = 1e5+10;
long long n,m,now,ans;

struct node {
    long long x,t;
    bool operator < (const node & N)const {
        return x+t < N.x+N.t;
    }
} a[maxn];

int main() {
    scanf("%lld%lld",&n,&m);
    for(int i = 1; i <= n; i++)
        scanf("%lld%lld",&a[i].x,&a[i].t);
    sort(a+1,a+n+1);
    for(int i = 1; i <= n; i++) {
        now += a[i].t;
        if(now + a[i].x > m) break;
        ans++;
    }
    printf("%lld",ans);
    return 0;
}
View Code

 

posted @ 2019-11-12 17:19  Mogeko  阅读(153)  评论(0编辑  收藏  举报