Loading

POJ-3061 Subsequence

Subsequence

找一个最短的区间,使得区间和大于等于s

尺取模板

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
int num[maxn];

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i=0; i<n; i++)
            scanf("%d", &num[i]);
        bool flag = false;
        int l = 0, r = 0, sum = 0, ans = n;
        while(1)
        {
            while(r < n && sum < m)
                sum += num[r++];
            int x = r - l;
            if(sum < m) break;
            flag = true;
            ans = min(ans, x);
            sum -= num[l++];
        }
        if(!flag) ans = 0;
        printf("%d\n", ans);
    }

    return 0;
}
posted @ 2022-05-13 01:21  dgsvygd  阅读(16)  评论(0编辑  收藏  举报