Codeforces Round 905 (Div. 3)
Codeforces Round 905 (Div. 3) A. Morning
import java.io.*;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
char[] s = bf.readLine().toCharArray();
for (int i = 0; i < 4; i++) {
if (s[i] == '0') {
s[i] = '0' + 10;
}
}
int res = 4 + s[0] - '1';
for (int i = 1; i < 4; i++) {
res += Math.abs(s[i] - s[i - 1]);
}
bw.write(res + "\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) B. Chemistry
import java.io.*;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
String[] str = bf.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
char[] s = bf.readLine().toCharArray();
int[] st = new int[26];
for (char c : s) {
st[c - 'a']++;
}
int odd = 0;
for (int i = 0; i < 26; i++) {
if (st[i] % 2 == 1) {
odd++;
}
}
if (odd - 1 > k) bw.write("NO\n");
else bw.write("YES\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) C. Raspberries
import java.io.*;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
String[] str = bf.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int k = Integer.parseInt(str[1]);
int[] a = new int[n + 1];
str = bf.readLine().split(" ");
if (k != 4) {
int ans = 1000000000;
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(str[i - 1]);
if (a[i] % k == 0) ans = 0;
else ans = Math.min(ans, k - a[i] % k);
}
bw.write(ans + "\n");
} else {
int t = 0, ans = 1000000000;
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(str[i - 1]);
if (a[i] % 2 == 0) t++;
if (a[i] % 4 == 0) ans = 0;
if (a[i] % 4 == 3) ans = Math.min(ans, 1);
}
bw.write(Math.min(ans, Math.max(0, 2 - t)) + "\n");
}
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) D. In Love
import java.io.*;
import java.util.TreeMap;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static TreeMap<Integer, Integer> tl = new TreeMap<>(), tr = new TreeMap<>();
public static void solve() throws IOException {
String[] str = bf.readLine().split(" ");
char ch = str[0].charAt(0);
int l = Integer.parseInt(str[1]);
int r = Integer.parseInt(str[2]);
if (ch == '+') {
tl.put(l, tl.getOrDefault(l, 0) + 1);
tr.put(r, tr.getOrDefault(r, 0) + 1);
} else {
tl.put(l, tl.getOrDefault(l, 0) - 1);
tr.put(r, tr.getOrDefault(r, 0) - 1);
if (tl.get(l) == 0) tl.remove(l);
if (tr.get(r) == 0) tr.remove(r);
}
bw.write(tl.size() > 0 && tr.size() > 0 && tl.lastKey() > tr.firstKey() ? "YES\n" : "NO\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) E. Look Back
import java.io.*;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
int n = Integer.parseInt(bf.readLine());
int[] a = new int[n + 1];
String[] str = bf.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(str[i - 1]);
}
long res = 0;
int e = 0;
for (int i = 2; i <= n; i++) {
int x = a[i];
while (e != 0 && x >= 2 * a[i - 1]) {
x /= 2;
e--;
}
while (x < a[i - 1]) {
x *= 2;
e++;
}
res += e;
}
bw.write(res + "\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) F. You Are So Beautiful
import java.io.*;
import java.util.TreeMap;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
int n = Integer.parseInt(bf.readLine());
int[] a = new int[n + 1];
TreeMap<Integer, Integer> l = new TreeMap<>();
TreeMap<Integer, Integer> r = new TreeMap<>();
String[] str = bf.readLine().split(" ");
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(str[i - 1]);
r.put(a[i], r.getOrDefault(a[i], 0) + 1);
}
long res = 0;
for (int i = 1; i <= n; i++) {
l.put(a[i], l.getOrDefault(a[i], 0) + 1);
if (l.get(a[i]) == 1) {
res += r.size();
}
r.put(a[i], r.get(a[i]) - 1);
if (r.get(a[i]) == 0) r.remove(a[i]);
}
bw.write(res + "\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) G1. Dances (Easy version)
import java.io.*;
import java.util.Arrays;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
String[] str = bf.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
int[] a = new int[n - 1];
int[] b = new int[n];
str = bf.readLine().split(" ");
for (int i = 0; i < n - 1; i++) {
a[i] = Integer.parseInt(str[i]);
}
str = bf.readLine().split(" ");
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(str[i]);
}
Arrays.sort(a, 0, n - 1);
Arrays.sort(b, 0, n);
int num = 0, j = 0;
long ans = n - 1;
for (int i = 0; i < n - 1 && j < n; i++, j++) {
while (j < n && a[i] >= b[j]) {
num = b[j];
j++;
}
if (j < n) ans--;
}
if (j < n) num = b[j];
bw.write((ans * m + Math.max(0, m - num + 1)) + "\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}
Codeforces Round 905 (Div. 3) G2. Dances (Hard Version)
import java.io.*;
import java.util.Arrays;
public class Main {
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void solve() throws IOException {
String[] str = bf.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
int[] a = new int[n - 1];
int[] b = new int[n];
str = bf.readLine().split(" ");
for (int i = 0; i < n - 1; i++) {
a[i] = Integer.parseInt(str[i]);
}
str = bf.readLine().split(" ");
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(str[i]);
}
Arrays.sort(a, 0, n - 1);
Arrays.sort(b, 0, n);
int num = 0, j = 0;
long ans = n - 1;
for (int i = 0; i < n - 1 && j < n; i++, j++) {
while (j < n && a[i] >= b[j]) {
num = b[j];
j++;
}
if (j < n) ans--;
}
if (j < n) num = b[j];
bw.write((ans * m + Math.max(0, m - num + 1)) + "\n");
}
public static void main(String[] args) throws IOException {
int T = Integer.parseInt(bf.readLine());
while (T-- > 0) {
solve();
}
bw.close();
}
}