题目描述
一个含有 nnn 项的数列,求出每一项前的 mmm 个数到它这个区间内的最小值。若前面的数不足 mmm 项则从第 111 个数开始,若前面没有数则输出 000。
输入格式
第一行两个整数,分别表示 nnn,mmm。
第二行,nnn 个正整数,为所给定的数列 aia_iai。
输出格式
nnn 行,每行一个整数,第 iii 个数为序列中 aia_iai 之前 mmm 个数的最小值。
输入输出样例
输入 #1
6 2
7 8 1 4 3 2
输出 #1
0
7
7
1
1
3
说明/提示
对于 100%100%100% 的数据,保证 1≤m≤n≤2×1061\le m\le n\le2\times10^61≤m≤n≤2×106,1≤ai≤3×1071\le a_i\le3\times10^71≤ai≤3×107。
题意:求滑动窗口的里的最小值
解法一 : 单调队列 O(N)
用双端队列deque
模拟窗口
- deque里存储结构体
{下标,值}
- 先把所有队尾
back
比当前值a[i]
大的全部弹出,while(back > a[i]) deque.pop_back()
a[i]
入队列,deque.push_back(a[i])
- 如果队头的下标
front_id
不在窗口区间[i-m, i]
内就弹出队头
洛谷大佬@lcglcg
的题解:https://www.luogu.com.cn/blog/lcglcg/solution-p1440
signed main() {
#ifdef debug
freopen("test.txt", "r", stdin);
clock_t stime = clock();
#endif
scanf("%d %d ", &n, &m);
for(int i=1; i<=n; i++) scanf("%d ", a+i);
deque<pair<int,int> > q; //deque模拟窗口
printf("0\n");
for(int i=1; i<n; i++) {
//先把队尾大于a[i]的弹出干净
while(!q.empty() && q.back().second>a[i]) q.pop_back();
q.push_back({i, a[i]}); //压入队尾
//再把队头所有不在区间 [i-m, i] 内的弹出干净
while(!q.empty() && q.front().first+m<=i) q.pop_front();
printf("%d\n", q.front().second);
}
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}
解法二:小根堆优化 O(N log N)
用priority_queue
模拟窗口,
堆
里存储结构体{下标,值}
- 压入值后,把所有
不在窗口区间[i-m, i]
内的堆顶弹出
signed main() {
#ifdef debug
freopen("test.txt", "r", stdin);
clock_t stime = clock();
#endif
scanf("%d %d ", &n, &m);
#if 1
for(int i=1; i<=n; i++) scanf("%d ", a+i);
// show(n, m);
// forarr(a, 1, n);
#define PII (pair<int,int>)
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int> > > q;
printf("0\n");
for(int i=1; i<n; i++) {
#define fstid (q.front().first)
#define fstval (q.front().second)
#define topval (q.top().first)
#define topid (q.top().second)
q.push({a[i], i});
while(topid <= i-m) q.pop();
printf("%d\n", topval);
}
#endif
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}
用java完美MLE
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)16, INF = 0x3f3f3f3f,
pre[] = new int[MAXN], sum[] = new int[MAXN], POS = 50000;
public static byte buf[] = new byte[MAXN];
public static int fa(int x) { return pre[x]==x ? x : (pre[x]=fa(pre[x])); }
public static void union_xy(int x, int y) {
x = fa(x); y = fa(y);
if(x != y) {
sum[x] += sum[y];
pre[y] = x;
}
}
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();
cout.printf("0\n");
int x;
PriorityQueue<Pair> q = new PriorityQueue<>();
for(int i=1; i<n; i++) {
x = read_int();
Pair now = new Pair(i, x);
q.add(now);
Pair top = null;
while(q.peek().fst+m <= i) q.poll();
cout.printf("%d\n", q.peek().sec);
}
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; }
}
static Edge a[] = new Edge[MAXN];
static 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;
return sec - o.sec;
}
}
}
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)16, INF = 0x3f3f3f3f,
pre[] = new int[MAXN], sum[] = new int[MAXN], POS = 50000;
public static byte buf[] = new byte[MAXN];
public static int fa(int x) { return pre[x]==x ? x : (pre[x]=fa(pre[x])); }
public static void union_xy(int x, int y) {
x = fa(x); y = fa(y);
if(x != y) {
sum[x] += sum[y];
pre[y] = x;
}
}
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();
cout.printf("0\n");
int x;
Deque<Pair> q = new LinkedList<>();
for(int i=1; i<n; i++) {
x = read_int();
while(!q.isEmpty() && q.peekLast().sec > x) q.pollLast();
q.addLast(new Pair(i, x));
if(q.peekFirst().fst+m <= i) q.pollFirst();
cout.printf("%d\n", q.peek().sec);
}
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; }
}
static Edge a[] = new Edge[MAXN];
static 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;
return sec - o.sec;
}
}
}