链接: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. Maya2019下载安装与激活

    目录 1. 更多推荐 2. 下载地址 2.1. OneDrive 2.2. Window (64位) 2.3. MAC_OSX 3. 安装激活教程 1. 更多推荐 其他Maya版本的下载与激活:htt ...

  2. C# List<object> 按特定字段排序

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  3. 使用C#解析XMIND文件格式

    static void Main(string[] args) { var tempPath = @"c:\Temp"; if (Directory.Exists(tempPath ...

  4. process-hacker

    https://github.com/processhacker/processhacker#process-hacker // begin_phapppub typedef enum _PH_KNO ...

  5. Redis分布式锁【实战】

    概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致性问题一直是一个比较重要的话题.分布式的CAP理论告诉我们“任何一个分布式系统都无法同时满足一致性(Consistency).可 ...

  6. error: must use ‘class’ tag to refer to type ‘XXX’ in this scope

    开发环境: Qt Creator 4.8.2 在写程序的时候,遇到了编译器报错 error: must use 'class' tag to refer to type 'XXX' in this s ...

  7. Ubuntu apt-get install E: 无法定位软件包Ubuntu apt-get install E: 无法定位软件包

    sudo cp /etc/apt/sources.list /etc/apt/sources.list-bak #先将之前的source-list备份 sudo vi /etc/apt/sources ...

  8. 二叉树的下一个结点(剑指offer_8)

    题目描述 给定一个二叉树和其中一个结点,请找出中序遍历顺序的下一个结点并返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. public class TreeLinkNode { i ...

  9. python类对象属性查找原理

    class Foo(object): def __init__(self): # 这是一个对象属性 self.obj_pro = 12 # 这是一类属性 c_pro = 11 # 这是一个静态方法 @ ...

  10. 当执行一条查询语句时,MySQL内部经历了什么?

    假如说我们有一张表 T ,表里只有一个字段 ID,当我们执行下边这条SQL语句时: mysql> select * fron T where ID=10; 在我们眼中能看到的只是输入一条 SQL ...