[每日一题]:CodeForces - 1279B Verse For Santa

题目:

题目大意:

给一个序列,然后给你一个数 s ,从头开始进行累加,不能超过 s
在累加的过程中,你可以 跳过 一个数 进行累加,最后保证累加的
数量是最多的,不是累加的和哦,输出你跳过的那个数在序列中的位置。
(根据样例推断,似乎只能从前开始累加,否则第一个样例就有误了,嘻嘻)

考察点:

签到题,模拟即可。

Code:

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 1e5 + 10;

typedef long long LL;

int a[maxn]; 

int t,n,s;

int main(void) {
	scanf("%d",&t);
	while(t --) {
		LL res = 0,ans = 0;
		scanf("%d%d",&n,&s);
		for(int i = 1; i <= n; i ++) {
			scanf("%d",&a[i]);
			ans += a[i];
		}
		// 总和 <= s 时直接跳过 
		if(ans <= s) {
			puts("0");
			continue;
		}
		// 存储跳过的位置,时刻更新最大值 
		int pos = 0,maxValue = -1;
		bool flag = false;
		for(int i = 1; i <= n; i ++) {
			res += a[i];
			if(res > s && flag) {
				res -= a[i];
				break;
			}
			if(res > s) {
				res -= a[pos];
				flag = true;
			}
			if(maxValue < a[i]) maxValue = a[i],pos = i;
		}
		printf("%d\n",pos);
	}	
	return 0;
} 

后记:

今天的题目有点水了,抱歉,各位!
posted @ 2020-04-26 19:54  IceSwords  阅读(215)  评论(2编辑  收藏  举报