UVa 11536 Smallest Sub-Array (水题, 滑动窗口)

题意:给定 n 个由0~m-1的整数组成的序列,输入 k ,问你找出连续的最短序列,使得这个序列含有1-k的所有整数。

析:这个题,很简单么,只要从头开始扫一遍就OK,时间复杂度为O(n)。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e6 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int a[maxn];
int vis[1005];
void init(){
    a[0] = 1;  a[1] = 2; a[2] = 3;
    for(int i = 3 ; i < n; ++i)
        a[i] = (a[i-1]+a[i-2]+a[i-3]) % m + 1;
}

int main(){
    int T;  cin >> T;
    for(int kase = 1; kase <= T; ++kase){
        int k;
        scanf("%d %d %d", &n, &m, &k);
        init();
        int ans = INF;
        int s = 0, e = 0;
        memset(vis, 0, sizeof(vis));
        int cnt = 0;
        while(e < n){
            while(e < n && cnt < k){
                if(!vis[a[e]] && a[e] <= k)  ++cnt;
                ++vis[a[e]];
                ++e;
            }
            if(cnt == k)  ans = min(ans, e-s);
            --vis[a[s]];
            if(!vis[a[s]] && a[s] <= k)  --cnt;
            ++s;
        }
        printf("Case %d: ", kase);
        ans == INF ? printf("sequence nai\n") : printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-01 10:44:32

UVa 11536 Smallest Sub-Array (水题, 滑动窗口)的相关文章

hdu 5532 Almost Sorted Array (水题)

Almost Sorted Array Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 8541    Accepted Submission(s): 1982 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort

Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解

题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 输入: nums = [1,3,-1,-3,5,3,6,7], 和 k = 3 输出: [3,3,5,5,6,7] 解释: 滑动窗口的位置|最大值 -|- [1 3 -1] -3 5

UVA 11100 The Trip, 2007 水题一枚

题目链接:UVA - 11100 题意描述:n个旅行箱,形状相同,尺寸不同,尺寸小的可以放在尺寸大的旅行箱里.现在要求露在最外面的旅行箱的数量最少的同时满足一个旅行箱里放的旅行箱的数量最少.求出这样满足要求的任意一种方案. 算法分析:首先我们可以确定最少的旅行箱的数量cnt:即n个旅行箱里按照尺寸大小分类(尺寸相同的在同一类),数量最多的那一类的数量.然后把cnt看成有cnt个堆,第二个要求就是要让这cnt个堆里最大旅行箱数量最小,直接用vector处理即可. 等AC之后突然想到,三个旅行箱尺寸

UVA 11947 Cancer or Scorpio 水题

Cancer or Scorpio Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3098 Description Alice and Michael is a young couple who are planning on having their

uva 10976 fractions again(水题)——yhx

1 #include<cstdio> 2 int a[30010],b[30010]; 3 int main() 4 { 5 int i,j,k,l,m,n,x,y,z; 6 while (scanf("%d",&k)==1) 7 { 8 n=0; 9 for (i=k+1;i<=2*k;i++) 10 if ((k*i)%(i-k)==0) 11 { 12 a[++n]=k*i/(i-k); 13 b[n]=i; 14 } 15 printf("%

uva 11536 - Smallest Sub-Array

题目大意:按照题目中的要求构造出一个序列,找出最短的子序列,包含1~k. 解题思路:先根据题目的方法构造出序列,然后用Towpointer的方法,用v[i]来记录当前[l, r]中有几个i:当r移动时,出现v[i] == 1时, c++(用来记录有几个1~k的数字):当c == k 时,就要移动l,当出现v[i] == 0时,c--. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #in

UVA 541 Error Correction【水题】

Error Correction Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Problem Description A boolean matrix has the parity property when each row and each column has an

GCD LCM UVA - 11388 (思维。。水题)

两个数的最小公倍数和最大公约数肯定是倍数关系 然后又让求使得a最小  因为 a = m * gcd 令m = 1 时 a取得最小  即gcd 则b = lcm #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <cmath> #de

UVa 1586 Molar mass --- 水题

UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现一个从字符型的数到整型的数的转换函数,再将输入的串从头到尾扫描,遇到字母,则进一步扫描后面的数字的区间, 再调用函数进行转换,再乘以其的原子质量,最后累加到sum中即可. /* UVa 1586 Molar mass --- 水题 */ #include <cstdio> #include <