zoukankan      html  css  js  c++  java
  • ZOJ

    题意:给定N个数,求和大于等于S的最短连续子序列的长度。

    分析:滑动窗口即可。两种写法。

    1、

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN];
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            int N, S;
            scanf("%d%d", &N, &S);
            int tot = 0;
            for(int i = 0; i < N; ++i){
                scanf("%d", &a[i]);
                tot += a[i];
            }
            if(tot < S){
                printf("0
    ");
                continue;
            }
            int st = 0, et = 0;
            int sum = 0;
            int cnt = 0x7f7f7f7f;
            while(et < N){
                while(et < N && sum + a[et] < S){
                    sum += a[et];
                    ++et;
                }
                cnt = min(cnt, et - st + 1);
                sum -= a[st];
                ++st;
            }
            printf("%d
    ", cnt);
        }
        return 0;
    }
    

    2、

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 100000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    int a[MAXN];
    int main(){
        int T;
        scanf("%d", &T);
        while(T--){
            int N, S;
            scanf("%d%d", &N, &S);
            int tot = 0;
            for(int i = 0; i < N; ++i){
                scanf("%d", &a[i]);
                tot += a[i];
            }
            if(tot < S){
                printf("0
    ");
                continue;
            }
            int st = 0, et = 0;
            int sum = 0;
            int cnt = 0x7f7f7f7f;
            for(int i = 0; i < N; ++i){
                sum += a[i];
                while(sum - a[st] >= S){
                    sum -= a[st];
                    ++st;
                }
                if(sum >= S){
                    cnt = min(cnt, i - st + 1);
                    sum -= a[st];
                    ++st;
                }
            }
            printf("%d
    ", cnt);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Oracle11g工具
    Oracle数据库中scott用户的所有表结构
    Oracle数据库手动解锁scott用户
    Oracle数据库实例的删除和安装
    Oracle数据库的安装
    Oracle数据库发展历史
    禁止浏览器中双击选中元素的解决方法
    Window 设置pm2开机自启动服务
    通过node创建web服务器----express插件打包上线
    vue项目优化----通过externals加载外部CDN资源
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7115373.html
Copyright © 2011-2022 走看看