【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。

13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define INF  1 << 30
#define MAXD 1000000 + 10
#define LEN 100 + 10
int array[MAXD];
int vis[LEN];
int N,M,K;
void init(){
    memset(vis,0,sizeof(vis));
    array[1] = 1;
    array[2] = 2;
    array[3] = 3;
    for(int i = 4 ; i <= N ; i++)
        array[i] = (array[i - 1] + array[i - 2] + array[i - 3]) % M + 1;
    return ;
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        int cnt = 0;
        int ok  = 0;
        int ans = INF;
        scanf("%d%d%d",&N,&M,&K);
        init();
        int l = 1 , r = 3;
        vis[1] = 1;
        vis[2] = 1;
        cnt = 2;
        if(K == 1){
            ans = 1;
        }
        else if(K == 2){
            ans = 2;
        }
        while(r <= N){
            int t = array[r];        /*右指针移动到的值*/
            if(t <= K && !vis[t]){   /*这个值在[1,k]并且没有访问过*/
                vis[t]++;
                cnt ++;
            }
            else if(t <= K && vis[t]){
                vis[t]++;
                int i;
                for(i = l ; i <= r ; i ++){ /*左指针移动*/
                    int t = array[i];
                    if(t <= K){
                        if(vis[t] > 1)
                            vis[t]--;
                        else
                            break;
                    }
                }
                l = i;
            }
            if(cnt == K)
                ans = min(ans,r - l + 1);
            r ++;
        }
        if(ans < INF)
        printf("Case %d: %d\n",Case ++,ans);
        else
        printf("Case %d: sequence nai\n",Case++);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值