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> ...
随机推荐
- Spark+Python+Pycharm在Windows下的配置
http://blog.csdn.net/ydq1206/article/details/51922148
- [BZOJ3196][Tyvj1730]二逼平衡树
[BZOJ3196][Tyvj1730]二逼平衡树 试题描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询 \(k\) 在区间内的排名 查询区间内排名为 \ ...
- 停课day1
一早上只做了一个calculator 还是参照题解,好惭愧 f[1]=0; flag[1]=true; for (int i=2,N=num[n];i<p;i++) { fo ...
- 微信小程序使用原生WebSokcet实现断线重连及数据拼接
以前做小程序为了应急找了个插件去链接WebSokcet,文章传送门. 回过头在新项目中再次使用时出现了些许问题,不一一赘述.遂决定好好用一下原生的WebSokcet. 一.说明 1.小程序原生的Web ...
- JavaScript渐变效果的实现
鼠标移上去透明度渐渐增加,鼠标移出,透明度渐渐减小. 关键代码: view source print? 1 var speed = 0; 2 if(target>obj.alpha){ 3 ...
- VC++使用CImage在内存中Bmp转换Jpeg图片
之前写了一篇<VC++使用CImage在内存中Jpeg转换Bmp图片>,通过CImage实现了在内存中Jpeg转Bmp. 既然Jpeg能转Bmp,那CImage也支持Bmp转Jpeg,与上 ...
- itext转html为pdf遇到的问题
记录一下使用itext将html文件转为pdf文件遇到的一些问题 1.中文不显示 原因:itext默认不支持中文 解决方法:引入中文字体 需要注意的是在java代码中设置好中文字体后,还需要在html ...
- Eclipse工具栏太多,自定义工具栏,去掉调试
Window --> Customize Perspective... --> Tool Bar Visibility 去掉勾选debug Tip:最新版本Customize Persp ...
- VR行业纷纷倒闭:有硬件没内容
从去年年底开始,VR就成为了一个流行词汇,不仅是巨头公司砸钱布局,众多创业公司也纷纷投入其中.但是,一窝蜂拥入的企业基本都没有成熟的商业模式和赢利模式,只能靠融资供血.在资本寒冬中,大部分的VR企业开 ...
- 【poj3415-Common Substrings】sam子串计数
题意: 给出两个串,问这两个串的所有的子串中(重复出现的,只要是位置不同就算两个子串),长度大于等于k的公共子串有多少个. 题解: 这题好像大神们都用后缀数组做..然而我在sam的题表上看到这题,做 ...