LA 2678 subsequence

Source: Internet
Author: User
Tags cmath

Test instructions: gives n integers, given integer s, to find the shortest continuous sequence, so that their sum is greater than or equal to s

The first is to ask for a prefix and then use Lower_bound to find

The second type, the ruler,

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A  -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=100005; -  - intn,s; + intA[MAXN],SUM[MAXN]; -  + intMain () { A      while(SCANF ("%d%d", &n,&s)! =EOF) { atMemset (A,0,sizeof(a)); -memset (SUM,0,sizeof(sum)); -          for(inti =0; i < n;i++) scanf ("%d",&a[i]); -sum[0] = a[0]; -          for(inti =1; i < n;i++) Sum[i] = sum[i-1] +A[i]; -          in     //for (int i =0;i <= n;i++) -     //printf ("sum[%d] =%d\n", i,sum[i]); to         if(sum[n-1] <S) { +printf"0\n"); -         } the         Else{ *             intres =INF; $          for(ints =0; Sum[s] + s <= sum[n-1];s++){Panax Notoginseng             intt = lower_bound (sum,sum + n,sum[s] + s)-sum; -res = min (res,t-s); the         //printf ("res =%d\n", res); +         } Aprintf"%d\n", res); the         }         +     } -     return 0; $}
View Code

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <stack>6#include <vector>7#include <map>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A  -typedefLong LongLL; - Const intINF = (1<< -)-1; the Const intMod=1000000007; - Const intmaxn=100005; -  - intA[MAXN]; + intn,s; -  + voidsolve () { A     intt =0, S =0; at     intres =INF; -     intsum =0; -      for(;;) { -          while(t < n && Sum <S) { -Sum + = a[t++]; -         } in         if(Sum < S) Break; -res = min (res,t-s); toSum-= a[s++]; +     } -     if(Res > N) res =0; theprintf"%d\n", res); * } $ Panax Notoginseng intMain () { -      while(SCANF ("%d%d", &n,&s)! =EOF) { theMemset (A,0,sizeof(a)); +          for(inti =0; i < n;i++) scanf ("%d",&a[i]); A solve (); the     } +     return 0; -}
View Code

LA 2678 subsequence

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.