QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#571556 | #9313. Make Max | rainbows | TL | 36ms | 48828kb | Java11 | 3.0kb | 2024-09-18 00:30:51 | 2024-09-18 00:30:52 |
Judging History
answer
import java.io.*;
import java.util.*;
public class Main {
static Read scanner = new Read();
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException {
int t = scanner.nextInt();
while (t > 0) {
int n = scanner.nextInt();
int[] arr = new int[n];
HashMap<Integer, Integer> first = new HashMap<>();
HashMap<Integer, Integer> last = new HashMap<>();//记录元素最后一次出现的位置
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
last.put(arr[i], i);
}
for (int i = n - 1; i >= 0; i--) {
first.put(arr[i], i);
}
Stack<Integer> stack = new Stack<>();
int[] left = new int[n];
int[] right = new int[n];
//左边第一个比他大,需要下标
for (int i = 0; i < n; i++) {
if (stack.isEmpty()) {
stack.push(i);
} else {
while (!stack.isEmpty() && arr[i] < arr[stack.peek()]) {
left[i] = stack.pop();
}
stack.push(i);
}
}
while (!stack.isEmpty()) {
left[stack.pop()] = -1;
}
for (int i = 0; i < n; i++) {
if (stack.isEmpty()) {
stack.push(i);
} else {
while (!stack.isEmpty() && arr[i] > arr[stack.peek()]) {
right[stack.pop()] = i;
}
stack.push(i);
}
}
while (!stack.isEmpty()) {
right[stack.pop()] = n;
}
int ans = 0;
for (Map.Entry<Integer, Integer> entry : first.entrySet()) {
int val = entry.getKey();
int index = entry.getValue();
ans += index - left[index] - 1;
}
for (Map.Entry<Integer, Integer> entry : last.entrySet()) {
int val = entry.getKey();
int index = entry.getValue();
ans += right[index] - index - 1;
}
out.println(ans);
t--;
}
out.close();
}
}
class Read {
StringTokenizer st = new StringTokenizer("");
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
String nextLine() throws IOException {
return bf.readLine();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 36ms
memory: 48828kb
input:
4 2 1 2 2 2 2 7 1 1 1 2 2 2 2 3 1 2 3
output:
1 0 3 3
result:
ok 4 number(s): "1 0 3 3"
Test #2:
score: -100
Time Limit Exceeded
input:
2 198018 875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...
output:
1238085722 513645830