Subsequence
http://poj.org/problem?id=3061
1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdio> 5 #include <algorithm> 6 using namespace std; 7 #define LL long long 8 const int maxn = 100010; 9 const int inf = 0x3f3f3f3f; 10 LL a[maxn], b[maxn]; 11 int main(){ 12 int t; 13 //freopen("in.txt", "r", stdin); 14 scanf("%d", &t); 15 while(t--){ 16 int n, s; 17 scanf("%d %d", &n, &s); 18 b[0] = 0; 19 for(int i = 1; i <= n; i++) { 20 scanf("%lld", &a[i]); 21 b[i] = b[i - 1] + a[i]; 22 } 23 int ans = inf; 24 for(int i = 1; i <= n; i++){ 25 if(b[i] < s) continue; 26 int temp = b[i] - s; 27 int id = lower_bound(b + 1, b + 1 + n, temp) - b; 28 if(b[id] == temp) while(b[id + 1] == temp) id++; 29 else{ 30 id--; 31 } 32 //cout<<i<<" ---------- "<<id<<" =++"<<endl; 33 ans = min(ans, i - id); 34 } 35 if(ans == inf) ans = 0; 36 printf("%d\n", ans); 37 } 38 }
尺取~
1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdio> 5 #include <algorithm> 6 using namespace std; 7 #define LL long long 8 const int maxn = 100010; 9 const int inf = 0x3f3f3f3f; 10 LL a[maxn]; 11 int main(){ 12 int t; 13 //freopen("in.txt", "r", stdin); 14 scanf("%d", &t); 15 while(t--){ 16 int n, s; 17 scanf("%d %d", &n, &s); 18 for(int i = 0; i < n; i++) scanf("%lld", &a[i]); 19 int i = 0, j = 0; 20 int sum = 0; 21 int ans = inf; 22 while(1){ 23 while(sum < s && j < n) sum += a[j++]; 24 if(sum < s) break; 25 ans = min(ans, j - i); 26 sum -= a[i++]; 27 } 28 if(ans == inf) ans = 0; 29 printf("%d\n", ans); 30 } 31 }