Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Round F
// Flattening
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int N = in.nextInt();
int K = in.nextInt();
int[] val = new int[N];
int err = 0, cnt = 0, fix = 0;
for (int n = 0; n < N; n++) {
val[n] = in.nextInt();
}
for (int n = 0; n < N-1; n++) {
int prev = n == 0 ? 0 : val[n-1];
int curr = val[n];
int next = val[n+1];
if (curr != next)
err++;
if (prev != curr && curr != next && prev == next)
cnt++;
}
err -= K;
while (err > 0 && cnt > 0) {
cnt--;
fix++;
err-=2;
}
System.out.printf("Case #%d: %d\n", t, Math.max(0, err) + fix);
}
}
}
// Teach Me
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int N = in.nextInt();
int S = in.nextInt();
List<Set> list = new ArrayList<>();
int cnt = 0;
for (int n = 1; n <= N; n++) {
int C = in.nextInt();
Set<Integer> set = new HashSet<>();
for (int c = 1; c <= C; c++) set.add(in.nextInt());
list.add(set);
}
for (int i = 0; i < N; i++) {
for (int j = i + 1; j < N; j++) {
Set<Integer> set1 = list.get(i);
Set<Integer> set2 = list.get(j);
int common = (int) set1.stream().filter(set2::contains).count();
if (set1.size() > common) cnt++;
if (set2.size() > common) cnt++;
}
}
System.out.printf("Case #%d: %d\n", t, cnt);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.