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个人演唱歌曲 ...
随机推荐
- 开源即时通讯(IM)项目OpenIM源码部署流程
由于 OpenIM 依赖的组件较多,开发者需求不一,导致 OpenIM 部署一直被人诟病,经过几次迭代优化,包括依赖的组件 compose 的一键部署,环境变量设置一次,全局生效,以及脚本重构,目前 ...
- python入门之后须掌握的知识点(模块化编程、时间模块)【一】
相关文章: 全网最详细超长python学习笔记.14章节知识点很全面十分详细,快速入门,只用看这一篇你就学会了! python入门合集: python快速入门[一]-----基础语法 python快速 ...
- 植物大战僵尸:寻找阳光掉落Call
本次实验内容:本次实验将接触到Call调用这个概念,什么是Call调用? Call相当于你在编程时所编写的函数,而高级语言中的函数最终也是会被编译器转换为汇编格式的Call调用,这些关键Call普遍都 ...
- Visual Studio部署C++矩阵库Armadillo的方法
本文介绍在Visual Studio软件中配置C++环境下线性代数运算库Armadillo的方法. 首先,我们需要在Armadillo库官网下载其源代码,直接点击下图所示红色框内部分即可. ...
- ElasticSearch-聚合、自动补全、集群、数据同步
数据聚合 1.数据聚合 聚合(aggregations)可以让我们极其方便的实现对数据的统计.分析.运算.例如: 什么品牌的手机最受欢迎? 这些手机的平均价格.最高价格.最低价格? 这些手机每月的销售 ...
- (C语言)每日代码||2023.12.21||C语言预处理命令,#define、#line、__LINE__、__FILE__
#include <stdio.h> #define AAA 111 void test() { printf("__LINE__ = % d\n", __LINE__ ...
- 新零售SaaS架构:促销系统架构设计
促销业务概述 什么是促销? 促销是商家用来吸引消费者购物的一种手段,目的是让更多的人知道并购买他们的产品,这样就能卖得更多.促销的方法有很多种,比如,价格优惠.赠品.优惠券.折扣.买一赠一等形式. 特 ...
- 洛谷P2670 扫雷游戏 关于区域搜索标记的坐标增量法
最简单的思路就是扫描一边所有节点,对每个非地雷节点,去检查一下他八个方向的元素是否有'*',有的话就加一 但是逐个写出有点麻烦,我们不妨定义两个增量数组来存储每一次的相对位移,对每次检查只需要遍历这个 ...
- electron 开发 ,如何使用 第三方 库 进行typescript 开发,举例:jquery 其它的 应该也是一致。
首先要弄明白一点,electron 开发 与 nodejs开发 基本一致. 要引入 jquery 实际上就是 nodejs 引入 jquery 第一步是 去 nmp中央仓库,查看,里面有详细的说明使用 ...
- [数据库] 数据库中的DDL、DML、DQL、DCL
SQL 程序语言有四种类型,对数据库的基本操作都属于这四种类,也就是标题上显示的 DDL.DML.DQL.DCL. 1. DDL DDL(Data Definition Language 数据定义语言 ...