假期训练--中石油--前缀和

中石油–数苹果

题目描述
苹果丰收了,有n堆苹果,小红就在苹果堆旁。小红已经知道了每堆苹果有多少个。她要问一问从第a堆到第b堆一共有多少个苹果。
输入
输入数字n,然后输入n个数据。再输入m,然后输入m行数据。
输出
输出m次a到b堆一共有多少个。
样例输入 Copy
5
1 2 3 4 5
3
1 3
2 4
1 5
样例输出 Copy
6
9
15
提示
对于%80的数据:0≤n≤10000;
对于%100的数据:0≤n≤100000。


#include<bits/stdc++.h>
using namespace std;
long long sum[100005];
int main(){
    long long n,m,x,i,y;
    scanf("%lld\n",&n);
    sum[0]=0;//此处sum[0]代表前0项和
    for( i=1;i<=n;i++){
        scanf("%lld",&x);
        sum[i]=sum[i-1]+x;//前i项和等于前i-1项和加上后来输入的数据
    }
    scanf("%lld",&m);
    while(m--){
        scanf("%lld %lld",&x,&y);
        printf("%lld\n",sum[y]-sum[**x-1**]);//注意后面数组的角标记得减1哦!
    }
    return 0;
}
在这里插入代码片

注意本题主要考察前缀和
解答本题时可以先将前n项和利用数组储存起来,然后再求前n项和的某一区间范围内的区间和,如[a,b]之间的和直接就可以用前b项和减去前a-1项和即可求出答案。
而且本题在提交代码在使用cin和cout可能会卡在时间超限上,所以请使用scanf和printf输入输出。

C. Kuangyeye and hamburges [ Problem 2725 ]

[ Discussion ]
Description
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.

Input
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
Output contain a single integer,represents maximum weight of hamburgers Kuangyeye can eat

Samples
Input Copy
5 2
4 3 5 2 6
1 1
3 4
Output
7
Hint
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

#pragma GCC optimize(2)
#pragma G++ optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+9;

int main()
{
	ll a[maxn],sum[maxn];
	ll n,m,c,d,i;
	ll ans=0;
	cin>>n>>m;
	for( i=1;i<=n;i++)
	{
	   cin>>a[i];
	}
	sort(a+1,a+n+1,greater<int>());
	for( i=1;i<=n;i++)
	{
	   sum[i]=sum[i-1]+a[i];
	}
	for( i=1;i<=m;i++)
	{
	  cin>>c>>d;
	  ans=max(ans,sum[d]-sum[c-1]);
	}
	cout<<ans<<endl;
   return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值