题面

链接

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(差分、贪心)的更多相关文章

  1. Round #169 (Div. 2)C. Little Girl and Maximum Sum

    1.用退化的线段树(也就是没有区间查询)做... 2.注意longlong. #include<cstdio> #include<cstring> #include<io ...

  2. Codeforces Round #169 (Div. 2)

    A. Lunch Rush 模拟. B. Little Girl and Game 因为可以打乱顺序,所以只关心每种数字打奇偶性. 若一开始就是回文,即奇数字母为0或1种,则先手获胜. 若奇数字母大于 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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个人演唱歌曲 ...

随机推荐

  1. 深入浅出Java多线程(二):Java多线程类和接口

    引言 大家好,我是你们的老伙计秀才!今天带来的是[深入浅出Java多线程]系列的第二篇内容:Java多线程类和接口.大家觉得有用请点赞,喜欢请关注!秀才在此谢过大家了!!! 在现代计算机系统中,多线程 ...

  2. LyScript 验证PE程序开启的保护

    有些漏洞利用代码需要在某个保护模式被关闭的情况下才可以利用成功,在此之前需要得到程序开启了何种保护方式.验证其实有很多方法,其原理是读入PE文件头部结构,找到OPTIONAL_HEADER.DllCh ...

  3. asp.net 生成word,处理图片,富文本框内容图片处理

    //基本导出方法public void Download() { Random rd = new Random(); string fileName = DateTime.Now.ToString(& ...

  4. C++11之函数对象

    目录 1.使用场景 2.函数对象 3.std::bind 4.总结 1.使用场景 在没有C++11的时候,我们通常使用回调函数来完成某些特定的功能,使用回调函数就需要先声明函数指针 示例: typed ...

  5. Load-balanced-online-OJ-system 负载均衡的OJ系统项目

    前言 那么这里博主先安利一些干货满满的专栏了! 首先是博主的高质量博客的汇总,这个专栏里面的博客,都是博主最最用心写的一部分,干货满满,希望对大家有帮助. 高质量博客汇总 本项目Github地址 - ...

  6. SpringBoot自动化配置

    SpringBoot自动化配置 一.SpringBoot自动化配置原理 1.SpringBoot引导类注解介绍 在SpringBoot的启动引导类上有一个@SpringBootApplication注 ...

  7. NSSRound16

    NSSRound16 RCE但是没有完全RCE 审题 审核代码,简单的md5绕过. 知识点 md5绕过,命令组合,shell里``中的内容会被当成代码执行 知识详解 md5等于的绕过方法 数组绕过 a ...

  8. Github开源项目Excalidraw:简洁易用的手绘风格白板工具

    Excalidraw是Github上的一个开源项目,它提供了一个简洁易用的手绘图形创建工具,用户可以通过它创建流程图.示意图.架构图和其他各种图形.本文将介绍Excalidraw的特点和功能,并探讨其 ...

  9. delphi TThread.WaitFor 用法

    在 Delphi 中,TThread.WaitFor 方法用于等待一个线程完成执行.当你创建一个线程并希望主线程(或其他线程)等待这个线程结束时,你可以使用这个方法. 以下是 TThread.Wait ...

  10. 下载安装JDK 和 IntelliJ IDEA 和 ActiveMq

    wget http://yun.diandaxia.com/other/jdk-8u92-linux-x64.rpm rpm -ivh jdk-8u92-linux-x64.rpm wget http ...