uva11536 Smallest Sub-Array
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的更多相关文章
- [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 ...
- 《Algorithms Unlocked》读书笔记2——二分查找和排序算法
<Algorithms Unlocked>是 <算法导论>的合著者之一 Thomas H. Cormen 写的一本算法基础,算是啃CLRS前的开胃菜和辅助教材.如果CLRS的厚 ...
- T - Posterized(贪心思维)
Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...
- CF980C Posterized 贪心 二十五
Posterized time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Arrange an Array to Form a Smallest Digit
/** * Input an array of positive integers, arrange the integers to form new digits, * and output the ...
- [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 ...
- 41.把数组排成最小的数[Sort array to smallest value]
[题目] 输入一个正整数数组,将它们连接起来排成一个数,输出能排出的所有数字中最小的一个.例如输入数组{3,32, 321},则输出这两个能排成的最小数字321323.请给出解决问题的算法,并证明该 ...
- Kth Smallest Element in Unsorted Array
(referrence: GeeksforGeeks, Kth Largest Element in Array) This is a common algorithm problem appeari ...
- luogu题解 UVA11536 【Smallest Sub-Array】最短set区间&滑动窗口
题目链接: https://www.luogu.org/problemnew/show/UVA11536 题目大意: 给定一个\(N,M,K\),构造这样的数列: \(x[1]=1,x[2]=2,x[ ...
随机推荐
- 指定hive输出格式
0.11版本以前: sed -e 's/\x01/|/g' file 0.11版本以后: insert overwrite local directory '/opt/aimcpro/libc/tes ...
- C - 字符识别?
C - 字符识别? Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Sta ...
- Android 电源系列小结s
package com.ritterliu.newBatteryWidget; import android.app.Activity; import android.app.Service; imp ...
- CSS 与 HTML5 响应式图片
什么是响应式图片? 响应式图片是指:用户代理根据输出设备的分辨率不同加载不同类型的图片,不会造成带宽的浪费.同时,在改变输出设备类型或分辨率时,能及时加载对应类型的图片. CSS3 响应式图片 对于很 ...
- poj 3228 Gold Transportation 二分+网络流
题目链接 给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中. 城市之间有路相连, 每条路有长度. 因为有些城市的货物量大于仓库的容量, 所以要运到 ...
- scanf一次给多个变量赋值
本节课程笔记: 一是对多个变量进行赋值,二是对非法输入的值做正确处理(处理方式了解即可,相关函数知识后期讲解),三是美化scanf代码加入输出说明. /* Name:scanf一次给多个变量赋值 Co ...
- Delphi2010的RTTI增强
Delphi编译的文件体积增大了很多.很大一部分原因是因为Delphi2010默认提供了全信息的RTTI. 每一个数据类型都有全部运行时信息.例如可以在运行时获得结构体的成员以及成员类型等. 这个功能 ...
- .NET(C#):在数组成员上加入XmlElement特性
原文 www.cnblogs.com/mgen/archive/2011/12/04/2276131.html 当对如下类进行XML序列化时: publicclassa { }; } 结果会是: &l ...
- sublime的20个插件
SublimeText是一款非常精巧的文本编辑器,适合编写代码.做笔记.写文章.它用户界面十分整洁,功能非同凡响,性能快得出奇.这些非常棒的特性 包括任意跳转(Goto Anything).多重选择( ...
- MSP430与ATK-NEO-6M GPS模块
近短时间在网上买了一个GPS模块,正好正在学习MSP430单片机,于是决心将GPS模块与MSP430结合起来,同时将代码贴出来,发现网上搜到好多资料都要注册才能下载,有些还要钱.自己动脑,才能自娱自乐 ...