Skip to content

Commit 31e6ea1

Browse files
committed
update
1 parent 3ef2824 commit 31e6ea1

7 files changed

+252
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// AC: 389 ms
2+
// Memory: 200 KB
3+
// Check 0XX0, 2XX5, 7XX5, 5XX0, four patterns.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1593B_Make_it_Divisible_by_25 {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
for (int i = 0; i < t; i++) {
13+
long n = sc.nextLong();
14+
String nStr = String.valueOf(n);
15+
int len = nStr.length(), ret = Integer.MAX_VALUE, firstZeroPos = -1, secondZeroPos = -1, firstFivePos = -1,
16+
firstFiveAfterZeroPos = -1, firstTwoAfterFivePos = -1, firstSevenAfterFivePos = -1, sign = 0;
17+
for (int j = len - 1; j >= 0; j--) {
18+
char c = nStr.charAt(j);
19+
if (c == '0') {
20+
if (firstZeroPos == -1) {
21+
firstZeroPos = j;
22+
sign++;
23+
} else if (secondZeroPos == -1) {
24+
secondZeroPos = j;
25+
sign++;
26+
}
27+
} else if (c == '5') {
28+
if (firstFivePos == -1) {
29+
firstFivePos = j;
30+
sign++;
31+
}
32+
if (firstZeroPos != -1 && firstFiveAfterZeroPos == -1) {
33+
firstFiveAfterZeroPos = j;
34+
sign++;
35+
}
36+
} else if (c == '2' && firstFivePos != -1 && firstTwoAfterFivePos == -1) {
37+
firstTwoAfterFivePos = j;
38+
sign++;
39+
} else if (c == '7' && firstFivePos != -1 && firstSevenAfterFivePos == -1) {
40+
firstSevenAfterFivePos = j;
41+
sign++;
42+
}
43+
44+
// no need to check other digits.
45+
if (sign == 6) {
46+
break;
47+
}
48+
}
49+
if (secondZeroPos != -1) {
50+
ret = Math.min(ret, len - secondZeroPos - 2);
51+
}
52+
if (firstTwoAfterFivePos != -1) {
53+
ret = Math.min(ret, len - firstTwoAfterFivePos - 2);
54+
}
55+
if (firstSevenAfterFivePos != -1) {
56+
ret = Math.min(ret, len - firstSevenAfterFivePos - 2);
57+
}
58+
if (firstZeroPos != -1 && firstFiveAfterZeroPos != -1) {
59+
ret = Math.min(ret, len - firstFiveAfterZeroPos - 2);
60+
}
61+
62+
System.out.println(ret);
63+
}
64+
}
65+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// AC: 405 ms
2+
// Memory: 400 KB
3+
// Hashmap. Check odd count char occurence, must be >= oddCount - 1.
4+
// T:O(sum(ni)), S:O(1)
5+
//
6+
import java.util.HashMap;
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1883B_Chemistry {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int t = sc.nextInt();
13+
for (int i = 0; i < t; i++) {
14+
int n = sc.nextInt(), k = sc.nextInt();
15+
String s = sc.next();
16+
HashMap<Character, Integer> record = new HashMap<>();
17+
for (char c : s.toCharArray()) {
18+
record.merge(c, 1, Integer::sum);
19+
}
20+
int countOdd = 0;
21+
for (char c : record.keySet()) {
22+
if (record.get(c) % 2 == 1) {
23+
countOdd++;
24+
}
25+
}
26+
27+
System.out.println(k < countOdd - 1 ? "NO" : "YES");
28+
}
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// AC: 420 ms
2+
// Memory: 800 KB
3+
// .
4+
// T:O(sum(ni)), S:O(1) ~ O(max(ni))
5+
//
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import java.util.Scanner;
9+
10+
public class Codeforces_1920A_Satisfying_Constraints {
11+
public static void main(String[] args) {
12+
Scanner sc = new Scanner(System.in);
13+
int t = sc.nextInt();
14+
for (int i = 0; i < t; i++) {
15+
int n = sc.nextInt(), low = 0, high = Integer.MAX_VALUE;
16+
List<Integer> notEqual = new LinkedList<>();
17+
for (int j = 0; j < n; j++) {
18+
int a = sc.nextInt(), x = sc.nextInt();
19+
if (a == 1) {
20+
low = Math.max(low, x);
21+
} else if (a == 2) {
22+
high = Math.min(high, x);
23+
} else if (a == 3) {
24+
notEqual.add(x);
25+
}
26+
}
27+
int ret = 0;
28+
if (high >= low) {
29+
ret = high - low + 1;
30+
for (int item : notEqual) {
31+
if (item >= low && item <= high) {
32+
ret--;
33+
}
34+
}
35+
}
36+
37+
System.out.println(ret);
38+
}
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// AC: 311 ms
2+
// Memory: 400 KB
3+
// Unless for every char position in strings, all char c1 == c3 || c2 == c3, we can always find such template which can meets the requirement.
4+
// So just check this situation: every index i, c1 == c3 || c2 == c3. If true, then the template not exists.
5+
// T:O(sum(ni)), S:O(max(ni))
6+
//
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1922A_Tricky_Template {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int t = sc.nextInt();
13+
for (int i = 0; i < t; i++) {
14+
int n = sc.nextInt();
15+
String a = sc.next(), b = sc.next(), c = sc.next();
16+
boolean flag = true;
17+
for (int j = 0; j < n; j++) {
18+
char c1 = a.charAt(j), c2 = b.charAt(j), c3 = c.charAt(j);
19+
if (c1 != c3 && c2 != c3) {
20+
flag = false;
21+
break;
22+
}
23+
}
24+
System.out.println(flag ? "NO" : "YES");
25+
}
26+
}
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// AC: 560 ms
2+
// Memory: 600 KB
3+
// String & Hashmap.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.HashMap;
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1927B_Following_the_String {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int t = sc.nextInt();
13+
for (int i = 0; i < t; i++) {
14+
int n = sc.nextInt();
15+
StringBuilder ret = new StringBuilder();
16+
HashMap<Character, Integer> record = new HashMap<>();
17+
for (int j = 0; j < n; j++) {
18+
int a = sc.nextInt();
19+
char c = ' ';
20+
for (int k = 0; k < 26; k++) {
21+
char temp = (char) ('a' + k);
22+
if (record.getOrDefault(temp, 0) == a) {
23+
c = temp;
24+
break;
25+
}
26+
}
27+
ret.append(c);
28+
record.merge(c, 1, Integer::sum);
29+
}
30+
31+
System.out.println(ret);
32+
}
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Runtime 0 ms Beats 100.00% of users with Java
2+
// Memory 42.42 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int minOperations(int[] nums, int k) {
8+
int ret = 0;
9+
for (int num : nums) {
10+
if (num < k) {
11+
ret++;
12+
}
13+
}
14+
15+
return ret;
16+
}
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Runtime 2 ms Beats 80.00% of users with Java
2+
// Memory 44.57 MB Beats 60.00% of users with Java
3+
// .
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public int[] resultArray(int[] nums) {
8+
int len = nums.length, lastInArr1 = 0, lastInArr2 = 0;
9+
int[] ret = new int[len];
10+
List<Integer> arr1 = new LinkedList<>(), arr2 = new LinkedList<>();
11+
for (int i = 0; i < len; i++) {
12+
if (i == 0) {
13+
arr1.add(nums[i]);
14+
lastInArr1 = nums[i];
15+
} else if (i == 1) {
16+
arr2.add(nums[i]);
17+
18+
lastInArr2 = nums[i];
19+
} else {
20+
if (lastInArr1 > lastInArr2) {
21+
arr1.add(nums[i]);
22+
lastInArr1 = nums[i];
23+
} else {
24+
arr2.add(nums[i]);
25+
lastInArr2 = nums[i];
26+
}
27+
}
28+
}
29+
int pos = 0;
30+
for (int i : arr1) {
31+
ret[pos++] = i;
32+
}
33+
for (int i : arr2) {
34+
ret[pos++] = i;
35+
}
36+
37+
return ret;
38+
}
39+
}

0 commit comments

Comments
 (0)