链接: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. Python在windows下编译成exe文件

    1. pip install pyinstaller 2. 在Terminal下输入:“pyinstaller -F -w  *.py” 就可以生成exe.生成的文件放在同目录dist下. -F(注意 ...

  2. python基础内置函数

    #取绝对值 #print(abs(-1)) #对序列中的元素进行bool运算,如果可迭代对象为空也返回True # print(all((1,23,))) # print(all({"nam ...

  3. MYSQL学习笔记——连接以及存储过程

    连接 当我们需要从多个表查询数据时,我们就需要使用到连接操作,mysql支持内连接,左连接以及右连接三种连接方式. 数据库准备 首先我们创建两个表t1, t2: create table t1(i1 ...

  4. Spring Aop 动态代理失效分析

    1. Spring Aop 原理 Spring Aop 通过动态代理创建代理对象,在调用代理对象方法前后做增强. 2. Transactional, Async 注解失效? 当在动态代理方法中调用当前 ...

  5. php-redis 使用命令

    PHP 使用redis 一些命令参考:https://www.jianshu.com/p/68b7114a1d70

  6. 如何分析及处理 Flink 反压?

    反压(backpressure)是实时计算应用开发中,特别是流式计算中,十分常见的问题.反压意味着数据管道中某个节点成为瓶颈,处理速率跟不上上游发送数据的速率,而需要对上游进行限速.由于实时计算应用通 ...

  7. maven构建docker镜像异常

    由于没有配置ip+2375端口,导致每次跑的时候,都是连接本地的,一直会报错 [ERROR] Failed to execute goal com.spotify:docker-maven-plugi ...

  8. 【Java】JSONObject学习

    介绍 JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素.JSONObject可以很方便的转换成字符串,也可以 ...

  9. 0XFF

    0xFF = 00000000000000000000000011111111 也就是说 0xFF 代表了一个 byte 位全为 1,-1的补码(数据). 数值和数据是两个不同的概念.数值表示一个数的 ...

  10. 2、投资之基金 - IT人思维之投资

    笔者曾经对基金进行投资,但是当时对基金不是很了解,只是为了投资而去投资.现在,笔者对基金的投资有了更深入的了解认识,所以就有了本文. 基金投资在国内还是挺流行的,虽然其是从国外引进来的概念经验,但是国 ...