[JOI2017/2018]美術展

题目大意:

有\(n(n\le5\times10^5)\)个物品,每个物品有两个属性:尺寸\(A_i\)和收益\(B_i\)。从中选取一个子集,总收益为\(\sum B_i-\max\{A_i\}-\min\{A_i\}\)。求总收益最大值。

思路:

将所有物品按照\(A_i\)排序,\(B_i\)前缀和记作\(S_i\)。答案相当于\(\max\{S_i-A_i+A_j-S_{j-1}\}\)。维护\(A_j-S_{j-1}\)前缀\(\max\)即可。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
typedef long long int64;
inline int64 getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int64 x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=5e5+1;
struct Node {
int64 a,b;
bool operator < (const Node &rhs) const {
return a<rhs.a;
}
};
Node v[N];
int64 sum[N];
int main() {
const int n=getint();
for(register int i=1;i<=n;i++) {
v[i].a=getint();
v[i].b=getint();
}
std::sort(&v[1],&v[n]+1);
int64 max=0,ans=0;
for(register int i=1;i<=n;i++) {
sum[i]=sum[i-1]+v[i].b;
max=std::max(max,v[i].a-sum[i-1]);
ans=std::max(ans,sum[i]-v[i].a+max);
}
printf("%lld\n",ans);
return 0;
}

[JOI2017/2018]美術展的更多相关文章

  1. LOJ2350:[JOI2017/2018决赛]月票购买——题解

    https://loj.ac/problem/2350 比较简单的题,为什么我实现得这么sb? 第一个包其实已经给了提示(第一个包的解法就是在S->T所有最短路径上的所有点到V的最短路的最小值. ...

  2. LOJ2351:[JOI2017/2018决赛]毒蛇越狱——题解

    https://loj.ac/problem/2351 参考:https://www.cnblogs.com/ivorysi/p/9144676.html 但是参考博客讲解太吓人了,我们换一种通俗易懂 ...

  3. 2018美赛准备之路——Matlab基础——命令行功能函数

    clc 清屏(只清除显示内容) clear  清除所有变量(运算结果) who  显示workspace的所有变量 whos  详细显示workspace的所有变量  help sin 显示sin函数 ...

  4. 2018美赛准备之路——Matlab基础——基本运算符号表示

    π pi ln(x) log(x)   lg(x) log10(x) log2(x) log2(x) 根号 sqrt(x) x的y次方 x^y e的y次方 exp(y)    

  5. 自在因梦 | 威爾伯的Fourth Turning所引發的聯想

    2015-05-06                                                 胡因梦                                       ...

  6. N4复习考试总结

    一つ(ひとつ) 半分(はんぶん) 煙草(たばこ)を吸う(すう) 玄関(げんかん) ナイフ(刀)     財布(さいふ) 浅い(あさい) 薄い(うすい) 牛乳(ぎゅうにゅう) 皿(さら) 七日(なのか) ...

  7. (转)Unity原厂讲师大解密

    本文转载自:http://nedwu13.blogspot.tw/2013_11_01_archive.html   Asset Bundle工作流程及人物換裝實例 - 劉剛 Unity內部的資源有兩 ...

  8. Python 爬虫 数据提取

    一下子运行 七八十个  select 将会是什么样的体验呢? 业务部门提供了一个需要,要求从爬虫数据中提取出88家的数据, 并且也提供了一个excel表格,如下图: 这个时候我们可以通过拍卖行,拍卖时 ...

  9. ionic4 ion-picker用法

    ion-picker实际开发中肯定多处使用,所以封装成服务的形式调用 新建picker.service服务模块   ionic g service picker import { Injectable ...

随机推荐

  1. 如何使用VisualSVN Server建立版本库

    首先打开VisualSVN Server Manager,如图: 可以在窗口的右边看到版本库的一些信息,比如状态,日志,用户认证,版本库等.要建立版本库,需要右键单击左边窗口的Repositores, ...

  2. mysql 简称

    一:DTS(Data Transformation Service) 数据转换服务 大多数组织都使用多种格式和多个位置来存储数据. 为了支持决策.改善系统性能或对现有系统进行升级,经常必须将数据从一个 ...

  3. SpringMVC - 1.快速入门

    1. HelloWorld 步骤: 加入 jar 包 mons-logging-1.1.3.jar spring-aop-4.0.0.RELEASE.jar spring-beans-4.0.0.RE ...

  4. Spring Cloud、Spring Boot与Docker 学习资料汇总

    使用Spring Cloud与Docker实战微服务https://gitee.com/itmuch/spring-cloud-bookhttps://eacdy.gitbooks.io/spring ...

  5. redis性能监控(一): Redis Info 命令 - 获取 Redis 服务器的各种信息和统计数值

    https://blog.csdn.net/wmx3ng/article/details/82772891 https://blog.csdn.net/lxpbs8851/article/detail ...

  6. Elasticsearch snapshot 备份的使用方法 【备忘】

    常见的数据库都会提供备份的机制,以解决在数据库无法使用的情况下,可以开启新的实例,然后通过备份来恢复数据减少损失.虽然 Elasticsearch 有良好的容灾性,但由于以下原因,其依然需要备份机制. ...

  7. elk服务器和运维服务器的IPTABLES

    --运维服务器 iptables -P INPUT ACCEPT iptables -F iptables -X iptables -Z iptables -A INPUT -i lo -j ACCE ...

  8. Vs2015 本地git获取的代码目录文件修改后,启动提示error:Unable to start program “C:\Program Files\dotnet\dotnet.exe” 已解决.

    http://stackoverflow.com/questions/39938453/unable-to-start-program-c-program-files-dotnet-dotnet-ex ...

  9. PyTorch中的backward [转]

    转自:https://sherlockliao.github.io/2017/07/10/backward/ backward只能被应用在一个标量上,也就是一个一维tensor,或者传入跟变量相关的梯 ...

  10. c_数据结构_链表

    #include<stdio.h> #include<stdlib.h> #define ERROR 0 #define OK 1 #define OVERFLOW -2 ty ...