POJ-3061 Subsequence

原题链接:

https://vjudge.net/problem/POJ-3061

AC代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
const int INF = 0x3fffffff;
typedef long long ll;
typedef unsigned long long ull;

void __init__()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
}

const int maxn = 100100;
int seq[maxn];
int main()
{
    // __init__();
    int t;
    while (~scanf("%d", &t))
    {
        while (t--)
        {
            int n;
            int s;
            scanf("%d %d", &n, &s);
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &seq[i]);
            }

            int l = 0, r = 0;
            int ans = INF;
            int sum1 = 0;

            for (int l = 0; l < n; l++)
            {
                while (sum1 < s && r < n)
                {
                    sum1 += seq[r++];
                }
                if (sum1 < s)
                    break;
                ans = min(ans, r - l);
                sum1 -= seq[l];
            }
            if (ans < INF)
                cout << ans << endl;
            else
                cout << "0\n";
        }
    }
    return 0;
}

尺取法,按说是很简单的一道题,但训练赛的时候疯狂wa,赛后我找了原题的测试数据,发现当数据量很大时,直接就不输出了,很离谱。总之当时的尺取用的很别扭,我是先找到一个满足条件的r,然后l++直到不满足sum<s,再右移一个r,理论上好像没啥问题,但就是不对(我现在也没想明白为啥不对,有发现问题的dalao请评论指点一下我),后来转变了思路,也是先找满足条件的r,然后左移一个l,再找r。我也不知道当时为啥就是绕不过来,非要多此一举,这道题当时做的我怀疑人生,赛后虽然知道了正确的解法,但还是不理解之前的代码为啥错,下面附上错误代码,希望有dalao能发现问题批评指正orz:
错误代码:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int INF = 0x3fffffff;
typedef long long ll;
typedef unsigned long long ull;

void __init__()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
}

const ll maxn = 100100;
int seq[maxn];
int main()
{
    // __init__();
    int t;
    while (~scanf("%d", &t))
    {
        while (t--)
        {
            int n;
            ll s;
            scanf("%d %lld", &n, &s);
            memset(seq, 0, sizeof(seq));
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &seq[i]);
            }

            int l = 0, r = 0;
            int ans = INF;
            int len;
            int fa = 0;
            ll sum1 = 0;
            while (sum1 < s && r < n)
            {
                sum1 += seq[r];
                r++;
            }
            //若全部加起来都没有s大
            if (sum1 < s)
            {
                printf("0\n");
                return 0;
            }
            r = r - 1;

            while (r < n && l <= r)
            {
                while (sum1 >= s && l <= r)
                {
                    sum1 -= seq[l];
                    l++;
                }
                len = r - l + 2;
                if (ans > len)
                {
                    ans = len;
                }
                r++;
                sum1 += seq[r];
            }
            cout << ans << endl;
        }
    }
    return 0;
}

这个错误代码是当数据量很大的时候直接不输出,分析了半天时间空间复杂度也没想出来问题所在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值