Skip to content

Commit 6b407f3

Browse files
committed
feat(codeforces/2108F): add p1/Main.java
1 parent ce11309 commit 6b407f3

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

codeforces/2108F/src/Main.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ public class Main {
88
BufferedReader in;
99
PrintWriter out;
1010

11-
int[] a;
11+
long[] a;
1212
int n;
13-
int maxMEX;
13+
long maxMEX;
1414

1515
Main() {
1616
in = new BufferedReader(new InputStreamReader(System.in));
1717
out = new PrintWriter(System.out);
1818
}
1919

2020
void solve() throws IOException {
21-
int t = Integer.parseInt(in.readLine());
21+
long t = Integer.parseInt(in.readLine());
2222
while (t-- > 0) {
2323
n = Integer.parseInt(in.readLine());
24-
a = new int[n];
24+
a = new long[n];
2525
StringTokenizer st = new StringTokenizer(in.readLine());
2626
for (int i = 0; i < n; i++) {
2727
a[i] = Integer.parseInt(st.nextToken());
@@ -43,10 +43,10 @@ void printArray(int[] p) {
4343

4444
void permutation(int[] p, boolean[] used, int cur) {
4545
if (cur == n) {
46-
int[] b = a.clone();
46+
long[] b = a.clone();
4747
for (int i = 0; i < n; i++) {
4848
int pos = p[i];
49-
int bpi = b[pos];
49+
long bpi = b[pos];
5050
for (int j = 0; j < bpi; j++) {
5151
if (pos + j + 1 < n) {
5252
b[pos + j + 1] += 1;
@@ -65,11 +65,11 @@ void permutation(int[] p, boolean[] used, int cur) {
6565
}
6666

6767
if (nonDecreasing) {
68-
Set<Integer> set = new HashSet<>();
68+
Set<Long> set = new HashSet<>();
6969
for (int i = 0; i < n; i++) {
7070
set.add(b[i]);
7171
}
72-
int mex;
72+
long mex;
7373
for (mex = 1; ; mex++) {
7474
if (!set.contains(mex)) {
7575
break;
@@ -102,16 +102,6 @@ void close() throws IOException {
102102
}
103103

104104
public static void main(String[] args) throws Exception {
105-
FileInputStream inStream = null;
106-
PrintStream outStream = null;
107-
boolean isLocal = System.getProperty("os.name").equals("Mac OS X");
108-
if (isLocal) {
109-
inStream = new FileInputStream("1.in");
110-
// outStream = new PrintStream("1.out");
111-
System.setIn(inStream);
112-
// System.setOut(outStream);
113-
}
114-
115105
Main main = new Main();
116106
main.solve();
117107
main.close();

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package p1;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
BufferedReader in;
8+
PrintWriter out;
9+
10+
Main() {
11+
in = new BufferedReader(new InputStreamReader(System.in));
12+
out = new PrintWriter(System.out);
13+
}
14+
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
27+
}
28+
29+
// Handle n = 1 separately
30+
if (n == 1) {
31+
out.println(1); // Final array is [0], MEX = 1
32+
continue;
33+
}
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+
}
45+
}
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;
53+
}
54+
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
60+
}
61+
out.println(mex);
62+
}
63+
}
64+
65+
void close() throws IOException {
66+
if (in != null) in.close();
67+
if (out != null) {
68+
out.flush();
69+
out.close();
70+
}
71+
}
72+
73+
public static void main(String[] args) throws Exception {
74+
Main main = new Main();
75+
main.solve();
76+
main.close();
77+
}
78+
}

0 commit comments

Comments
 (0)