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
#include
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>a[i];
			m=m-a[i];             //当前能量
			if(m<0) 
			{
				res+=abs(m); 
				m=0;           
			}
		}
		cout<

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

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

#include
#include
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;
}

你可能感兴趣的:(CF,算法)