B. Verse For Santa----思维+模拟

New Year is coming! Vasya has prepared a New Year’s verse and wants to recite it in front of Santa Claus.

Vasya’s verse contains n parts. It takes ai seconds to recite the i-th part. Vasya can’t change the order of parts in the verse: firstly he recites the part which takes a1 seconds, secondly — the part which takes a2 seconds, and so on. After reciting the verse, Vasya will get the number of presents equal to the number of parts he fully recited.

Vasya can skip at most one part of the verse while reciting it (if he skips more than one part, then Santa will definitely notice it).

Santa will listen to Vasya’s verse for no more than s seconds. For example, if s=10, a=[100,9,1,1], and Vasya skips the first part of verse, then he gets two presents.

Note that it is possible to recite the whole verse (if there is enough time).

Determine which part Vasya needs to skip to obtain the maximum possible number of gifts. If Vasya shouldn’t skip anything, print 0. If there are multiple answers, print any of them.

You have to process t test cases.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains two integers n and s (1≤n≤105,1≤s≤109) — the number of parts in the verse and the maximum number of seconds Santa will listen to Vasya, respectively.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the time it takes to recite each part of the verse.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
For each test case print one integer — the number of the part that Vasya needs to skip to obtain the maximum number of gifts. If Vasya shouldn’t skip any parts, print 0.

Example
inputCopy

3
7 11
2 9 1 3 18 1 4
4 35
11 9 10 7
1 8
5
outputCopy
2
1
0
Note
In the first test case if Vasya skips the second part then he gets three gifts.

In the second test case no matter what part of the verse Vasya skips.

In the third test case Vasya can recite the whole verse.

题意:n个背书的时间,老师只听m秒,必须按照顺序从1--n背.
你只有一次机会可以不背某个片段。输出你不背的那个片段下标,使得背的片段数最大。


解析:贪心,一直背下去,背不下去的时候就把前面的最大值去掉。答案就出来了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+100;
int a[N],t,n,m;
int main()
{
	 scanf("%d",&t);
	 while(t--)
	 {
	 	ll sum=0;
	 	scanf("%d %d",&n,&m);
	 	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	 	int mx=0;
	 	int pos=0;
	 	int res=0;
	 	for(int i=1;i<=n;i++)
	 	{
	 		sum+=(ll)a[i];
	 		if(a[i]>mx)
	 		{
	 			mx=a[i];
	 			pos=i;
			 }
			if(sum>m&&sum-a[pos]<=m)  res=pos;
		 }
		 cout<<res<<endl;
	  } 
}


发布了284 篇原创文章 · 获赞 6 · 访问量 3770

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104052629