codeforces( 1697 A Parkway Walk 1697 B Promo)

You are walking through a parkway near your house. The parkway has n+1n+1 benches in a row numbered from 11 to n+1n+1 from left to right. The distance between the bench ii and i+1i+1 is aiai meters.

Initially, you have mm units of energy. To walk 11 meter of distance, you spend 11 unit of your energy. You can't walk if you have no energy. Also, you can restore your energy by sitting on benches (and this is the only way to restore the energy). When you are sitting, you can restore any integer amount of energy you want (if you sit longer, you restore more energy). Note that the amount of your energy can exceed mm.

Your task is to find the minimum amount of energy you have to restore (by sitting on benches) to reach the bench n+1n+1 from the bench 11 (and end your walk).

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1≤t≤1001≤t≤100) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and mm (1≤n≤1001≤n≤100; 1≤m≤1041≤m≤104).

The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), where aiai is the distance between benches ii and i+1i+1.

Output

For each test case, print one integer — the minimum amount of energy you have to restore (by sitting on benches) to reach the bench n+1n+1 from the bench 11 (and end your walk) in the corresponding test case.

翻译:给了一段路,中间有座位,就是初始会给你能量,走一步会消耗一个能量,坐在座位上会恢复能量,求必须恢复的最小能量。转换思维,使得刚好到达座位时的能量为0就行,直接用m去减去椅子之间的距离,为负数时说明恰好差这么多能量就能到达这里.

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a[110];
		int n,m,res=0;
		cin>>n>>m;
		for(int i=0;i<n;i++)
		{
			cin>>a[i];
			m=m-a[i];             //当前能量
			if(m<0) 
			{
				res+=abs(m); 
				m=0;           
			}
		}
		cout<<res<<endl;
	}
	return 0;
 } 

第二题的题意:给了一堆商品有各自的价值,从中选x件,挑y件最小的为免费,求y件的价值最大是多少,思维,我们先排序,找出x件最大的,那么其中y件最小的就为找的最大值。

注意题目给的数据:如果直接暴力解的话会TLE,我们就用前缀和,但是肯定会爆,所以我们前缀和数组必须开long long

#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10;
int  a[N];
long long b[N];
int main()
{
	int n,q;
	scanf("%d %d",&n,&q);
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&a[i]);
	}
	sort(a,a+n+1);
	for(int i=1;i<=n;i++) 
	{
		b[i]=b[i-1]+a[i];
	}
	while(q--)
	{
		long long sum=0;
		int  x,y;
	    scanf("%d %d",&x,&y);
	    sum=b[n-x+y]-b[n-x];
	    printf("%lld\n",sum);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值