zoukankan      html  css  js  c++  java
  • 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
    ", ans);
    37     }
    38 }
    View Code

    尺取~

     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
    ", ans);
    30     }
    31 }
    View Code
  • 相关阅读:
    Enum 枚举值 (一) 获取描述信息
    sqlbulkcopy 批量更新 数据库
    linq partition by
    C#委托的介绍(delegate、Action、Func、predicate)ga
    安装Tomcat
    安装 oracle
    468 Validate IP Address 验证IP地址
    467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串
    464 Can I Win 我能赢吗
    463 Island Perimeter 岛屿的周长
  • 原文地址:https://www.cnblogs.com/yijiull/p/7955655.html
Copyright © 2011-2022 走看看