Little Girl and Maximum Sum CodeForces - 276C - 差分
给定一个数列 \(a= { a_1,a_2,...,a_n }\) 以及 \(q\) 次查询。
其中第 \(i\) 次查询如同:\(l_i, r_i\),意指求 \(\sum_{j=l_i}^{r_i} {a_j}\)。
但是查询前可以对数列任意排序,使得查询结果的和最大,问最大的和是多少。
Input
第 \(1\) 行 \(n,q\);
第 \(2\) 行 \(a_1, a_2, ..., a_n\);
接下来 \(q\) 行,每行 \(2\) 个整数 \(l_i,r_i\);
数据范围:\(1≤n,q,a_i≤2·10^5,1≤l_i≤r_i≤n\)。
Output
最大的和
Sample Input
3 3
5 3 2
1 2
2 3
1 3
Sample Output
25
分析

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e6+10,INF=0x3f3f3f3f;
int n,m,a[N],b[N];
LL res=0;
//#define local
int main() {
#ifdef local
freopen("data.in", "r", stdin);
// freopen("data.out", "w", stdout);
#endif
int n,q; cin>>n>>q;
for(int i=1; i<=n; i++) cin>>a[i];
int l,r;
while(q--) {
cin>>l>>r; b[l]++, b[r+1]--;
}
for(int i=1; i<=n; i++) b[i]+=b[i-1];
sort(a+1, a+1+n); sort(b+1, b+1+n);
for(int i=1; i<=n; i++) res += 1ll*a[i]*b[i];
cout<<res;
return 0;
}
Little Girl and Maximum Sum CodeForces - 276C - 差分的更多相关文章
- CF 276C Little Girl and Maximum Sum【贪心+差分】
C. Little Girl and Maximum Sum time limit per test2 seconds memory limit per test256 megabytes input ...
- CodeForces 1060 B Maximum Sum of Digits
Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
- POJ 2479 Maximum sum 解题报告
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40596 Accepted: 12663 Des ...
随机推荐
- LeetCode(Java版)
两数之和 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15 ...
- 比Everything更强的文件搜索工具,支持文件名、文件内容和文件图片上的文字搜索,文件内容搜索工具,文件图片内容搜索工具,OCR图片文本识别搜索,文件快速搜索工具,文字识别文件搜索工具
Windows自带的文件搜索功能想必不需要过多吐槽,搜索速度简直是在龟爬,所以小编很早之前就在用Everything进行文件搜索了, 不过,今天的主角不是它,而是比它更更更更更强的一款软件! 这款软件 ...
- nginx的优化及防盗链
简介: Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为"engine X",是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/S ...
- ssh登录、scp传送文件
一.ssh 1.远程登陆服务器 ssh user@hostname user: 用户名hostname: IP地址或域名 第一次登录时会提示:The authenticity of host '123 ...
- unittest框架-测试报告模板【BeautifulReport】安装、配置使用、生成带截图的测试报告
一.下载BeautifulReport模块 1.下载BeautifulReport模块 下载地址:https://github.com/TesterlifeRaymond/BeautifulRepor ...
- Ubuntu之docker搭建dvwa
前提,已经安装好docker. 首先,搜索DVWA镜像,
- (0303)怎么在sequence中调用agent中的函数以及如何快速实验你的想法?
https://mp.weixin.qq.com/s/9hDz9-nur5szBib18_yPnA
- nginx(一) の 入门解析
OSI 模型的前三层 应用层: 每一个应用程序自定义的协议 表示层: 数据的压缩与解压缩.图片的编码与解码 会话层: 会话管理(session) 和 网络验证 .包括断点续传和服务器验证用户登录等.比 ...
- ServiceEntry与WorkloadEntry
ServiceEntry: 用于将未能自动添加至网格中的服务,以手形式使得内发 现机制能够访问或路由到这些服务.网格外部的服务 运行于 Kubernetes 上,但却非为 Istio 网格 ...
- 配dns
cat /etc/resolv.conf