File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.Stack ;
4+ import java.util.StringTokenizer ;
5+
6+ public class BJ_24241_Elder_price_robot {
7+
8+ private static final BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
9+ private static final BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
10+ private static final StringBuilder sb = new StringBuilder ();
11+ private static StringTokenizer st;
12+
13+ private static int N ;
14+ private static int [] nums, ans;
15+ private static Stack<Integer > s;
16+
17+ public static void main (String [] args ) throws IOException {
18+ init();
19+ sol();
20+ }
21+
22+ private static void init () throws IOException {
23+ N = Integer . parseInt(br. readLine());
24+
25+ nums = new int [N ];
26+ ans = new int [N ];
27+ st = new StringTokenizer (br. readLine());
28+ for (int i = 0 ; i < N ; i++ ) {
29+ nums[i] = Integer . parseInt(st. nextToken());
30+ }
31+ s = new Stack<> ();
32+ }
33+
34+ private static void sol () throws IOException {
35+ for (int i = N - 1 ; i >= 0 ; i-- ) {
36+ while (! s. isEmpty() && nums[s. peek()] > nums[i]) {
37+ s. pop();
38+ }
39+
40+ if (s. isEmpty()) {
41+ ans[i] = - 1 ; // Inf
42+ } else {
43+ ans[i] = s. peek() - i;
44+ }
45+ s. push(i);
46+ }
47+
48+ for (int i = 0 ; i < N ; i++ ) {
49+ sb. append(ans[i] != - 1 ? ans[i] + " \n " : " infinity\n " );
50+ }
51+ bw. write(sb. toString());
52+ bw. flush();
53+ bw. close();
54+ br. close();
55+ }
56+
57+ }
58+ ```
You can’t perform that action at this time.
0 commit comments