GYM 101149 B.No Time for Dragons(贪心)

news/2024/7/16 9:36:26

Description
n场战争,每场战争需要派a[i]名士兵去,幸存者b[i]人,问如何安排这些战争能够使得最后派出的总人数最少(一名士兵可以参加多次战争,但其只被派出了一次)
Input
第一行一整数n表示战争数量,之后n行每行两个整数a[i]和b[i]分别表示派出的人数和幸存的人数(1<=n<=2e5,1<=b[i]<=a[i]<=1e9)
Output
输出最少需要派出的人数
Sample Input
2
7 4
5 1
Sample Output
8
Solution
贪心,因为战死的人数是一定的,肯定要先开始那些损失数少的战争,这样多活下来的士兵可以参加更多的战争,故对a[i]-b[i]升序排即为最佳安排
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 222222
int n;
struct node
{
    int a,b;
    bool operator<(const node&c)const
    {
        if(a-b!=c.a-c.b)return a-b>c.a-c.b;
        return a<c.a;
    }
}p[maxn];
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)scanf("%d%d",&p[i].a,&p[i].b);
        sort(p,p+n);
        ll ans=p[0].a,pre=p[0].a-p[0].b;
        for(int i=1;i<n;i++)
        {
            if(pre<p[i].a)ans+=p[i].a-pre,pre=p[i].a;
            pre-=p[i].b;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

http://www.niftyadmin.cn/n/3301769.html

相关文章

delphi按字节长度分割字符串函数(转)

此字符串分割函数用delphi编写&#xff0c;可以适应字符串中存在双字节字符和单字节字符。 function TricheditEfm.SplitString(source:string;Sleng:Integer):TStringlist;stdcall; //字符串分割函数 ,按字符串长度分割 var copycount,i:Integer; …

UEFI实战

本篇为UEFI实战系列第一部分。UEFI实战前10个部分计划如下&#xff1a;UEFI 实战(1) 开发环境 讲述如何配置开发环境。UEFI 实战(2) HelloWorld讲述dsc, inf文件的格式&#xff0c; application常用的变量&#xff0c;数据结构和函数。UEFI 实战(3) C讲述如何用C开发UEFI程序…

将一个double类型的小数,按照四舍五入保留两位小数.

package come.one01; public class One02 { public static void main(String[] args) { double numa 3.1415926; // 调用df.format(num1)方法&#xff0c;传入参数 System.out.println(String.format("%.2f", numa));// %.2f %. 表示 小数点前任意位数 2 表示两位小…

Delphi 判断特定字符是为单字节还是双字节

判断特定字符是为单字节还是双字节 // mbSingleByte 单字节字符 //mbLeadByte 双字节字符首字节 //mbTrailByte 双字节字符尾字节Edit1.Text:0102030405060708我1112131415;n:Length(WideString(Edit1.Text));ShowMessage(IntToStr(n));if ByteType(Edit1.Text,17)mbLeadByte…

GYM 101149 C.Mathematical Field of Experiments(水~)

Description 给出一个素数 p&#xff0c;问0~p-1模p的二次剩余 Input 一个素数p(2<p<1e6) Output 输出p个数分别表示0~p-1模p的二次剩余&#xff0c;如果不是p的二次剩余则输出-1 Sample Input 5 Sample Output 0 4 -1 -1 3 Solution i是i*i%p模p的二次剩余&a…

python发布中的几个细节

按照《Head First Python》书中的说明&#xff0c;已经建立好了一个nester的文件夹&#xff0c;且其中包括一个nester.py和setup.py两个python文件&#xff0c;剩下的是如何操作发布&#xff0c;要注意以下几点&#xff1a; 1.一定要到nester该文件夹的路径来操作后面的发布命…

面试常见算法问题

1. KMP算法 https://blog.csdn.net/v_july_v/article/details/7041827 next数组讲解&#xff1a;https://www.cnblogs.com/tangzhengyue/p/4315393.html 2. 批标准化 https://www.cnblogs.com/guoyaohua/p/8724433.html 转载于:https://www.cnblogs.com/sclczk/p/11079760.html…

Delphi读取和写入utf-8编码格式的文件

读取UTF-8格式的文件内容 function LoadUTF8File(AFileName: string): string; var ffileStream:TFileStream; fAnsiBytes: string;S: string; beginffileStream:TFileStream.Create(AFileName,fmOpenRead);SetLength(S,ffileStream.Size); ffileStream.Read(S[1],Length(S));…