Skip to content

Commit b63b2c4

Browse files
committed
fix(codeforces/2108F): Correct file paths
1 parent 6b407f3 commit b63b2c4

File tree

2 files changed

+105
-88
lines changed

2 files changed

+105
-88
lines changed

codeforces/2108F/src/Main.java

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import java.io.*;
2+
import java.util.Arrays;
23
import java.util.HashSet;
34
import java.util.Set;
45
import java.util.StringTokenizer;
@@ -11,6 +12,7 @@ public class Main {
1112
long[] a;
1213
int n;
1314
long maxMEX;
15+
int[] maxP;
1416

1517
Main() {
1618
in = new BufferedReader(new InputStreamReader(System.in));
@@ -25,12 +27,22 @@ void solve() throws IOException {
2527
StringTokenizer st = new StringTokenizer(in.readLine());
2628
for (int i = 0; i < n; i++) {
2729
a[i] = Integer.parseInt(st.nextToken());
30+
if (a[i] > n) {
31+
a[i] = n;
32+
}
2833
}
2934
int[] p = new int[n];
3035
boolean[] used = new boolean[n];
3136
maxMEX = 0;
37+
for (int i = 0; i < n; i++) {
38+
p[i] = n - 1 - i;
39+
}
40+
maxP = new int[n];
41+
42+
//maxMEX = getMex(p);
3243
permutation(p, used, 0);
3344
out.println(maxMEX);
45+
// printArray(maxP);
3446
}
3547
}
3648

@@ -41,43 +53,14 @@ void printArray(int[] p) {
4153
out.println();
4254
}
4355

56+
4457
void permutation(int[] p, boolean[] used, int cur) {
4558
if (cur == n) {
46-
long[] b = a.clone();
47-
for (int i = 0; i < n; i++) {
48-
int pos = p[i];
49-
long bpi = b[pos];
50-
for (int j = 0; j < bpi; j++) {
51-
if (pos + j + 1 < n) {
52-
b[pos + j + 1] += 1;
53-
} else {
54-
break;
55-
}
56-
}
57-
b[pos] = 0;
58-
}
59-
boolean nonDecreasing = true;
60-
for (int i = 0; i < n - 1; i++) {
61-
if (b[i] > b[i + 1]) {
62-
nonDecreasing = false;
63-
break;
64-
}
65-
}
66-
67-
if (nonDecreasing) {
68-
Set<Long> set = new HashSet<>();
69-
for (int i = 0; i < n; i++) {
70-
set.add(b[i]);
71-
}
72-
long mex;
73-
for (mex = 1; ; mex++) {
74-
if (!set.contains(mex)) {
75-
break;
76-
}
77-
}
78-
if (mex > maxMEX) {
79-
maxMEX = mex;
80-
}
59+
long mex;
60+
mex = getMex(p);
61+
if (mex > maxMEX) {
62+
maxMEX = mex;
63+
maxP = p.clone();
8164
}
8265
} else {
8366
for (int i = 0; i < n; i++) {
@@ -91,6 +74,45 @@ void permutation(int[] p, boolean[] used, int cur) {
9174
}
9275
}
9376

77+
private long getMex(int[] p) {
78+
long mex;
79+
long[] b = a.clone();
80+
for (int i = 0; i < n; i++) {
81+
int pos = p[i];
82+
long bpi = b[pos];
83+
for (int j = 0; j < bpi; j++) {
84+
if (pos + j + 1 < n) {
85+
b[pos + j + 1] += 1;
86+
} else {
87+
break;
88+
}
89+
}
90+
b[pos] = 0;
91+
}
92+
boolean nonDecreasing = true;
93+
for (int i = 0; i < n - 1; i++) {
94+
if (b[i] > b[i + 1]) {
95+
nonDecreasing = false;
96+
break;
97+
}
98+
}
99+
100+
if (nonDecreasing) {
101+
Set<Long> set = new HashSet<>();
102+
for (int i = 0; i < n; i++) {
103+
set.add(b[i]);
104+
}
105+
for (mex = 1; ; mex++) {
106+
if (!set.contains(mex)) {
107+
break;
108+
}
109+
}
110+
} else {
111+
mex = -1;
112+
}
113+
return mex;
114+
}
115+
94116
void close() throws IOException {
95117
if (in != null) {
96118
in.close();

codeforces/2108F/src/p1/Main.java

Lines changed: 48 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,70 @@
44
import java.util.*;
55

66
public class Main {
7-
BufferedReader in;
8-
PrintWriter out;
7+
private BufferedReader in;
8+
private PrintWriter out;
9+
private int[] a;
10+
private int n;
911

10-
Main() {
12+
public Main() {
1113
in = new BufferedReader(new InputStreamReader(System.in));
1214
out = new PrintWriter(System.out);
1315
}
1416

15-
void solve() throws IOException {
16-
int t = Integer.parseInt(in.readLine().trim());
17-
while (t-- > 0) {
18-
int n = Integer.parseInt(in.readLine().trim());
19-
long[] a = new long[n];
20-
StringTokenizer st = new StringTokenizer(in.readLine());
21-
for (int i = 0; i < n; i++) {
22-
if (!st.hasMoreTokens()) {
23-
throw new IOException("Insufficient input tokens");
24-
}
25-
a[i] = Long.parseLong(st.nextToken());
26-
if (a[i] > n) a[i] = n; // Cap a[i] at n
17+
public boolean check(int x) {
18+
int[] d = new int[n];
19+
for (int i = 0; i < n; i++) {
20+
int need = Math.max(0, x - n + i);
21+
if (i > 0) {
22+
d[i] += d[i - 1];
2723
}
28-
29-
// Handle n = 1 separately
30-
if (n == 1) {
31-
out.println(1); // Final array is [0], MEX = 1
32-
continue;
24+
if (d[i] < need) {
25+
return false;
3326
}
34-
35-
// Difference array for range updates
36-
long[] diff = new long[n + 1];
37-
for (int i = n - 1; i >= 0; i--) {
38-
if (a[i] == 0) continue; // No increments if a[i] = 0
39-
int start = i + 1;
40-
int end = (int) Math.min((long) i + a[i], n - 1);
41-
if (start <= end) {
42-
diff[start]++;
43-
if (end + 1 <= n) diff[end + 1]--;
44-
}
27+
int len = d[i] - need + a[i];
28+
if (i + 1 < n) {
29+
d[i + 1]++;
4530
}
46-
47-
// Compute final array b
48-
long[] b = new long[n];
49-
long sum = 0;
50-
for (int i = 0; i < n; i++) {
51-
sum += diff[i];
52-
b[i] = sum;
31+
if (i + len + 1 < n) {
32+
d[i + len + 1]--;
5333
}
34+
}
35+
return true;
36+
}
5437

55-
// Compute MEX efficiently
56-
long mex = 0;
57-
for (int i = 0; i < n; i++) {
58-
if (b[i] > mex) break; // Gap found
59-
if (b[i] == mex) mex++; // All values up to mex-1 exist
38+
public void solve() throws IOException {
39+
StringTokenizer st = new StringTokenizer(in.readLine());
40+
n = Integer.parseInt(st.nextToken());
41+
42+
a = new int[n];
43+
st = new StringTokenizer(in.readLine());
44+
for (int i = 0; i < n; i++) {
45+
a[i] = Integer.parseInt(st.nextToken());
46+
}
47+
48+
int lo = 0, hi = n;
49+
while (lo < hi) {
50+
int x = (lo + hi + 1) / 2;
51+
if (check(x)) {
52+
lo = x;
53+
} else {
54+
hi = x - 1;
6055
}
61-
out.println(mex);
6256
}
57+
out.println(lo);
6358
}
6459

65-
void close() throws IOException {
66-
if (in != null) in.close();
67-
if (out != null) {
68-
out.flush();
69-
out.close();
60+
public void run() throws IOException {
61+
StringTokenizer st = new StringTokenizer(in.readLine());
62+
int t = Integer.parseInt(st.nextToken());
63+
64+
while (t-- > 0) {
65+
solve();
7066
}
67+
out.flush();
7168
}
7269

73-
public static void main(String[] args) throws Exception {
74-
Main main = new Main();
75-
main.solve();
76-
main.close();
70+
public static void main(String[] args) throws IOException {
71+
new Main().run();
7772
}
7873
}

0 commit comments

Comments
 (0)