CF 276C Little Girl and Maximum Sum【贪心+差分】
C. Little Girl and Maximum Sum
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The little girl loves the problems on array queries very much.
One day she came across a rather well-known problem: you've got an array of n elements (the elements of the array are indexed starting from 1); also, there are q queries, each one is defined by a pair of integers li, ri (1 ≤ li ≤ ri ≤ n). You need to find for each query the sum of elements of the array with indexes from li to ri, inclusive.
The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum.
Input
The first line contains two space-separated integers n (1 ≤ n ≤ 2·105) and q (1 ≤ q ≤ 2·105) — the number of elements in the array and the number of queries, correspondingly.
The next line contains n space-separated integers ai (1 ≤ ai ≤ 2·105) — the array elements.
Each of the following q lines contains two space-separated integers li and ri (1 ≤ li ≤ ri ≤ n) — the i-th query.
Output
In a single line print a single integer — the maximum sum of query replies after the array elements are reordered.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
inputCopy
3 3
5 3 2
1 2
2 3
1 3
outputCopy
25
inputCopy
5 3
5 2 4 1 3
1 5
2 3
2 3
outputCopy
33
【题意】:有若干对区间和的查询,问如何重组数组使查询结果的和最大。
【分析】:统计每个数出现的次数,则总和最大的策略为:依次让最大的数放在出现查询区间最多的位置上。
重点在于如何快速的统计每个区间总共被查询的次数。可使用线段树的区间更新,也可使用巧妙一点的方法——差分标记,通过一个数组记录下每次查询的 L、 R,然后 cnt[L]++,cnt[R+1]--,然后统计cnt数组前缀和,因为是cnt[R+1]--,所以从头开始遍历计算前缀和不会多统计那些没有查询的区间的次数。
【代码】:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
typedef long long ll;
ll p[maxn];
ll x[maxn];
ll n,q;
ll a[maxn];
int main()
{
while(~scanf("%lld%lld",&n,&q))
{
memset(p,0,sizeof(p));
for(ll i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
while(q--)
{
ll l,r;
scanf("%lld%lld",&l,&r);
p[l]++;
p[r+1]--;
}
for(ll i=1; i<=n; i++)
{
p[i] += p[i-1];
}
sort(p+1,p+n+1);
sort(a+1,a+n+1);
ll sum = 0;
for(ll i=n;i>=1;i--)
{
sum += (ll)(a[i]*p[i]);
}
cout<<sum<<endl;
}
}
CF 276C Little Girl and Maximum Sum【贪心+差分】的更多相关文章
- [CF 276C]Little Girl and Maximum Sum[差分数列]
题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- 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 ...
- 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值
[抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- 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 ...
- CF 628C --- Bear and String Distance --- 简单贪心
CF 628C 题目大意:给定一个长度为n(n < 10^5)的只含小写字母的字符串,以及一个数d,定义字符的dis--dis(ch1, ch2)为两个字符之差, 两个串的dis为各个位置上字符 ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
随机推荐
- CSS继承—深入剖析
CSS的继承是指被包在内部的标签将拥有外部标签的样式性质.继承特性最典型的应用通常发挥在整个网页的样式预设,即整体布局声明.而需要要指定为其它样式的部份设定在个别元素里即可达到效果.这项特性可以给网页 ...
- 【C++ 拾遗】extern 关键字
Separate compilation allows programs to be written in logical parts. let us split our programs into ...
- 【题解】HNOI2009无归岛
这题真的是无语了,在哪个岛上根本就没有任何的用处……不过我是画了下图,感受到一定是仙人掌,并不会证.有谁会证的求解…… 如果当做仙人掌来做确实十分的简单.只要像没有上司的舞会一样树形dp就好了,遇到环 ...
- [BZOJ3196][Tyvj1730]二逼平衡树
[BZOJ3196][Tyvj1730]二逼平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询 \(k\) 在区间内的排名 查询区间内排名为 \ ...
- SqlServer中临时表的应用
一.变通处理WHERE后面IN的参数过多 WHERE后面的条IN操作符里的参数比较小时,可以直接使用IN(1,2,3)这样处理,当个数不确定(可能超过1000)时,应该考虑使用临时表关联查询: SEL ...
- 你是否彻底了解margin属性?
写css,你少不了与margin打交道.你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用padding而不是m ...
- “CNKI 中国知网 PDF 全文下载”油猴脚本在线安装地址
https://greasyfork.org/zh-CN/scripts/18841-cnki-%E4%B8%AD%E5%9B%BD%E7%9F%A5%E7%BD%91-pdf-%E5%85%A8%E ...
- 修改innodb_flush_log_at_trx_commit参数提升insert性能
最近,在一个系统的慢查询日志里发现有个insert操作很慢,达到秒级,并且是比较简单的SQL语句,把语句拿出来到mysql中直接执行,速度却很快. 这种问题一般不是SQL语句本身的问题,而是在具体的应 ...
- HDU4289:Control(最小割)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 注意@Bean中的initMethod和destroyMethod
@Configuration public class AppConfig { @Bean(initMethod = "init") public Foo foo() { retu ...