Skip to content

Commit 9ba73cf

Browse files
committed
abc285 a~f abc287 a~c abc188 a~c
1 parent 423f44e commit 9ba73cf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+1118
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package c285;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc285_a {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
int a = scanner.nextInt();
10+
int b = scanner.nextInt();
11+
if (a / 2 == b || b / 2 == a) {
12+
System.out.println("Yes");
13+
return;
14+
}
15+
System.out.println("No");
16+
}
17+
}
18+
/*
19+
A - Edge Checker 2
20+
https://atcoder.jp/contests/abc285/tasks/abc285_a
21+
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package c285;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc285_b {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
int N = scanner.nextInt();
10+
String S = " " + scanner.next();
11+
12+
// S1 ~ S6
13+
char[] chars = S.toCharArray();
14+
for (int i = 1; i < N; i++) {
15+
int l = 0;
16+
for (; l + i <= N; l++) {
17+
if (chars[l] == chars[l + i]) {
18+
break;
19+
}
20+
}
21+
System.out.println(l - 1);
22+
}
23+
}
24+
}
25+
/*
26+
B - Longest Uncommon Prefix
27+
https://atcoder.jp/contests/abc285/tasks/abc285_b
28+
*/
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package c285;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
6+
public class Abc285_c {
7+
public static void main(String[] args) {
8+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
9+
String S = scanner.next();
10+
int len = S.length();
11+
long res = 0;
12+
long a = 1;
13+
for (int i = len - 1; i >= 0; i--) {
14+
res += (S.charAt(i) - 'A' + 1) * a;
15+
a *= 26;
16+
}
17+
System.out.println(res);
18+
}
19+
}
20+
/*
21+
C - abc285_brutmhyhiizp
22+
https://atcoder.jp/contests/abc285/tasks/abc285_c
23+
*/
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package c285;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.ArrayList;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Queue;
11+
import java.util.Scanner;
12+
import java.util.Set;
13+
14+
public class Abc285_d {
15+
public static void main(String[] args) {
16+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
17+
int N = scanner.nextInt();
18+
19+
Set<String> seen = new HashSet<>();
20+
Map<String, Integer> inDegrees = new HashMap<>();
21+
Map<String, List<String>> adj = new HashMap<>();
22+
for (int i = 0; i < N; i++) {
23+
String S = scanner.next();
24+
String T = scanner.next();
25+
adj.computeIfAbsent(S, key -> new ArrayList<>()).add(T);
26+
// 入度
27+
inDegrees.put(T, inDegrees.getOrDefault(T, 0) + 1);
28+
// 出现过的节点
29+
seen.add(S);
30+
seen.add(T);
31+
}
32+
33+
Queue<String> queue = new LinkedList<>();
34+
for (String s : seen) {
35+
if (!inDegrees.containsKey(s)) {
36+
queue.add(s);
37+
}
38+
}
39+
List<String> topo = new ArrayList<>();
40+
while (!queue.isEmpty()) {
41+
int size = queue.size();
42+
for (int i = 0; i < size; i++) {
43+
String u = queue.remove();
44+
topo.add(u);
45+
46+
for (String v : adj.getOrDefault(u, new ArrayList<>())) {
47+
inDegrees.put(v, inDegrees.get(v) - 1);
48+
if (inDegrees.get(v) == 0) {
49+
queue.add(v);
50+
}
51+
}
52+
}
53+
}
54+
if (topo.size() == seen.size()) {
55+
System.out.println("Yes");
56+
return;
57+
}
58+
System.out.println("No");
59+
}
60+
}
61+
/*
62+
D - Change Usernames
63+
https://atcoder.jp/contests/abc285/tasks/abc285_d
64+
65+
拓扑排序
66+
*/
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package c285;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Arrays;
5+
import java.util.Scanner;
6+
7+
public class Abc285_e {
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in, StandardCharsets.UTF_8);
10+
int n = scanner.nextInt();
11+
long[] a = new long[n + 1];
12+
for (int i = 1; i <= n; i++) {
13+
a[i] = scanner.nextInt();
14+
}
15+
16+
long[] b = new long[n + 1];
17+
for (int i = 1; i <= n; i++) {
18+
b[i] = b[i - 1] + a[(i + 1) / 2];
19+
}
20+
// f[i][j] 表示有 i 个任务固定,到目前为止连续 j 个工作日 的 最大生产力
21+
long[][] f = new long[n + 1][n + 1];
22+
for (int i = 0; i < n + 1; i++) {
23+
Arrays.fill(f[i], Long.MIN_VALUE);
24+
}
25+
f[1][0] = 0L;
26+
for (int i = 1; i < n; i++) {
27+
for (int j = 0; j <= n; j++) {
28+
if (f[i][j] < 0) {
29+
continue;
30+
}
31+
f[i + 1][j + 1] = Math.max(f[i + 1][j + 1], f[i][j]);
32+
f[i + 1][0] = Math.max(f[i + 1][0], f[i][j] + b[j]);
33+
}
34+
}
35+
36+
long res = 0;
37+
for (int i = 0; i < n; i++) {
38+
res = Math.max(res, f[n][i] + b[i]);
39+
}
40+
System.out.println(res);
41+
}
42+
}
43+
/*
44+
E - Work or Rest
45+
https://atcoder.jp/contests/abc285/tasks/abc285_e
46+
47+
在高桥生活的世界里,一周有N天。
48+
AtCoder王国的国王高桥给一周中的每一天都指定了“工作日”或“假日”。所有周的作业都应该是一样的。每周至少有一天应被指定为假日。在这种情况下,每周第i天的生产力由长度为N的序列a定义如下:
49+
如果每周第i天是“holiday”,其生产力为0;如果一周的第i天是“weekday”,则其生产率为A min(x,y),
50+
如果最后一个假期是前x天,下一个假期是后y天。
51+
请注意,由于定期分配,上一个/下一个假期可能属于不同的一周。具体请参见样例。
52+
当任务选择得最优时,找出每周最大的生产力。在这里,每周的生产率是指每周第1天、第2天、……和第n天的生产率之和。
53+
54+
动态规划
55+
*/
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package c285;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.StringTokenizer;
9+
10+
public class Abc285_f {
11+
private static List<Fenwick> seg;
12+
13+
public static void main(String[] args) {
14+
FastReader scanner = new FastReader();
15+
int N = scanner.nextInt();
16+
String S = scanner.next();
17+
char[] s = S.toCharArray();
18+
19+
// 26 棵线段树
20+
seg = new ArrayList<>();
21+
for (int i = 0; i < 26; i++) {
22+
seg.add(new Fenwick(N));
23+
}
24+
int[] cnt = new int[26];
25+
26+
for (int i = 0; i < N; i++) {
27+
seg.get(s[i] - 'a').add(i + 1, 1);
28+
cnt[s[i] - 'a']++;
29+
}
30+
31+
int Q = scanner.nextInt();
32+
while (Q-- > 0) {
33+
int op = scanner.nextInt();
34+
if (op == 1) {
35+
int x = scanner.nextInt() - 1;
36+
char c = scanner.next().charAt(0);
37+
38+
seg.get(s[x] - 'a').add(x + 1, -1);
39+
cnt[s[x] - 'a']--;
40+
s[x] = c;
41+
seg.get(s[x] - 'a').add(x + 1, 1);
42+
cnt[s[x] - 'a']++;
43+
} else {
44+
int l = scanner.nextInt();
45+
int r = scanner.nextInt();
46+
47+
int[] ret = new int[26];
48+
for (int i = 0; i < 26; i++) {
49+
ret[i] = seg.get(i).getSum(l, r);
50+
}
51+
int m = 0, M = 26;
52+
while (ret[m] == 0) m++;
53+
while (ret[M - 1] == 0) M--;
54+
boolean flag = true;
55+
for (int i = m + 1; i < M - 1; i++) {
56+
flag &= (ret[i] == cnt[i]);
57+
}
58+
flag &= check(ret, l, r);
59+
System.out.println(flag ? "Yes" : "No");
60+
}
61+
}
62+
}
63+
64+
private static boolean check(int[] ret, int l, int r) {
65+
for (int i = 0; i < 26; i++) {
66+
if (seg.get(i).getSum(l, l + ret[i] - 1) != ret[i]) {
67+
return false;
68+
}
69+
l += ret[i];
70+
}
71+
return true;
72+
}
73+
74+
private static class Fenwick {
75+
private final int N;
76+
private final int[] tree;
77+
78+
public Fenwick(int n) {
79+
this.N = n;
80+
this.tree = new int[N + 1];
81+
}
82+
83+
public int lowbit(int x) {
84+
return x & (-x);
85+
}
86+
87+
// nums[x] add k
88+
public void add(int x, int k) {
89+
while (x <= N) {
90+
tree[x] += k;
91+
x += lowbit(x);
92+
}
93+
}
94+
95+
// nums [1,x] 的和
96+
public int getSum(int x) {
97+
int ans = 0;
98+
while (x >= 1) {
99+
ans += tree[x];
100+
x -= lowbit(x);
101+
}
102+
return ans;
103+
}
104+
105+
// nums [l,r] 的和
106+
public int getSum(int l, int r) {
107+
return getSum(r) - getSum(l - 1);
108+
}
109+
}
110+
111+
private static class FastReader {
112+
private final BufferedReader bufferedReader;
113+
private StringTokenizer stringTokenizer;
114+
115+
public FastReader() {
116+
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
117+
}
118+
119+
public String next() {
120+
while (stringTokenizer == null || !stringTokenizer.hasMoreElements()) {
121+
try {
122+
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
123+
} catch (IOException e) {
124+
e.printStackTrace();
125+
}
126+
}
127+
return stringTokenizer.nextToken();
128+
}
129+
130+
public int nextInt() {
131+
return Integer.parseInt(next());
132+
}
133+
134+
public long nextLong() {
135+
return Long.parseLong(next());
136+
}
137+
138+
public double nextDouble() {
139+
return Double.parseDouble(next());
140+
}
141+
142+
public String nextLine() {
143+
String str = "";
144+
try {
145+
if (stringTokenizer.hasMoreTokens()) {
146+
str = stringTokenizer.nextToken("\n");
147+
} else {
148+
str = bufferedReader.readLine();
149+
}
150+
} catch (IOException e) {
151+
e.printStackTrace();
152+
}
153+
return str;
154+
}
155+
}
156+
}
157+
/*
158+
F - Substring of Sorted String
159+
https://atcoder.jp/contests/abc285/tasks/abc285_f
160+
161+
给定一个长度为N的字符串S,由小写英文字母组成,并进行Q次查询。按顺序处理查询。每个查询是以下两种类型之一:
162+
1 x c: 将S的第x个字符替换为字符c。
163+
2 l r: 设T为将S中的字符按升序排序得到的字符串。
164+
如果由S的第l到第r个字符组成的字符串是T的子字符串,则打印Yes;否则,请打印No。
165+
166+
树状数组 + Fast I/O
167+
线段树会 TLE。。
168+
不用 Fast I/O 也会 TLE
169+
*/

0 commit comments

Comments
 (0)