Skip to content

Commit f108048

Browse files
committed
update
1 parent 380d18d commit f108048

8 files changed

+193
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// AC: 280 ms
2+
// Memory: 500 KB
3+
// Geometry.
4+
// T:O(1), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_0507B_Amr_and_Pins {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
long r = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt(), x1 = sc.nextInt(), y1 = sc.nextInt(),
12+
xDelta = Math.abs(x - x1), yDelta = Math.abs(y - y1);
13+
double dis = Math.sqrt(xDelta * xDelta + yDelta * yDelta);
14+
int ret = (int) (dis / (2 * r));
15+
if (dis > 2 * r * ret) {
16+
ret++;
17+
}
18+
System.out.println(ret);
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// AC: 296 ms
2+
// Memory: 600 KB
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1213A_Chips_Moving {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int n = sc.nextInt(), oddCount = 0;
12+
for (int i = 0; i < n; i++) {
13+
int x = sc.nextInt();
14+
if (x % 2 == 1) {
15+
oddCount++;
16+
}
17+
}
18+
System.out.println(Math.min(oddCount, n - oddCount));
19+
}
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// AC: 608 ms
2+
// Memory: 500 KB
3+
// do as it say.
4+
// T:O(n), S:O(n)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1352D_Alice_Bob_and_Candies {
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+
int n = sc.nextInt(), cur = 0, round = 0, sum1 = 0, sum2 = 0;
14+
int[] arr = new int[n];
15+
for (int j = 0; j < n; j++) {
16+
arr[j] = sc.nextInt();
17+
}
18+
for (int left = 0, right = n - 1; left <= right; round++) {
19+
int sum = 0;
20+
if (round % 2 == 0) {
21+
sum = arr[left++];
22+
while (sum <= cur) {
23+
if (left > right) {
24+
break;
25+
}
26+
sum += arr[left++];
27+
}
28+
cur = Math.max(cur, sum);
29+
sum1 += sum;
30+
} else {
31+
sum = arr[right--];
32+
while (sum <= cur) {
33+
if (left > right) {
34+
break;
35+
}
36+
sum += arr[right--];
37+
}
38+
cur = Math.max(cur, sum);
39+
sum2 += sum;
40+
}
41+
}
42+
System.out.println(round + " " + sum1 + " " + sum2);
43+
}
44+
}
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// AC: 342 ms
2+
// Memory: 500 KB
3+
// It can be proved that when n is odd, the sum is always a square number, so the maximum side length is Math.ceil(n / 2).
4+
// T:O(t), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1748A_The_Ultimate_Square {
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+
int n = sc.nextInt();
14+
System.out.println(n % 2 == 0 ? n / 2 : (n / 2 + 1));
15+
}
16+
}
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// AC: 436 ms
2+
// Memory: 500 KB
3+
// Can only flip once, so first find adjacent -1 pair, if not, check if has any -1,
4+
// if not, we have to conver "1 1" to "-1 -1", thus the sum will be minus by 4.
5+
// T:O(sum(ni)), S:O(1)
6+
//
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1778A_Flip_Flop_Sum {
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(), countNega = 0, sum = 0, prev = 0, ret = 0;
15+
boolean hasAdjacentNega = false;
16+
for (int j = 0; j < n; j++) {
17+
int a = sc.nextInt();
18+
if (a < 0) {
19+
countNega++;
20+
}
21+
if (prev != 0) {
22+
if (!hasAdjacentNega && a == -1 && prev == -1) {
23+
hasAdjacentNega = true;
24+
}
25+
}
26+
sum += a;
27+
prev = a;
28+
}
29+
if (countNega == 0) {
30+
ret = sum - 4;
31+
} else if (hasAdjacentNega) {
32+
ret = sum + 4;
33+
} else {
34+
ret = sum;
35+
}
36+
37+
System.out.println(ret);
38+
}
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Runtime 5 ms Beats 97.05% of users with Java
2+
// Memory 44.39 MB Beats 62.56% of users with Java
3+
// Math: 逆波兰表达式,算术表达式解析
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int evalRPN(String[] tokens) {
8+
Stack<Integer> record = new Stack();
9+
for (String token : tokens) {
10+
if ("+".equals(token)) {
11+
int a = record.pop(), b = record.pop();
12+
record.add(a + b);
13+
} else if ("-".equals(token)) {
14+
int a = record.pop(), b = record.pop();
15+
record.add(b - a);
16+
} else if ("*".equals(token)) {
17+
int a = record.pop(), b = record.pop();
18+
record.add(a * b);
19+
} else if ("/".equals(token)) {
20+
int a = record.pop(), b = record.pop();
21+
record.add(b / a);
22+
} else {
23+
record.add(Integer.parseInt(token));
24+
}
25+
}
26+
27+
return record.peek();
28+
}
29+
}

leetcode_solved/leetcode_1291_Sequential_Digits.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// AC: Runtime: 0 ms, faster than 100.00% of Java online submissions for Sequential Digits.
22
// Memory Usage: 36.6 MB, less than 58.00% of Java online submissions for Sequential Digits.
33
// see the annotation
4-
// T:O(9 * log10(high)) ~ O(log(n), S:O(1)
4+
// T:O(9 * log10(high)) ~ O(log(n)), S:O(1)
55
//
66
class Solution {
77
public List<Integer> sequentialDigits(int low, int high) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// AC: Runtime 1 ms Beats 100.00% of users with Java
2+
// Memory 42.13 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int countKeyChanges(String s) {
8+
int ret = 0;
9+
char prev = s.charAt(0);
10+
for (int i = 1; i < s.length(); i++) {
11+
char cur = s.charAt(i);
12+
if (prev == cur || Math.abs(prev - cur) == ('a' - 'A')) {
13+
continue;
14+
}
15+
ret++;
16+
prev = cur;
17+
}
18+
19+
return ret;
20+
}
21+
}

0 commit comments

Comments
 (0)