Smallest Sub-Array UVA-11536(可变滑动窗口)

本文介绍了如何使用可变滑动窗口算法解决UVA 11536题目,涉及数据结构中的动态数组和不固定大小的窗口操作,重点在于维护在特定条件下的最小连续子数组和。

Smallest Sub-Array UVA-11536

传送门 UVA 11536

思路

可变滑动窗口。右指针不断向后推进,左指针在cnt==k时向右推进直到cnt<k。在cnt==k时维护最小b-a+1的值。这个滑动窗口的特色在于其大小不固定。

#include <bits/stdc++.h>
typedef long double ld;
typedef long long ll;
#define pb push_back
#define mk make_pair
#define mt make_tuple
#define eb emplace_back
#define pob pop_back
#define rz resize
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define debug(a) cout<<#a<<"="<<a<<endl;
#define xx first
#define yy second
#define qwe(i,a,b) for(int i=a;i<=b;i++)
#define ewq(i,a,b) for(int i=a;i>=b;i--)
inline ll rr(){ll f=1,x=0;char ch;do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');return f*x;}
using namespace std;
const int INF=0x3f3f3f3f;
const ll mod[2]={int(1e9 + 7), 10007};
const int base[2]={29,31};
const int maxn=1e6+6;
ll qpow(ll x,ll n){ll ans=1;while(n>0){if(n&1)ans=ans*x%mod[1];x=x*x%mod[1];n>>=1;}return ans;}

int n,m,k,_;
int a[maxn],b[maxn];
inline void init(/* arguments */) {
    n=rr(),m=rr(),k=rr();
    mem(b,0);
    a[1]=1,a[2]=2,a[3]=3;
    for(int i=4;i<=n;i++) {
        a[i]=(a[i-1]+a[i-2]+a[i-3])%m+1;
    }
}
int main(int argc, char const *argv[]) {
    int t=rr();
    while (t--) {
        init();
        int ans=INF,cnt=0;
        int le=1;
        for(int i=1;i<=n;i++) {
            if(a[i]<=k && !b[a[i]]) cnt++;
            b[a[i]]++;
            while (cnt==k) {
                ans=min(ans,i-le+1);
                if(--b[a[le]]==0 && a[le]<=k) cnt--;
                le++;
            }
        }
        if(ans==INF) std::cout << "Case "<<++_<<": sequence nai" << '\n';
        else std::cout << "Case "<<++_<<": "<< ans << '\n';
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值