美团校园招聘笔试真题--第一天

This commit is contained in:
huangge1199@hotmail.com 2021-08-23 21:56:58 +08:00
parent 6131c97c37
commit 6db6546a2c

View File

@ -1,9 +1,6 @@
package study.plan.meituan.meituan002; package study.plan.meituan.meituan002;
import javafx.util.Pair;
import java.io.*; import java.io.*;
import java.util.*;
/** /**
* @ClassName Solution * @ClassName Solution
@ -14,80 +11,59 @@ import java.util.*;
class Solution { class Solution {
static Solution.Scanner sc;
static PrintWriter pw;
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
sc = new Solution.Scanner(System.in); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
solve();
pw.close();
}
public static void solve() throws IOException { int n = Integer.parseInt(reader.readLine());
int num = sc.nextInt(); int[] arr = new int[n + 1];
int index = 0; int[] prev = new int[n + 1];
int[] sums = new int[num]; String[] w = reader.readLine().split(" ");
while (index < num) { for (int i = 1; i <= n; i++) {
sums[index] = index == 0 ? sc.nextInt() : sums[index - 1] + sc.nextInt(); arr[i] = Integer.parseInt(w[i - 1]);
index++; prev[i] += prev[i - 1] + arr[i];
} }
index = 0;
while (index < num) { int[][] d = new int[n + 1][2];
int remove = sc.nextInt(); for (int i = 0; i <= n; i++) {
for (int i = remove; i < num; i++) { d[i] = new int[]{-1, -1};
sums[i] -= sums[remove - 1]; }
int[] res = new int[n];
int maxW = 0;
String[] q = reader.readLine().split(" ");
for (int i = n - 1; i >= 0; i--) {
int x = Integer.parseInt(q[i]);
res[i] = maxW;
if (i == 0) break;
//更新最大重量
int cur = arr[x];
int left = x, right = x;
//每次只会将左右两块区域连成一块,我们只需关心一段区间的左边界和右边界,就能通过前缀和数组查询到区间和
if (x + 1 <= n && d[x + 1][0] != -1) {
cur += prev[d[x + 1][1]] - prev[d[x + 1][0] - 1];
right = d[x + 1][1]; //更新右边界
} }
sums[remove - 1] = 0; if (x - 1 > 0 && d[x - 1][1] != -1) {
int max = Integer.MAX_VALUE; cur += prev[d[x - 1][1]] - prev[d[x - 1][0] - 1];
for (int i = 0; i < num; i++) { left = d[x - 1][0]; //更新左边界
max = Math.max(max,sums[i]);
} }
pw.println(max);
index++;
}
}
maxW = Math.max(maxW, cur);
static class Scanner { //更新两端点的左右区间
BufferedReader br; d[left][0] = left;
StringTokenizer st; d[left][1] = right;
d[right][0] = left;
public Scanner(InputStream s) { d[right][1] = right;
br = new BufferedReader(new InputStreamReader(s));
} }
public Scanner(FileReader f) { for (int i = 0; i < n; i++) {
br = new BufferedReader(f); writer.write(res[i] + "\n");
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextIntArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(next());
}
return arr;
} }
writer.close();
reader.close();
} }
} }