Thinking about it:

  我的思路跟sliding window有点类似。假设已经确定了一个区间[l, r],序列中从 l 到 r 恰好包含了[1, K]的各个元素,则从 r 开始继续迭代序列的各个位置,如果发现了1到K的数,则做以下处理:

  如果 这个数 刚好是 l 位置上的数,那么就意味着这个区间可能缩短,则同时更新 l 和 r,计算区间长度的变化。

  如果 这个数 不是 l 上的数,那么即使 更新了 r 那也不能使答案更好,所以可以不做处理。

  那么第一个符合条件的[l, r]可以直接迭代得出,如果迭代一次都不能发现[1, K]的各个数,那么这个序列肯定是不能满足的要求。

PS:

  虽然AC了这题,但是感觉对这道题的题解表述上还有些欠缺。

Code:

/**
* AC @ Sep 11th 2015
* Run Time : 0.739s
*/
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 50;
int var[MAXN*1000];
int Case = 0;
int N, M, K; void init() {
for (int i = 1; i <= 3; ++i) {
var[i] = i;
}
int sum = 6;
for (int i = 4; i <=N ; ++i) {
var[i] = sum % M + 1;
sum -= var[i-3];
sum += var[i];
}
} void done() {
int pos[MAXN] = {0};
int counter = 0;
for (int i = 1; i <= N && counter < K; ++i) {
if (var[i] >= 1 && var[i] <= K && !pos[var[i]]) {
++ counter;
}
pos[var[i]] = max(pos[var[i]], i);
}
if (counter < K) {
cout << "Case " << (++Case) << ": sequence nai" << endl;
return ;
}
int minPos = MAXN * 1000, maxPos = -1;
for (int i = 1; i <= K; ++i) {
minPos = min(minPos, pos[i]);
maxPos = max(maxPos, pos[i]);
}
int ans = maxPos - minPos + 1;
for (int i = 1; i <= N; ++i) {
if (var[i] >= 1 && var[i] <= K) {
if (pos[var[i]] == minPos || pos[var[i]] == maxPos || i > maxPos) {
pos[var[i]] = i;
minPos = MAXN * 1000, maxPos = -1;
for (int j = 1; j <= K; ++j) {
minPos = min(minPos, pos[j]);
maxPos = max(maxPos, pos[j]);
}
ans = min(ans, maxPos - minPos + 1);
}
}
}
cout << "Case " << (++Case) << ": " << ans << endl;
} void work() {
cin >> N >> M >> K;
init();
done();
} int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T --) {
work();
}
return 0;
}

  

  

uva11536 Smallest Sub-Array的更多相关文章

  1. [Javascript] Implement zip function

    1. Use a for loop to traverse the videos and bookmarks array at the same time. For each video and bo ...

  2. 《Algorithms Unlocked》读书笔记2——二分查找和排序算法

    <Algorithms Unlocked>是 <算法导论>的合著者之一 Thomas H. Cormen 写的一本算法基础,算是啃CLRS前的开胃菜和辅助教材.如果CLRS的厚 ...

  3. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

  4. CF980C Posterized 贪心 二十五

    Posterized time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. Arrange an Array to Form a Smallest Digit

    /** * Input an array of positive integers, arrange the integers to form new digits, * and output the ...

  6. [Algorithm] Find Nth smallest value from Array

    Q1: Find the smallest value from array: function findMin(arr) { let min = arr[0]; for (let i = 1; i ...

  7. 41.把数组排成最小的数[Sort array to smallest value]

    [题目] 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个.例如输入数组{3,32,  321},则输出这两个能排成的最小数字321323.请给出解决问题的算法,并证明该 ...

  8. Kth Smallest Element in Unsorted Array

    (referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...

  9. luogu题解 UVA11536 【Smallest Sub-Array】最短set区间&滑动窗口

    题目链接: https://www.luogu.org/problemnew/show/UVA11536 题目大意: 给定一个\(N,M,K\),构造这样的数列: \(x[1]=1,x[2]=2,x[ ...

随机推荐

  1. jsp-forward跳转

    在Web中可以使用<jsp:forward>指令,将一个用户的请求(request)从一个页面传递到另一个页面,即完成跳转的操作. 1.调整前页:tiaozhuan_a.jsp 代码: & ...

  2. 项目适配iOS9遇到的一些问题及解决办法

    1.网络请求报错.升级Xcode 7.0发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Secur ...

  3. HOJ1087

    Self Numbers My Tags   (Edit)   Source : ACM ICPC Mid-Central USA 1998   Time limit : 5 sec   Memory ...

  4. shell查看并修复网络连接

    1.  shell监控网卡状态,故障时自动重启网卡 http://blog.slogra.com/post-425.html cat fix_eth0.sh #!/bin/bash check_and ...

  5. Struts学习之自定义拦截器

    * 所有的拦截器都需要实现Interceptor接口或者继承Interceptor接口的扩展实现类    * 要重写init().intercept().destroy()方法        * in ...

  6. fiddler--手机https

    1.访问fiddler的主讲加端口,比如我的是:http://192.168.1.103:8080 在点击fiddlerroot  certificate 下载安装证书即可

  7. 面试题之 query转为obj

    要注意处理编码后的字串  对于a=123要得到number形的值 function parseQueryString(url) { var obj = {}; var query = url.sear ...

  8. Vim 实用技术,第 1 部分: 实用技巧(转)

    原文链接:http://blog.jobbole.com/20604/ 0. Vim 简介 作为开源世界最重要的编辑器之一(另一个是 Emacs),Vim 以其强大的功能和可定制能力被众多开发者所喜爱 ...

  9. API各函数作用简介

    API各函数作用简介 1.控件与消息函数 AdjustWindowRect 给定一种窗口样式,计算获得目标客户区矩形所需的窗口大小 AnyPopup 判断屏幕上是否存在任何弹出式窗口 ArrangeI ...

  10. 关于对象和this、new

    //创建一个猫类 function Cat(name,color,eyeColor){ //上面处Cat首字母大写表示创建一个‘类别’叫Cat类.假如首字母小写cat则是创建一个cat的函数: thi ...