Codeforces Round 169 (Div. 2)C. Little Girl and Maximum Sum(差分、贪心)
题面

链接
C. Little Girl and Maximum Sum
题意
给q个[l,r]将所有这些区间里面的数相加和最大。
可以进行的操作是任意排列数组
题解
对出现的每个区间内的位置加上1,代表权值
操作完之后求一遍前缀和,得到每个位置的权值
然后贪心的考虑,权值越大,应该分配给该位置的数越大越好这样对答案的贡献最大。
代码
#include <bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
using namespace std;
const int N=2e5+10;
int a[N],cf[N];
void solve()
{
int n,q;
cin>>n>>q;
rep(i,1,n){
cin>>a[i];
}
sort(a+1,a+1+n,greater<int>());
while(q--){
int l,r;
cin>>l>>r;
cf[l]+=1,cf[r+1]-=1;
}
rep(i,1,n){
cf[i]+=cf[i-1];
}
sort(cf+1,cf+1+n,greater<int>());
int ans=0;
rep(i,1,n){
ans+=cf[i]*a[i];
}
cout<<ans<<endl;
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int _;
// cin>>_;
// while(_--)
solve();
return 0;
}
总结
注意空间,空间开小了在cf上可能\(tle\)
对着种多次区间操作,一次查询要考虑一下差分,很多时候用不到那么复杂的数据结构就能解决问题。
Codeforces Round 169 (Div. 2)C. Little Girl and Maximum Sum(差分、贪心)的更多相关文章
- Round #169 (Div. 2)C. Little Girl and Maximum Sum
1.用退化的线段树(也就是没有区间查询)做... 2.注意longlong. #include<cstdio> #include<cstring> #include<io ...
- Codeforces Round #169 (Div. 2)
A. Lunch Rush 模拟. B. Little Girl and Game 因为可以打乱顺序,所以只关心每种数字打奇偶性. 若一开始就是回文,即奇数字母为0或1种,则先手获胜. 若奇数字母大于 ...
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
- Codeforces Round #169 (Div. 2) A水 B C区间更新 D 思路
A. Lunch Rush time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #320 (Div. 1) [Bayan Thanks-Round] B. "Or" Game 线段树贪心
B. "Or" Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/578 ...
- Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)
A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...
- Codeforces Round #371 (Div. 1) C. Sonya and Problem Wihtout a Legend 贪心
C. Sonya and Problem Wihtout a Legend 题目连接: http://codeforces.com/contest/713/problem/C Description ...
- codeforces水题100道 第十八题 Codeforces Round #289 (Div. 2, ACM ICPC Rules) A. Maximum in Table (brute force)
题目链接:http://www.codeforces.com/problemset/problem/509/A题意:f[i][1]=f[1][i]=1,f[i][j]=f[i-1][j]+f[i][j ...
- Codeforces Round #540 (Div. 3) D1. Coffee and Coursework (Easy version) 【贪心】
任意门:http://codeforces.com/contest/1118/problem/D1 D1. Coffee and Coursework (Easy version) time limi ...
- Codeforces Round #375 (Div. 2) Polycarp at the Radio 优先队列模拟题 + 贪心
http://codeforces.com/contest/723/problem/C 题目是给出一个序列 a[i]表示第i个歌曲是第a[i]个人演唱,现在选出前m个人,记b[j]表示第j个人演唱歌曲 ...
随机推荐
- minIO系列文章01---MinIO 简介
MinIO.jpeg MinIO 官网 MinIO 官方GitHub MinIO 官方文档 1.什么是对象存储? 关于对象存储,我们可以看下 阿里云OSS 的解释. 对象存储服务OSS(Object ...
- 分布式压测之locust和Jmeter的使用
受限于单台机器的配置问题,我们在单台机器上达不到一个很高的压测并发数,那这个时候就需要引入分布式压测 分布式压测原理: 一般通过局域网把不同测试计算机链接到一起,达到测试共享.分散操作.集中管理的目的 ...
- navicat破解(15以前的版本)
navicat破解各种不成功,很耽误事.所以,再次整理一个相对省事有效的办法.内容如下: 一:下载此激活工具 二:按下图生成激活码 1. 2. 点击手动激活 3. 将请求码按图粘贴,按图点击操作便可激 ...
- delphi 自己用的加密解密算法
// 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...
- Linux-nmon系统监控工具
一.Nmon介绍 Nmon得名于 Nigel 的监控器,是IBM的员工 Nigel Griffiths 为 AIX 和 Linux 系统开发的,使用 Nmon 可以很轻松的监控系统的 CPU.内存.网 ...
- MySQL系列文章汇总
MySQL系列文章汇总: 导读: 大家好,我是xbhog,MySQL还是到了单独开一个系列了,这样不管是对我还是对读者来说在查找的时候都会方便一些: 话不多说,来看下,该系列会持续更新的(还是看学到哪 ...
- JS leetcode 旋转数组 题解分析
壹 ❀ 引 今天来做一道同样简单,但是挺有趣的题,题目来自leetcode189. 旋转数组,题目描述如下: 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: ...
- JS script脚本async和defer的区别
壹 ❀ 引 我在 google recaptcha 谷歌人机身份验证使用教程 一文中有引用这样一段外部资源代码,如下: <script src="https://www.google. ...
- NVME学习笔记六—Controller Architecture
Controller架构 NVMe over Fabrics使用与NVMe基础规格说明书中定义相同的controller架构.这包括主机和controller之间使用SQ提交队列和CQ完成队列来执 ...
- 【分布式】load balance 02-consistent hash algorithm 一致性哈希算法原理详解
负载均衡系列专题 01-负载均衡基础知识 02-一致性 hash 原理 03-一致性哈希算法 java 实现 04-负载均衡算法 java 实现 概念 一致哈希是一种特殊的哈希算法. 在使用一致哈希算 ...