dlut1224题解-贪心及注意事项

来源:互联网 发布:ubuntu默认启动windows 编辑:程序博客网 时间:2024/04/30 09:45

这是我写的贪心第一题,注意:1:这一题的sort对相同力量的要排质量,(因为这个wa了3次,最后给我yy对了哭);2:是注意为N=0的情况;

#include<cstring>#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn=100005;struct node{    int power;    int weight;};node peo[maxn];bool cmp(node a,node b){    if(a.power==b.power)return a.weight<b.weight;    return a.power<b.power;}int main(){    int T;    scanf("%d",&T);    while(T--){        int N;        scanf("%d",&N);        for(int i=0;i<N;i++)            scanf("%d%d",&peo[i].weight,&peo[i].power);        sort(peo,peo+N,cmp);        int sum=0,cnt=0;        for(int i=0;i<N;i++){            if(sum<=peo[i].power){                sum+=peo[i].weight;                cnt++;            }        }        printf("%d\n",cnt);    }    return 0;}


0 0
原创粉丝点击