链接:https://ac.nowcoder.com/acm/contest/338/H
来源:牛客网

题目描述

Kuangyeye is a dalao of the ACM school team of Hunan University. His favorite food are hamburgers. One day, Kuangyeye came to the KFC(or maybe McDonald) and saw n hamburgers on the counter.The weight of the i-th hamburger was wi. Since he likes hamburgers very much, he would like to buy some hamburgers. Considering his weight or other factors, Kuangyeye only wanted to eat all the hamburgers from the a-th heaviest to the b-th. Since Kuangyeye is fickle, he had k plans before buying hamburgers. The i-th plan gives ai and bi. Please help Kuangyeye calculate the maximum weight of hamburgers he can eat among the k plans.

输入描述:

the first line of input contains two integer n and k--the number of hamburgers on the counter and the number of plans Kuangyeye had;
the next line contains n integer--the i-th integer represents the weight of i-th hamburger,namely w

i

;
Each the following k line contains two integer a

i

 and b

i

 ,represents Kuangyeye's strategy in his i-th plan.

输出描述:

Output contain a single integer,represents maximum weight of hamburgers Kuangyeye can eat.
示例1

输入

复制

5 2
4 3 5 2 6
1 1
3 4

输出

复制

7

说明

Kuangyeye's first plan was to eat the hamburger weighing 6;

and his second plan was to eat the hamburger weighing 3 and 4;

So the maximum weight of hamburgers he can eat was 7.

备注:

1≤n,k≤100000,1≤a

i

≤b

i

≤n,1≤w

i

≤10000

题解:排序,计算前缀和,模拟

#include <iostream>
#include <algorithm>
using namespace std;
int w[100050];
long long dp[100050]; bool cmp(const int& a,const int& b) {
return a>b;
} int main() {
int n,k;
long long ans=0;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>w[i];
sort(w+1,w+n+1,cmp);
dp[0]=0;
for(int i=1;i<=n;i++) {
dp[i]=dp[i-1]+w[i];
}
while(k--) {
int l,r;
cin>>l>>r;
ans=max(dp[r]-dp[l-1],ans);
}
cout<<ans<<endl;
return 0;
}

H Kuangyeye and hamburgers的更多相关文章

  1. 【哈希表】Ural Championship April 30, 2017 Problem H. Hamburgers

    题意:有n群人,每个人有喜欢的汉堡配方:有m家店,给出每家店的每个汉堡的配方,如果存在某个汉堡,其配料表包含某个人喜欢的配方,则这个人喜欢这个汉堡所在的店家.问你对每群人,输出被喜欢的人数最多的店面是 ...

  2. Codeforces Round #218 (Div. 2) C. Hamburgers

    C. Hamburgers time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. B - Hamburgers

    Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own han ...

  4. Codeforces 371C Hamburgers (二分答案)

    题目链接 Hamburgers 二分答案,贪心判断即可. #include <bits/stdc++.h> using namespace std; #define REP(i,n) fo ...

  5. C. Hamburgers

    Polycarpus loves hamburgers very much. He especially adores the hamburgers he makes with his own han ...

  6. APUE中fcntl.h的使用及O_SYNC在Mac与Ubuntu下的测试

    此部分测试涉及到APUE V3中,第三章的图3-12到图3-14. 通过fcntl.h提供的功能,修改fd的文件属性,本处增加O_SYNC功能,并测试其效果. 本文涉及代码: tree ch3 ch3 ...

  7. 关于apue.3e中apue.h的使用

    关于apue.3e中apue.h的使用 近来要学一遍APUE第三版,并于此开博做为记录. 先下载源文件: # url: http://http//www.apuebook.com/code3e.htm ...

  8. YYModel 源码解读(二)之NSObject+YYModel.h (1)

    本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...

  9. YYModel 源码解读(一)之YYModel.h

    #if __has_include(<YYModel/YYModel.h>) FOUNDATION_EXPORT double YYModelVersionNumber; FOUNDATI ...

随机推荐

  1. Java基础学习(3)

    Java基础学习(三) Java异常 Throwable类:所有异常的祖先类 Error:虚拟机异常.内存错误.没法处理 Exception:编码.环境.用户操作输入出现问题 非检查异常(自动捕获): ...

  2. python基础--匿名函数

    def calc(x): return x+1 a=calc(10) print(calc) print(lambda x:x+1)#lambda函数的内存地址,利用函数名可以作为值传递给变量 b=l ...

  3. (转)nginx+redis实现接入层高性能缓存技术

    转自:https://blog.csdn.net/phil_code/article/details/79154271 1. OpenRestyOpenResty是一个基于 Nginx与 Lua的高性 ...

  4. python 的三元运算符

    一.三元运算符 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式:[on_true] if [expression] else [on_false] res = 值1 if 条件 els ...

  5. maven-enforcer-plugin查看冲突

    我们会经常碰到这样的问题,在pom中引入了一个jar,里面默认依赖了其他的jar包.jar包一多的时候,我们很难确认哪些jar是我们需要的,哪些jar是冲突的.此时会出现很多莫名其妙的问题,什么类找不 ...

  6. Dubbo学习-1-基础知识

    分布式基础理论 1.什么是分布式系统: 分布式系统是若干独立计算机的集合,这些计算机对于用户来说就像是单个相关系统.分布式系统是建立在网络之上的软件系统 随着互联网发展,网站应用规模的不断扩大,常规的 ...

  7. Python全栈开发,Day2

    一.Pycharm的使用 1.创建项目 2.python调整字体大小随ctrl+鼠标滚轮上下滚动 3.python新建程序自动补全编码和环境 4.设置断点(在代码前面行号后面单击鼠标左键) 5.调试断 ...

  8. [IOI2018] meetings 会议

    https://www.luogu.org/problemnew/show/P5044 题解 这种关于最大值或者最小值的问题,可以往笛卡尔树的方面想. 先考虑一个朴素的\(dp\),设\(dp[l][ ...

  9. [CSP-S模拟测试]:传递(暴力+bitset)

    题目描述 我们称一个有向图$G$是传递的,当且仅当对于图$G$的三个不同顶点$a,b,c$,若图$G$中有一条边从$a$到$b$且有一条边从$b$到$c$,那么图中也有一条边从$a$到$c$.我们称一 ...

  10. 笨办法学Python(learn python the hard way)--练习程序39-40

    下面是练习39-练习40,基于python3 #ex39.py 1 ten_things = "Apples Oranges Crows Telephone Light Sugar" ...