zoukankan      html  css  js  c++  java
  • X国的军队

    这里写图片描述
    这里写图片描述
    这里写图片描述

    贪心。
    要使得人数最小,必须使最后剩下活着的人最少。
    那就按照b-a(剩下人数)降序排序

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    #include<algorithm>
    #define LL long long
    #define N 100009
    using namespace std;
    int T,n;
    struct H{
        int a,b,p;
    }K[N];
    LL ans,last;
    bool cmp(H x,H y)
    {
        return x.p>y.p; 
    }
    inline int read()
    {
        int X=0,w=1; char ch=0;
        while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}
        while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
        return X*w;
    }
    int main()
    {
        scanf("%d",&T);
        while(T--)
        {
            ans=0;last=0;
            n=read();
            for(int i=1;i<=n;i++)
            {
                K[i].a=read();K[i].b=read();
                K[i].p=K[i].b-K[i].a;
            }
            sort(K+1,K+n+1,cmp);
            for(int i=1;i<=n;i++)
            {
                if(last<K[i].b) 
                {
                    ans+=K[i].b-last;
                    last=K[i].b-K[i].a;
                }
                else 
                {
                    last-=K[i].a;
                }
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    关于centos防火墙
    linux基础命令
    mysql经典语句
    异常处理
    抽象类
    特性-多态
    特性-继承
    特性-封装
    python模块/文件/日期时间
    python函数3-函数嵌套/递归/匿名函数
  • 原文地址:https://www.cnblogs.com/dfsac/p/7587797.html
Copyright © 2011-2022 走看看