[POJ] 3061 subsequence (ruler)

Source: Internet
Author: User

Title Address: http://poj.org/problem?id=3061

Method One: Because the elements are greater than 0, so maintain the prefix and sum[i], there must be Sum[k]>sum[i] (k>i). This way the sequence beginning S is determined, with a binary lookup to determine the minimum value of the end T that makes the sequence and is not less than S.

Time complexity O (NLOGN).

1#include <cstdio>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <math.h>6#include <stdbool.h>7#include <time.h>8#include <stdlib.h>9#include <map>Ten#include <stack> One#include <queue> A#include <vector> - using namespacestd; - #defineCLR (x, y) memset (x,y,sizeof (×)) the #defineSQR (x) ((x) * (x)) - #defineRep (i,a,b) for (int i= (a); i<= (b); i++) - #defineLL Long Long - #defineINF 0x3f3f3f3f + #defineA First - #defineB Second + Const intn=1e5+131; A intN,s,a[n],sum[n]; at  - voidInit () - { -CLR (A,0); -CLR (SUM,0); - } in voidSolve () - { to     intCAs; +      -scanf"%d",&CAs); the      while(cas--){ * init (); $scanf"%d%d",&n,&s);Panax Notoginseng          for(intI=0; i<n;i++) { -scanf"%d",&a[i]); thesum[i+1]=sum[i]+A[i]; +         } A         intres=n+1; the          for(intk=0; sum[k]+s<=sum[n];k++){ +             intP=lower_bound (Sum+k,sum+n,sum[k]+s)-sum; -Res=min (res,p-k); $         } $         if(res>N) { -Puts"0"); -             Continue; the         } -printf"%d\n", res);Wuyi     } the      - } Wu  - intMain () About { $ solve (); -     return 0; -}

Method Two: Ruler methods. Time complexity O (n).

1#include <cstdio>2#include <iostream>3#include <string.h>4#include <algorithm>5#include <math.h>6#include <stdbool.h>7#include <time.h>8#include <stdlib.h>9#include <map>Ten#include <stack> One#include <queue> A#include <vector> - using namespacestd; - #defineCLR (x, y) memset (x,y,sizeof (×)) the #defineSQR (x) ((x) * (x)) - #defineRep (i,a,b) for (int i= (a); i<= (b); i++) - #defineLL Long Long - #defineINF 0x3f3f3f3f + #defineA First - #defineB Second + Const intn=1e5+131; A intN,s,a[n]; at  -  - voidSolve () - { -     intCAs; -      inscanf"%d",&CAs); -      while(cas--) { toscanf"%d%d",&n,&s); +          for(intI=0; i<n;i++) { -scanf"%d",&A[i]);  the         } *          $         intt=0, sum=0, h=0, res=n+1;Panax Notoginseng          -          while(true) { the               while(T<n && sum<s) { +sum+=a[t++]; A              } the             if(Sum<s) Break; +Res=min (res,t-h);  -sum-=a[h++]; $         } $         if(res>N) { -Puts"0"); -             Continue; the         } -printf"%d\n", res);Wuyi     } the } -  Wu intMain () - { About solve (); $      -     return 0; -}

[POJ] 3061 subsequence (ruler)

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.