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个人演唱歌曲 ...
随机推荐
- 微信小程序-页面跳转navigator组件
官方文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/app-service/route.html 在官方文档当中有提到一 ...
- 第三届人工智能,大数据与算法国际学术会议 (CAIBDA 2023)
第三届人工智能,大数据与算法国际学术会议 (CAIBDA 2023) 大会官网:http://www.caibda.org/ 大会时间:2023年6月16-18日 大会地点:中国郑州 截稿日期:2 ...
- 机器学习算法(四): 基于支持向量机的分类预测(SVM)
机器学习算法(四): 基于支持向量机的分类预测 本项目链接:https://www.heywhale.com/home/column/64141d6b1c8c8b518ba97dcc 1.相关流程 支 ...
- 5.0 Python 定义并使用函数
函数是python程序中的基本模块化单位,它是一段可重用的代码,可以被多次调用执行.函数接受一些输入参数,并且在执行时可能会产生一些输出结果.函数定义了一个功能的封装,使得代码能够模块化和组织结构化, ...
- 错误解决:ElasticSearch SearchResponse的Hits[]总是比totalHits少一条记录
在做ElasticSearch查询操作的时候,发现Hits[].length总是比totalHits.value少1.代码如下: SearchRequest request = new SearchR ...
- 关于SUPPLEMENTAL_LOG_DATA_MIN的设置问题
Oracle数据库开启附加日志,用于Logminer或基于Logminer的一些操作. 客户咨询关于开启附加日志,SUPPLEMENTAL_LOG_DATA_MIN显示为啥是implicit,如何改成 ...
- Delphi中 调试 指针
p1.Free; 释放堆中数据,最终无内存泄漏,只是加深记忆: 有些时候 灵活应对:
- DELPHI IDE 代码智能提示
- HWS山大专区PWN双一血 & CRYPTO-WP
2023.11.18 两天半的比赛,就打了半天(因为要赶去打香山杯决赛了),不过结果还算好,人生第一次拿了两个一血hhh.写wp的时候人在中大南校北门的酒店里:) controller 格式化字符串泄 ...
- JS Leetcode 70. 爬楼梯 题解分析,斐波那契数列与动态规划
本题来自LeetCode70. 爬楼梯,难度简单,属于一道动态规划的入门题,题目描述如下: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬 ...