湖南大学第十四届ACM程序设计大赛 H Kuangyeye and hamburgers

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

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

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 wi;
Each the following k line contains two integer ai and bi ,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≤ai≤bi≤n,1≤wi≤10000

题目大意:

Kuangyeye是湖南大学ACM校队的大佬。他最喜欢的食物是汉堡包。有一天,Kuangyeye来到肯德基(或者麦当劳),看到柜台上有N个汉堡包,第一个汉堡包的重量是WI。因为他非常喜欢汉堡包,所以他想买一些汉堡包。考虑到他的体重或其他因素,光眼只想吃从A到B的所有汉堡包。由于光眼变化无常,他在买汉堡包之前就有了K计划。第i个计划给出了ai和bi。请帮助况眼计算出他在K计划中所能吃的汉堡包的最大重量。

第一行输入包含两个整数n和k——柜台上的汉堡包数量和Kuangeye的计划数量;
下一行包含n个整数——第i个整数表示第i个汉堡的重量,即wi;
下面的每一行包含两个整数ai和bi,代表了Kuangyeye在他的第i个计划中的策略。

输出包含一个整数,代表了Kuangyeye可以吃的汉堡包的最大重量。

1≤n,k≤100000,1≤ai≤bi≤n,1≤wi≤10000

分析:n个数字,k个方案,每个求从第a大数字到第b大之间所有数字的和,问所有方案中数字的和最大是多少,先对所有的数字(汉堡的重量)??按由大到小排个序,这样求区间a至b的重量和是最大的。但n和k的数据范围是1e5,每次单独求和肯定会超时,这个时候就要用到前缀和了。即

答案即为:

最后输出的ans即为最大值

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int x=1000000;
int n,k,i,j,c,d;
ll sum[x],ans,a[x];
bool cmp(int a,int b)//用于定义降序排序
{
	return a>b;
}
int main()
{
	cin>>n>>k;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1,cmp);//对输入数据进行降序排序
	for(int i=1;i<=n;i++)
	{
		sum[i]=sum[i-1]+a[i];//求前缀和
	}
	for(int i=1;i<=k;i++)
	{
		cin>>c>>d;//输入区间c和d
		ans=max(ans,sum[d]-sum[c-1]);//求区间c和d的前缀和的差,与之前的最大值比较取新的最大值
	}
	cout<<ans<<endl;//输出最终最大值
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值