旋转
原题链接
问题描述
解题思路
模拟输出即可
参考代码
import java.io.*;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int n, m;
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
m = Integer.parseInt(s[1]);
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
s = in.readLine().split(" ");
for (int j = 0; j < m; j++) {
arr[i][j] = Integer.parseInt(s[j]);
}
}
for (int i = 0; i < m; i++) {
for (int j = n - 1; j >= 0; j--) {
out.print(arr[j][i] + " ");
}
out.println();
}
out.flush();
in.close();
}
}
附近最小
原题链接
问题描述
解题思路
单调队列,比怂的优雅
参考代码
import java.io.*;
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)2e6 + 10, n, k;
static int[] a = new int[N];
public static void main(String[] args) throws Exception {
n = Integer.parseInt(in.readLine());
String[] s = in.readLine().split(" ");
for (int i = 0; i < n; i++) a[i] = Integer.parseInt(s[i]);
k = Integer.parseInt(in.readLine());
Deque<Integer> dq = new ArrayDeque<>();
dq.offerLast(a[0]);
for (int i = 1; i <= k; i++) {
while (!dq.isEmpty() && dq.peekLast() > a[i]) dq.pollLast();
dq.offerLast(a[i]);
}
for (int i = 0; i < n; i++) {
out.print(dq.peekFirst() + " ");
while (!dq.isEmpty() && i + k + 1 < n && dq.peekLast() > a[i + k + 1]) {
dq.pollLast();
}
dq.offerLast(a[i + k + 1]);
if (i - k >= 0 && dq.peekFirst() == a[i - k]) dq.pollFirst();
}
out.flush();
in.close();
}
}
扫地机器人
原题链接
问题描述
解题思路
二分答案
参考代码
import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
public class Main {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int N = (int)1e5 + 10, n, k;
static int[] a = new int[N];
public static boolean check(int x) {
if (x % 2 == 1) x -= 1;
int st = 1;
for (int i = 0; i < k; i++) {
int t = x;
if (a[i] > st) t -= (a[i] - st) * 2;
if (t < 0) return false;
st = a[i] + t / 2 + 1;
}
return st > n;
}
public static void main(String[] args) throws Exception {
String[] s = in.readLine().split(" ");
n = Integer.parseInt(s[0]);
k = Integer.parseInt(s[1]);
for (int i = 0; i < k; i++) {
a[i] = Integer.parseInt(in.readLine());
}
Arrays.sort(a, 0, k);
int l = 0, r = (int)2e6 + 10;
while (l < r) {
int mid = l + ((r - l) >> 1);
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
out.println(l);
out.flush();
in.close();
}
}
窗口
原题链接
问题描述
解题思路
todo:这里是解题思路
参考代码
todo:这里是参考代码