题目描述
博览馆正在展出由世上最佳的 M 位画家所画的图画。
wangjy想到博览馆去看这几位大师的作品。
可是,那里的博览馆有一个很奇怪的规定,就是在购买门票时必须说明两个数字,
a和b,代表他要看展览中的第 a 幅至第 b 幅画(包含 a 和 b)之间的所有图画,而门票
的价钱就是一张图画一元。
为了看到更多名师的画,wangjy希望入场后可以看到所有名师的图画(至少各一张)。
可是他又想节省金钱。。。
作为wangjy的朋友,他请你写一个程序决定他购买门票时的 a 值和 b 值。
输入格式
第一行是 N 和 M,分别代表博览馆内的图画总数及这些图画是由多少位名师的画
所绘画的。
其后的一行包含 N 个数字,它们都介于 1 和 M 之间,代表该位名师的编号。
输出格式
a和 b(a<=b) 由一个空格符所隔开。
保证有解,如果多解,输出a最小的。
输入输出样例
输入 #1
12 5
2 5 3 1 3 2 4 1 1 5 4 3
输出 #1
2 7
说明/提示
约定 30%的数据N<=200 , M<=20
60%的数据N<=10000 , M<=1000
100%的数据N<=1000000 , M<=2000
题意:给定一个数组a[ ] 1e6的长度
,求一个最短
区间[L,R]
,要求区间中所有数字必须出现至少一次
- 尺取法的经典题了, O ( N ) O(N) O(N)
- 考虑双指针
lef
和rig
,- 当窗口内
不满足
所有数都出现时右指针rig
扩张, - 当窗口内
满足
所有数都出现时左指针lef
收缩
- 当窗口内
- 注意到 1 ≤ M ≤ 2000 1\leq M \leq 2000 1≤M≤2000,所以开个数组记录个数即可
c++代码
read(n, m);
for(int i=1; i<=n; i++) read(a[i]);
int lef = 1, rig = 0, ansL = 1, ansR = n, cnt[2048] = { 0 };
int count = 0, ptag = INF;
while(lef <= n) {
while(rig+1<=n && count<m) {
rig ++;
if( ! cnt[a[rig]] ) { count ++; }
cnt[a[rig]] ++;
}
if(count==m && (rig-lef<ptag)) {
ptag = rig - lef;
ansL = lef, ansR = rig;
}
cnt[a[lef]] --;
if(cnt[a[lef]] == 0) count --;
lef ++;
}
printf("%d %d\n", ansL, ansR);
java代码
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
public class Main {
public static final boolean debug = true;
public static String INPATH = "C:\\Users\\majiao\\Desktop\\test.txt",
OUTPATH = "C:\\Users\\majiao\\Desktop\\out.txt";
public static StreamTokenizer tok;
public static BufferedReader cin;
public static PrintWriter cout;
public static long start_time = 0, out_time = 0;
public static int n, m, K, Q, MAXN = (int)1e6+7, INF = 0x3f3f3f3f;
public static int a[] = new int[MAXN];
public static void main(String[] args) throws IOException {
main_init();
if(debug) { start_time = System.currentTimeMillis(); }
if(false) { System.setOut(new PrintStream(OUTPATH)); }
n = read_int(); m = read_int();
for(int i=1; i<=n; i++) a[i] = read_int();
/**
int lef = 1, rig = 0, ansL = 1, ansR = n, ptag = INF;
int count = 0, cnt[] = new int[2048];
while(lef <= n) {
while(rig+1<=n && count<m) {
++ rig;
if(cnt[a[rig]] == 0) count ++;
cnt[a[rig]] ++;
}
if(count==m && ptag<(rig-lef)) {
ptag = rig - lef;
ansL = lef; ansR = rig;
}
cnt[a[lef]] --;
if(cnt[a[lef]] == 0) count --;
lef ++;
}
*/
int lef = 1, rig = 0, ansL = 1, ansR = n, cnt[] = new int[2048];
int count = 0, ptag = INF;
while(lef <= n) {
while(rig+1<=n && count<m) {
rig ++;
if( 0 == cnt[a[rig]] ) { count ++; }
cnt[a[rig]] ++;
}
if(count==m && (rig-lef<ptag)) {
ptag = rig - lef;
ansL = lef; ansR = rig;
}
cnt[a[lef]] --;
if(cnt[a[lef]] == 0) count --;
lef ++;
}
cout.printf("%d %d\n", ansL, ansR);
if(debug) {
out_time = System.currentTimeMillis();
cout.printf("run time : %d ms\n", out_time-start_time);
}
cout.flush();
}
public static void show(List<Object> list, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
for(Object x : list) {
cout.printf("[%s] ", x);
}
cout.printf("\n");
}
public static void show(Map<Object, Object> mp, Object... obj) {
cout.printf("%s : ", obj.length>0 ? obj[0] : "");
Set<Map.Entry<Object, Object>> entries = mp.entrySet();
for (Map.Entry<Object, Object> en : entries) {
cout.printf("[%s,%s] ", en.getKey(), en.getValue());
}
cout.printf("\n");
}
public static<T> void forarr(T arr[], int ...args) {
int lef = 0, rig = arr.length - 1;
if(args.length > 0) { lef = args[0]; rig = args[1]; }
cout.printf(" : ");
for( ; lef<=rig; lef++) {
cout.printf("[%s] ", args[lef]);
}
cout.printf("\n");
}
public static void main_init() {
try {
if (debug) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream(INPATH)));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
// cout = new PrintWriter(OUTPATH);
tok = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String next_str() {
try {
tok.nextToken();
if (tok.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tok.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf((int)tok.nval);
} else if (tok.ttype == StreamTokenizer.TT_WORD) {
return tok.sval;
} else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static int read_int() {
String tmp_next_str = next_str();
return null==tmp_next_str ? -1 : Integer.parseInt(tmp_next_str);
}
public static long read_long() { return Long.parseLong(next_str()); }
public static double read_double() { return Double.parseDouble(next_str()); }
public static BigInteger read_big() { return new BigInteger(next_str()); }
public static BigDecimal read_dec() { return new BigDecimal(next_str()); }
static class Edge implements Comparable<Edge> {
int u, v, w;
@Override
public int compareTo(Edge o) { return w - o.w; }
}
class Pair implements Comparable<Pair>{
int fst, sec;
public Pair() { }
public Pair(int fst, int sec) {
this.fst = fst;
this.sec = sec;
}
@Override
public int compareTo(Pair o) {
return fst - o.fst == 0 ? sec - o.sec : fst - o.fst;
}
}
}