Thursday, February 20, 2020

POJ.3061 Subsequence

1.Problem
http://poj.org/problem?id=3061

2.Idea
Two Pointers method

3.Source
 int n, S;  
 int a[1000006];  
 void solve()  
 {  
      int res = n + 1;  
      int s = 0, t = 0, sum = 0;  
      for (;;) {  
           while (t < n && sum < S) sum += a[t++];  
           if (sum < S) break;  
           res = min(res, t - s);  
           sum -= a[s++];  
      }  
      if (res > n) res = 0;  
      printf("%d\n", res);  
 }  
 int main()  
 {  
      int t;  
      scanf("%d", &t);  
      while (t--) {  
           scanf("%d%d", &n, &S);  
           for (int i = 0; i < n; i++) scanf("%d", &a[i]);  
           solve();  
      }  
      return 0;  
 }  

No comments:

Post a Comment