Counter Strike (ruler algorithm)

Source: Internet
Author: User
Tags sessions

Description

The 17,125 teams have N fighters, numbered three ... N, each warrior corresponds to a fighting force P (0<=p<=100). In order to maintain the normal functioning of the force, it was stipulated that only a number of successive soldiers with serial numbers could be assigned each time. During the two sessions, reactionary forces gathered in the suburbs to try to create a terrorist event. In order to maintain the normal development of the two sessions, the Commander-in-chief ordered 17,125 units to annihilate the terrorist. (Assuming all fighters can participate in this mission)

Input

The first line gives the integer t (1<=t<=10), which indicates that there is a T group of data. The first row of each group of data two integers n (1<=n<=100000) indicates that there are N Warriors, M (0<m<100000) to eliminate the enemy's combat effectiveness, the second row gives n integers, number I represents the warrior's combat effectiveness Pi (0&LT;PI &LT;100).

Output

For each set of test data, the single-line output requires at least the number of warriors required to complete this task. If this task cannot be completed, output-1.

Sample Input
310 208 2 1 13 2 5 9 6 3 46 308 2 1 5 3 42 53 2
Sample Output
3-12

problem-solving ideas: This is a very classic ruler of the application of algorithms, which is my first contact with this algorithm, before the violence with a pitch, it is time overrun, and then I will be a summary of this algorithm.
Ruler Take algorithm Description: http://www.cnblogs.com/wkfvawl/p/9016281.html


First on the code:
1 #include<stdio.h>2 int main ()3 {4 int t,n,m,ans,min,sum,i,j,w;5int a[100010];6scanf"%d",&t);7      while(t--)8     {9scanf"%d%d",&n,&m);Ten          for(i=0;i<n;i++) One         { Ascanf"%d",&a[i]); -         } -j=0; thew=0; -sum=0; -Ans=n+1; -          while(1) +         { -              while(j<n&&sum<m) +             { Asum=sum+a[j++]; at             } -             if(sum<m) -             { -                  Break; -             } -min=j-W; in             if(min>ans) -             { tomin=ans; +             } -ans=min; thesum=sum-A[w]; *w++; $         }Panax Notoginseng         if(ans==n+1) -         { theprintf"-1\n"); +         } A         Else the         { +printf"%d\n", ans); -         } $     } $     return0; -}

Counter Strike (ruler algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.