B. Verse For Santa (模拟、细节)

题目

当能直接唱完就输出0。
当不能时从开始遍历相加直到sum>s,取前面最大值的那个下标,因为减去最大才更有利于后面唱的片段最多。
但要注意下范围:跳过片段该遍历到哪?跳过之后若想能比不跳过多,则需至少到达s+2(无跳过时s是最大不超出的片段数),因为跳过一个则需到s+1才和原来的片段相等,要大于需s+2。所以我们要找到从1—s+1中的最大数下标。

Code:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int Max = 1e6 + 5;
int lst[Max];

int main()
{
    
    
	int t;cin >> t;
	while (t--)
	{
    
    
		int n, s;cin >> n >> s;
		int f = 0, sum = 0;
		int ma = 0, g=1;
		for (int i = 1;i <= n;i++)
		{
    
    
			cin >> lst[i];
		}
		int i;
		for (i = 1;i <= n;i++)
		{
    
    
			sum += lst[i];
			if (ma < lst[i])
			{
    
    
				ma = lst[i];
				g = i;
			}
			if (sum > s)break;

		}
		if (i>n)cout << 0 << endl;
		else
		{
    
    
			if (sum - lst[g] + lst[i + 1]<=s)cout << g << endl;
			else cout << 0 << endl;
		}
	}
}

猜你喜欢

转载自blog.csdn.net/asbbv/article/details/114381399