Skip to content

Commit ed0bb6f

Browse files
committed
update
1 parent c189c71 commit ed0bb6f

7 files changed

+186
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// AC: 343 ms
2+
// Memory: 1100 KB
3+
// brute-force.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1822A_TubeTube_Feed {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int q = sc.nextInt();
12+
for (int i = 0; i < q; i++) {
13+
int n = sc.nextInt(), t = sc.nextInt(), ret = -1, retIndex = -1;
14+
int[] arr1 = new int[n], arr2 = new int[n];
15+
for (int j = 0; j < n; j++) {
16+
arr1[j] = sc.nextInt();
17+
}
18+
for (int j = 0; j < n; j++) {
19+
arr2[j] = sc.nextInt();
20+
}
21+
for (int j = 0; j < n; j++) {
22+
if (arr1[j] <= t && arr2[j] > ret) {
23+
ret = Math.max(ret, arr2[j]);
24+
retIndex = j + 1;
25+
}
26+
t--;
27+
}
28+
29+
System.out.println(retIndex);
30+
}
31+
}
32+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// AC: 358 ms
2+
// Memory: 1300 KB
3+
// Discuss by situations.
4+
// T:O(t), S:O(t)
5+
//
6+
import java.util.Arrays;
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1881B_Three_Threadlets {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int t = sc.nextInt(), maxOccurTime = 0;
13+
for (int i = 0; i < t; i++) {
14+
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
15+
int[] arr = new int[]{a, b, c};
16+
Arrays.sort(arr);
17+
boolean ret = false;
18+
if ((long) arr[1] * 4 < arr[2]) {
19+
ret = false;
20+
} else {
21+
if (arr[0] == arr[2]) {
22+
ret = true;
23+
} else if (arr[0] == arr[1]) {
24+
ret = (arr[2] == (arr[0] * 2)) || (arr[2] == (arr[0] * 3L)) || (arr[2] == (arr[0] * 4L));
25+
} else if (arr[1] == arr[2]) {
26+
ret = arr[1] == (arr[0] * 2);
27+
} else {
28+
ret = (arr[1] == (arr[0] * 2)) && (arr[2] == (arr[0] * 3L));
29+
}
30+
}
31+
32+
System.out.println(ret ? "YES" : "NO");
33+
}
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// AC: 327 ms
2+
// Memory: 800 KB
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1895A_Treasure_Chest {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int n = sc.nextInt();
12+
for (int i = 0; i < n; i++) {
13+
int x = sc.nextInt(), y = sc.nextInt(), k = sc.nextInt(), ret = x;
14+
if (y > x) {
15+
ret = y + (k >= (y - x) ? 0 : (y - x - k));
16+
}
17+
System.out.println(ret);
18+
}
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Runtime 35 ms Beats 82.95%
2+
// Memory 60.36 MB Beats 31.15%
3+
// Greedy & Sort. Or using set both are ok.
4+
// T:O(nlogn), S:O(1)
5+
//
6+
class Solution {
7+
public int minIncrementForUnique(int[] nums) {
8+
int cur = -1, ret = 0;
9+
Arrays.sort(nums);
10+
for (int num : nums) {
11+
if (num > cur) {
12+
cur = num;
13+
} else if (num == cur) {
14+
ret += 1;
15+
cur += 1;
16+
} else {
17+
ret += cur - num + 1;
18+
cur += 1;
19+
}
20+
}
21+
22+
return ret;
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Runtime 2 ms Beats 100.00% of users with Java
2+
// Memory 42.66 MB Beats 100.00% of users with Java
3+
// Using stack
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public String clearDigits(String s) {
8+
Stack<Character> record = new Stack<>();
9+
for (char c : s.toCharArray()) {
10+
if (c >= '0' && c <= '9') {
11+
if (!record.isEmpty()) {
12+
record.pop();
13+
}
14+
} else {
15+
record.push(c);
16+
}
17+
}
18+
StringBuilder ret = new StringBuilder();
19+
while (!record.isEmpty()) {
20+
ret.append(record.pop());
21+
}
22+
23+
return ret.reverse().toString();
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Runtime 0 ms Beats 100.00% of users with Java
2+
// Memory 40.54 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(1), S:O(1)
5+
//
6+
class Solution {
7+
public int numberOfChild(int n, int k) {
8+
int remain = k % (2 * (n - 1));
9+
if (remain <= n - 1) {
10+
return remain;
11+
} else {
12+
return 2 * (n - 1) - remain;
13+
}
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Solution 1: Brute-force 暴力解法
2+
// Runtime 198 ms Beats 100.00% of users with Java
3+
// Memory 40.65 MB Beats 100.00% of users with Java
4+
// Brute-force.
5+
// T:O(n * k), S:O(1)
6+
//
7+
class Solution {
8+
public int valueAfterKSeconds(int n, int k) {
9+
int[] arr = new int[n];
10+
arr[0] = 1;
11+
for (int i = 0; i <= k; i++) {
12+
for (int j = 1; j < n; j++) {
13+
arr[j] = (arr[j] + arr[j - 1]) % (1_000_000_000 + 7);
14+
}
15+
}
16+
17+
return arr[n - 1];
18+
}
19+
}
20+
21+
22+
// Solution 2: 杨辉三角通项公式
23+
// 1
24+
// 1 1
25+
// 1 2 1
26+
// 1 3 3 1
27+
// 1 4 6 4 1
28+
// 1 5 10 10 5 1
29+
// 1 6 15 20 15 6 1
30+
// 1 7 21 35 35 21 7 1
31+
// 1 8 28 56 70 56 28 8 1
32+
//
33+
34+
// 结论:combination(k+n-1,n-1)
35+
// todo-补充理解思路,没搞懂

0 commit comments

Comments
 (0)