A - Buildings
Problem Statement
There are
N
N
N buildings aligned in a row. The
i
i
i-th building from the left has a height of
H
i
H_i
Hi.
Determine if there is a building taller than the first one from the left. If such a building exists, find the position of the leftmost such building from the left.
Constraints
1
≤
N
≤
100
1 \leq N \leq 100
1≤N≤100
1
≤
H
i
≤
100
1 \leq H_i \leq 100
1≤Hi≤100
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
H
1
H_1
H1
H
2
H_2
H2
…
\ldots
…
H
N
H_N
HN
Output
If no building is taller than the first one from the left, print -1
.
If such a building exists, print the position (index) of the leftmost such building from the left.
Sample Input 1
4
3 2 5 2
Sample Output 1
3
The building taller than the first one from the left is the third one from the left.
Sample Input 2
3
4 3 2
Sample Output 2
-1
No building is taller than the first one from the left.
Sample Input 3
7
10 5 10 2 10 13 15
Sample Output 3
6
The buildings taller than the first one from the left are the sixth and seventh ones. Among them, the leftmost is the sixth one.
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, a, x;
cin >> n >> a;
for (int i = 2; i <= n; i ++) {
cin >> x;
if (x > a) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
B - AtCoder Amusement Park
Problem Statement
The AtCoder amusement park has an attraction that can accommodate
K
K
K people. Now, there are
N
N
N groups lined up in the queue for this attraction.
The
i
i
i-th group from the front
(
1
≤
i
≤
N
)
(1\leq i\leq N)
(1≤i≤N) consists of
A
i
A_i
Ai people. For all
i
i
i
(
1
≤
i
≤
N
)
(1\leq i\leq N)
(1≤i≤N), it holds that
A
i
≤
K
A_i \leq K
Ai≤K.
Takahashi, as a staff member of this attraction, will guide the groups in the queue according to the following procedure.
Initially, no one has been guided to the attraction, and there are
K
K
K empty seats.
- If there are no groups in the queue, start the attraction and end the guidance. Compare the number of empty seats in the attraction with the number of people in the group at the front of the queue, and do one of the following: If the number of empty seats is less than the number of people in the group at the front, start the attraction. Then, the number of empty seats becomes $K$ again. Otherwise, guide the entire group at the front of the queue to the attraction. The front group is removed from the queue, and the number of empty seats decreases by the number of people in the group. Go back to step 1.
1
≤
N
≤
100
1\leq N\leq 100
1≤N≤100
1
≤
K
≤
100
1\leq K\leq 100
1≤K≤100
1
≤
A
i
≤
K
(
1
≤
i
≤
N
)
1\leq A_i\leq K\ (1\leq i\leq N)
1≤Ai≤K (1≤i≤N)
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
K
K
K
A
1
A_1
A1
A
2
A_2
A2
…
\ldots
…
A
N
A_N
AN
Output
Print the answer.
Sample Input 1
7 6
2 5 1 4 1 2 3
Sample Output 1
4
Initially, the seven groups are lined up as follows:
Part of Takahashi’s guidance is shown in the following figure:
Initially, the group at the front has
2
2
2 people, and there are
6
6
6 empty seats. Thus, he guides the front group to the attraction, leaving
4
4
4 empty seats.
Next, the group at the front has
5
5
5 people, which is more than the
4
4
4 empty seats, so the attraction is started.
After the attraction is started, there are
6
6
6 empty seats again, so the front group is guided to the attraction, leaving
1
1
1 empty seat.
Next, the group at the front has
1
1
1 person, so they are guided to the attraction, leaving
0
0
0 empty seats.
In total, he starts the attraction four times before the guidance is completed.
Therefore, print 4
.
Sample Input 2
7 10
1 10 1 10 1 10 1
Sample Output 2
7
Sample Input 3
15 100
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60
Sample Output 3
8
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
int res = 0, tot = 0, x;
for (int i = 1; i <= n; i ++) {
cin >> x;
if (tot + x > m) {
res ++, tot = 0;
}
tot += x;
}
if (tot) res ++;
cout << res << endl;
return 0;
}
C - Sigma Problem
Problem Statement
For positive integers
x
x
x and
y
y
y, define
f
(
x
,
y
)
f(x, y)
f(x,y) as the remainder of
(
x
+
y
)
(x + y)
(x+y) divided by
1
0
8
10^8
108.
You are given a sequence of positive integers
A
=
(
A
1
,
…
,
A
N
)
A = (A_1, \ldots, A_N)
A=(A1,…,AN) of length
N
N
N. Find the value of the following expression:
## Constraints
2
≤
N
≤
3
×
1
0
5
2 \leq N \leq 3\times 10^5
2≤N≤3×105
KaTeX parse error: Expected 'EOF', got '&' at position 12: 1 \leq A_i &̲lt; 10^8
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
A
1
A_1
A1
…
\ldots
…
A
N
A_N
AN
Output
Print the answer.
Sample Input 1
3
3 50000001 50000002
Sample Output 1
100000012
f
(
A
1
,
A
2
)
=
50000004
f(A_1,A_2)=50000004
f(A1,A2)=50000004
f
(
A
1
,
A
3
)
=
50000005
f(A_1,A_3)=50000005
f(A1,A3)=50000005
f
(
A
2
,
A
3
)
=
3
f(A_2,A_3)=3
f(A2,A3)=3
Thus, the answer is
f
(
A
1
,
A
2
)
+
f
(
A
1
,
A
3
)
+
f
(
A
2
,
A
3
)
=
100000012
f(A_1,A_2) + f(A_1,A_3) + f(A_2,A_3) = 100000012
f(A1,A2)+f(A1,A3)+f(A2,A3)=100000012.
Note that you are not asked to compute the remainder of the sum divided by
1
0
8
10^8
108.
Sample Input 2
5
1 3 99999999 99999994 1000000
Sample Output 2
303999988
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
std::vector<int> a(n + 1);
int tot = 0, res = 0;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
res += tot + a[i] * (i - 1), tot += a[i];
}
sort(a.begin() + 1, a.end());
int tmp = 1e8;
for (int i = 1; i < n; i ++) {
auto it = lower_bound(a.begin() + i + 1, a.end(), tmp - a[i]);
if (it == a.end()) continue;
int id = it - a.begin();
res -= tmp * (n - id + 1);
}
cout << res << endl;
return 0;
}
D - Another Sigma Problem
Problem Statement
For positive integers
x
x
x and
y
y
y, define
f
(
x
,
y
)
f(x, y)
f(x,y) as follows:
Interpret the decimal representations of
x
x
x and
y
y
y as strings and concatenate them in this order to obtain a string
z
z
z. The value of
f
(
x
,
y
)
f(x, y)
f(x,y) is the value of
z
z
z when interpreted as a decimal integer.
For example,
f
(
3
,
14
)
=
314
f(3, 14) = 314
f(3,14)=314 and
f
(
100
,
1
)
=
1001
f(100, 1) = 1001
f(100,1)=1001.
You are given a sequence of positive integers
A
=
(
A
1
,
…
,
A
N
)
A = (A_1, \ldots, A_N)
A=(A1,…,AN) of length
N
N
N. Find the value of the following expression modulo
998244353
998244353
998244353:
## Constraints
2
≤
N
≤
2
×
1
0
5
2 \leq N \leq 2 \times 10^5
2≤N≤2×105
1
≤
A
i
≤
1
0
9
1 \leq A_i \leq 10^9
1≤Ai≤109
All input values are integers.
Input
The input is given from Standard Input in the following format:
$N$
$A_1$ $\ldots$ $A_N$
Output
Print the answer.
Sample Input 1
3
3 14 15
Sample Output 1
2044
f
(
A
1
,
A
2
)
=
314
f(A_1, A_2) = 314
f(A1,A2)=314
f
(
A
1
,
A
3
)
=
315
f(A_1, A_3) = 315
f(A1,A3)=315
f
(
A
2
,
A
3
)
=
1415
f(A_2, A_3) = 1415
f(A2,A3)=1415
Thus, the answer is
f
(
A
1
,
A
2
)
+
f
(
A
1
,
A
3
)
+
f
(
A
2
,
A
3
)
=
2044
f(A_1, A_2) + f(A_1, A_3) + f(A_2, A_3) = 2044
f(A1,A2)+f(A1,A3)+f(A2,A3)=2044.
Sample Input 2
5
1001 5 1000000 1000000000 100000
Sample Output 2
625549048
Be sure to calculate the value modulo 998244353 998244353 998244353.
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 2e5 + 10, mod = 998244353;
int n;
int a[N], s[N];
int dec(int x) {
int res = 0;
while (x) res ++, x /= 10;
return res;
}
int ksm(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> a[i];
reverse(a + 1, a + 1 + n);
for (int i = 1; i <= n; i ++)
s[i] = (s[i - 1] + ksm(10, dec(a[i]))) % mod;
int res = 0;
for (int i = 1; i <= n; i ++) {
res = (res + s[i - 1] * a[i]) % mod;
res = (res + a[i] * (n - i) % mod) % mod;
}
cout << res << endl;
return 0;
}
E - Yet Another Sigma Problem
Problem Statement
For strings
x
x
x and
y
y
y, define
f
(
x
,
y
)
f(x, y)
f(x,y) as follows:
f
(
x
,
y
)
f(x, y)
f(x,y) is the length of the longest common prefix of
x
x
x and
y
y
y.
You are given
N
N
N strings
(
S
1
,
…
,
S
N
)
(S_1, \ldots, S_N)
(S1,…,SN) consisting of lowercase English letters. Find the value of the following expression:
## Constraints
2
≤
N
≤
3
×
1
0
5
2 \leq N \leq 3\times 10^5
2≤N≤3×105
S
i
S_i
Si is a string consisting of lowercase English letters.
1
≤
∣
S
i
∣
1 \leq |S_i|
1≤∣Si∣
∣
S
1
∣
+
∣
S
2
∣
+
…
+
∣
S
N
∣
≤
3
×
1
0
5
|S_1|+|S_2|+\ldots+|S_N|\leq 3\times 10^5
∣S1∣+∣S2∣+…+∣SN∣≤3×105
All input numbers are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
S
1
S_1
S1
…
\ldots
…
S
N
S_N
SN
Output
Print the answer.
Sample Input 1
3
ab abc arc
Sample Output 1
4
f
(
S
1
,
S
2
)
=
2
f(S_1,S_2)=2
f(S1,S2)=2
f
(
S
1
,
S
3
)
=
1
f(S_1,S_3)=1
f(S1,S3)=1
f
(
S
2
,
S
3
)
=
1
f(S_2,S_3)=1
f(S2,S3)=1
Thus, the answer is
f
(
S
1
,
S
2
)
+
f
(
S
1
,
S
3
)
+
f
(
S
2
,
S
3
)
=
4
f(S_1,S_2) + f(S_1,S_3) + f(S_2,S_3) = 4
f(S1,S2)+f(S1,S3)+f(S2,S3)=4.
Sample Input 2
11
ab bb aaa bba baba babb aaaba aabbb a a b
Sample Output 2
32
Solution
具体见文末视频。
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
const int N = 3e5 + 10;
int n;
string s[N];
int tr[N][26], idx, cnt[N * 26];
int res;
void insert(string s) {
int p = 0;
for (int i = 0; i < s.size(); i ++) {
int u = s[i] - 'a';
if (!tr[p][u]) tr[p][u] = ++ idx;
p = tr[p][u], cnt[p] ++;
}
}
void dfs(int u) {
res += cnt[u] * (cnt[u] - 1) / 2;
for (int i = 0; i < 26; i ++)
if (tr[u][i]) {
dfs(tr[u][i]);
}
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; i ++)
cin >> s[i], insert(s[i]);
dfs(0);
cout << res << endl;
return 0;
}
G - Merchant Takahashi
Problem Statement
The Kingdom of AtCoder has
N
N
N towns: towns
1
1
1,
2
2
2,
…
\ldots
…,
N
N
N.
To move from town
i
i
i to town
j
j
j, you must pay a toll of
C
×
∣
i
−
j
∣
C \times |i-j|
C×∣i−j∣ yen.
Takahashi, a merchant, is considering participating in zero or more of
M
M
M upcoming markets.
The
i
i
i-th market
(
1
≤
i
≤
M
)
(1 \leq i \leq M)
(1≤i≤M) is described by the pair of integers
(
T
i
,
P
i
)
(T_i, P_i)
(Ti,Pi), where the market is held in town
T
i
T_i
Ti and he will earn
P
i
P_i
Pi yen if he participates.
For all KaTeX parse error: Expected 'EOF', got '&' at position 10: 1 \leq i &̲lt; M, the
i
i
i-th market ends before the
(
i
+
1
)
(i+1)
(i+1)-th market begins.
The time it takes for him to move is negligible.
He starts with
1
0
1
0
100
10^{10^{100}}
1010100 yen and is initially in town
1
1
1.
Determine the maximum profit he can make by optimally choosing which markets to participate in and how to move.
Formally, let
1
0
1
0
100
+
X
10^{10^{100}} + X
1010100+X be his final amount of money if he maximizes the amount of money he has after the
M
M
M markets. Find
X
X
X.
Constraints
1
≤
N
≤
2
×
1
0
5
1 \leq N \leq 2 \times 10^5
1≤N≤2×105
1
≤
C
≤
1
0
9
1 \leq C \leq 10^9
1≤C≤109
1
≤
M
≤
2
×
1
0
5
1 \leq M \leq 2 \times 10^5
1≤M≤2×105
1
≤
T
i
≤
N
1 \leq T_i \leq N
1≤Ti≤N
(
1
≤
i
≤
M
)
(1 \leq i \leq M)
(1≤i≤M)
1
≤
P
i
≤
1
0
13
1 \leq P_i \leq 10^{13}
1≤Pi≤1013
(
1
≤
i
≤
M
)
(1 \leq i \leq M)
(1≤i≤M)
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
N
N
C
C
C
M
M
M
T
1
T_1
T1
P
1
P_1
P1
T
2
T_2
T2
P
2
P_2
P2
⋮
\vdots
⋮
T
M
T_M
TM
P
M
P_M
PM
Output
Print the answer.
Sample Input 1
6 3
4
5 30
2 10
4 25
2 15
Sample Output 1
49
For example, Takahashi can increase his money by
49
49
49 yen by acting as follows:
Move to town
5
5
5. His money becomes
1
0
1
0
100
−
12
10^{10^{100}} - 12
1010100−12 yen.
Participate in the first market. His money becomes
1
0
1
0
100
+
18
10^{10^{100}} + 18
1010100+18 yen.
Move to town
4
4
4. His money becomes
1
0
1
0
100
+
15
10^{10^{100}} + 15
1010100+15 yen.
Participate in the third market. His money becomes
1
0
1
0
100
+
40
10^{10^{100}} + 40
1010100+40 yen.
Move to town
2
2
2. His money becomes
1
0
1
0
100
+
34
10^{10^{100}} + 34
1010100+34 yen.
Participate in the fourth market. His money becomes
1
0
1
0
100
+
49
10^{10^{100}} + 49
1010100+49 yen.
It is impossible to increase his money to
1
0
1
0
100
+
50
10^{10^{100}} + 50
1010100+50 yen or more, so print 49
.
Sample Input 2
6 1000000000
4
5 30
2 10
4 25
2 15
Sample Output 2
0
The toll fee is so high that it is optimal for him not to move from town 1 1 1.
Sample Input 3
50 10
15
37 261
28 404
49 582
19 573
18 633
3 332
31 213
30 377
50 783
17 798
4 561
41 871
15 525
16 444
26 453
Sample Output 3
5000
Sample Input 4
50 1000000000
15
30 60541209756
48 49238708511
1 73787345006
24 47221018887
9 20218773368
34 40025202486
14 28286410866
24 82115648680
37 62913240066
14 92020110916
24 20965327730
32 67598565422
39 79828753874
40 52778306283
40 67894622518
Sample Output 4
606214471001
Note that the output value may exceed the range of a 32 32 32-bit integer.
Solution
后期补一下这题目的视频
Code
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
template<class Info>
struct SGT {
#define l(p) (p << 1)
#define r(p) (p << 1 | 1)
int n;
std::vector<Info> info;
SGT() {}
SGT(int _n, Info _v = Info()) {
init(_n, _v);
}
template<class T>
SGT(std::vector<T> _init) {
init(_init);
}
void init(int _n, Info _v = Info()) {
init(std::vector<Info>(_n, _v));
}
template<class T>
void init(std::vector<T> _init) {
n = _init.size();
info.assign(4 << std::__lg(n), Info());
auto build = [&](auto self, int p, int l, int r) {
if (r - l == 1) {
info[p] = _init[l];
return;
}
int m = l + r >> 1;
self(self, l(p), l, m);
self(self, r(p), m, r);
pull(p);
};
build(build, 1, 0, n);
}
void pull(int p) { // 这个就是 y 总的 pushup
info[p] = info[l(p)] + info[r(p)];
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = l + r >> 1;
if (x < m) {
modify(l(p), l, m, x, v);
} else {
modify(r(p), m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info query(int p, int l, int r, int x, int y) {
if (l >= y or r <= x) {
return Info();
}
if (l >= x and r <= y) {
return info[p];
}
int m = l + r >> 1;
return query(l(p), l, m, x, y) + query(r(p), m, r, x, y);
}
Info query(int l, int r) {
return query(1, 0, n, l, r + 1);
}
};
struct Info {
// 定义要存的变量,比如区间和 sum 或者最大公约数 d 等等,至于下标就不用存了
int mx;
Info(int s = -1e18): mx(s) {}
};
Info operator+(Info a, Info b) {
Info c;
// 对 a(左儿子) 和 b(右儿子) 一通操作合成 c(父结点)
c.mx = max(a.mx, b.mx);
return c;
}
const int N = 2e5 + 10;
int n, c, m;
int a[N], b[N], f[N];
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
cin >> n >> c >> m;
for (int i = 1; i <= m; i ++)
cin >> a[i] >> b[i];
SGT<Info> plus, sub;
plus.init(n + 1), sub.init(n + 1);
plus.modify(1, Info(c)), sub.modify(1, Info(-c));
for (int i = 1; i <= m; i ++) {
int tmp = plus.query(1, a[i]).mx - c * a[i] + b[i];
tmp = max(tmp, sub.query(a[i] + 1, n + 1).mx + c * a[i] + b[i]);
plus.modify(a[i], max(tmp + c * a[i], plus.query(a[i], a[i]).mx));
sub.modify(a[i], max(tmp - c * a[i], sub.query(a[i], a[i]).mx));
f[i] = tmp;
}
int res = 0;
for (int i = 0; i <= m; i ++)
res = max(res, f[i]);
cout << res << endl;
return 0;
}
视频题解
AtCoder Beginner Contest 353(A ~ E + G 题讲解)
最后祝大家早日