Verse For Santa

支洋
2023-12-01

Description
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 nn parts. It takes aiai seconds to recite the ii-th part. Vasya can’t change the order of parts in the verse: firstly he recites the part which takes a1a1 seconds, secondly — the part which takes a2a2 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 ss seconds. For example, if s=10s=10, a=[100,9,1,1]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 tt test cases.

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

The first line of each test case contains two integers nn and ss (1≤n≤105,1≤s≤1091≤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 nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the time it takes to recite each part of the verse.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

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

Input
3
7 11
2 9 1 3 18 1 4
4 35
11 9 10 7
1 8
5
Output
2
1
0
题目大意
给定一串数字,以及一个整数s,并给定条件:前 i 项之和 减 第 j 项 <= s(1<= j <= i )。求出当 i 最大时,j 的值,如存在多个,输出任意一个。特殊地,当所有数之和都小于 s 时,输出0.

思路
从第一项往后进行循环,将前 i 项累加得到sum,并且不断更新前 i 项中最大的一项的位置p,当sum大于 s 时,结束循环,此时便根据p得到答案。

#include <stdio.h>
int main()
{
    int t;
    int n,s;
    int arr[10001];
    int i,j;
    int sum=0;
    int max=-9999;
    int p=0;
    scanf("%d",&t);
    while(t--)
    {
    sum=0;p=0;max=0;
    scanf("%d%d",&n,&s);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
        sum+=arr[i];//得到前i项的和sum
        if(arr[i]>max)//更新前i项中最大的一项
        {
            max=arr[i];
            p=i;
        }
        if(sum>s)//sum大于s时,即代表i此时最大,无法再增加
        {
            break;
        }
    }
    if(sum>s)// 如果sum不大于s,即属于特殊情况
        printf("%d\n",p+1);
    else
        printf("0\n");
    sum=0;max=-9999;
    }
}

 类似资料:

相关阅读

相关文章

相关问答