• 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
  • 相关阅读:
    程序的了解
    Oracle VM VirtualBox虚拟网卡消失解决方法
    YARN 运维、巡检、监控、调优、排障
    HDFS巡检、监控、调优、排障
    Windows CMD命令大全
    [HDU]6356 Glad You Came(ST表)
    [BZOJ] 1019 [SHOI2008]汉诺塔
    树上叶子之间点对距离平方和
    [BZOJ]1026[SCOI2009]windy数
    [计蒜客]A1542 The Maximum Unreachable Node Set
  • 原文地址:https://www.cnblogs.com/yijiull/p/7955655.html
Copyright © 2020-2023  润新知