AtCoder Beginner Contest 373(ABCDEF 题)视频讲解

ops/2024/10/17 20:55:36/

A - September

Problem Statement

There are 12 12 12 strings S 1 , S 2 , … , S 12 S_1, S_2, \ldots, S_{12} S1,S2,,S12 consisting of lowercase English letters.
Find how many integers i i i ( 1 ≤ i ≤ 12 ) (1 \leq i \leq 12) (1i12) satisfy that the length of S i S_i Si is i i i.

Constraints

Each S i S_i Si is a string of length between 1 1 1 and 100 100 100, inclusive, consisting of lowercase English letters. ( 1 ≤ i ≤ 12 ) (1 \leq i \leq 12) (1i12)

Input

The input is given from Standard Input in the following format:

S 1 S_1 S1
S 2 S_2 S2
⋮ \vdots
S 12 S_{12} S12

Output

Print the number of integers i i i ( 1 ≤ i ≤ 12 ) (1 \leq i \leq 12) (1i12) such that the length of S i S_i Si is i i i.

Sample Input 1
january
february
march
april
may
june
july
august
september
october
november
december
Sample Output 1
1

There is only one integer i i i such that the length of S i S_i Si is i i i: 9 9 9. Thus, print 1.

Sample Input 2
ve
inrtfa
npccxva
djiq
lmbkktngaovl
mlfiv
fmbvcmuxuwggfq
qgmtwxmb
jii
ts
bfxrvs
eqvy
Sample Output 2
2

There are two integers i i i such that the length of S i S_i Si is i i i: 4 4 4 and 8 8 8. Thus, print 2.

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se secondusing namespace std;signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int res = 0;for (int i = 1; i <= 12; i ++) {string s;cin >> s;if (s.size() == i) res ++;}cout << res << endl;return 0;
}

B - 1D Keyboard

Problem Statement

There is a keyboard with 26 26 26 keys arranged on a number line.
The arrangement of this keyboard is represented by a string S S S, which is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ.
The key corresponding to the character S x S_x Sx is located at coordinate x x x ( 1 ≤ x ≤ 26 ) (1 \leq x \leq 26) (1x26). Here, S x S_x Sx denotes the x x x-th character of S S S.
You will use this keyboard to input ABCDEFGHIJKLMNOPQRSTUVWXYZ in this order, typing each letter exactly once with your right index finger.
To input a character, you need to move your finger to the coordinate of the key corresponding to that character and press the key.
Initially, your finger is at the coordinate of the key corresponding to A. Find the minimal possible total traveled distance of your finger from pressing the key for A to pressing the key for Z. Here, pressing a key does not contribute to the distance.

Constraints

S S S is a permutation of ABCDEFGHIJKLMNOPQRSTUVWXYZ.

Input

The input is given from Standard Input in the following format:

S S S

Output

Print the answer.

Sample Input 1
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Sample Output 1
25

From pressing the key for A to pressing the key for Z, you need to move your finger 1 1 1 unit at a time in the positive direction, resulting in a total traveled distance of 25 25 25. It is impossible to press all keys with a total traveled distance less than 25 25 25, so print 25.

Sample Input 2
MGJYIZDKSBHPVENFLQURTCWOAX
Sample Output 2
223

Solution

具体见文末视频。

Code

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se secondusing namespace std;signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int res = 0;unordered_map<char, int> idx;for (int i = 1; i <= 26; i ++) {char x;cin >> x, idx[x] = i;}for (char i = 'A' + 1; i <= 'Z'; i ++)res += abs(idx[i] - idx[i - 1]);cout << res << endl;return 0;
}

C - Max Ai+Bj

Problem Statement

You are given two integer sequences A A A and B B B, each of length N N N. Choose integers i , j i, j i,j ( 1 ≤ i , j ≤ N ) (1 \leq i, j \leq N) (1i,jN) to maximize the value of A i + B j A_i + B_j Ai+Bj.

Constraints

1 ≤ N ≤ 5 × 1 0 5 1 \leq N \leq 5 \times 10^5 1N5×105
∣ A i ∣ ≤ 1 0 9 |A_i| \leq 10^9 Ai109 ( i = 1 , 2 , … , N ) (i=1,2,\dots,N) (i=1,2,,N)
∣ B j ∣ ≤ 1 0 9 |B_j| \leq 10^9 Bj109 ( j = 1 , 2 , … , N ) (j=1,2,\dots,N) (j=1,2,,N)
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 A 2 A_2 A2 … \dots A N A_N AN
B 1 B_1 B1 B 2 B_2 B2 … \dots B N B_N BN

Output

Print the maximum possible value of A i + B j A_i + B_j Ai+Bj.

Sample Input 1
2
-1 5
3 -7
Sample Output 1
8

For ( i , j ) = ( 1 , 1 ) , ( 1 , 2 ) , ( 2 , 1 ) , ( 2 , 2 ) (i,j) = (1,1), (1,2), (2,1), (2,2) (i,j)=(1,1),(1,2),(2,1),(2,2), the values of A i + B j A_i + B_j Ai+Bj are 2 , − 8 , 8 , − 2 2, -8, 8, -2 2,8,8,2 respectively, and ( i , j ) = ( 2 , 1 ) (i,j) = (2,1) (i,j)=(2,1) achieves the maximum value 8 8 8.

Sample Input 2
6
15 12 3 -13 -1 -19
7 17 -13 -10 18 4
Sample Output 2
33

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se secondusing namespace std;signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);int n;cin >> n;int A = -1e18, B = -1e18;for (int i = 1, x; i <= n; i ++) cin >> x, A = max(A, x);for (int i = 1, x; i <= n; i ++) cin >> x, B = max(B, x);cout << A + B << endl;return 0;
}

D - Hidden Weights

Problem Statement

You are given a directed graph with N N N vertices and M M M edges. The j j j-th directed edge goes from vertex u j u_j uj to vertex v j v_j vj and has a weight of w j w_j wj.
Find one way to write an integer between − 1 0 18 -10^{18} 1018 and 1 0 18 10^{18} 1018, inclusive, to each vertex such that the following condition is satisfied.
Let x i x_i xi be the value written on vertex i i i. For all edges j = 1 , 2 , … , M j=1,2,\dots,M j=1,2,,M, it holds that x v j − x u j = w j x_{v_j} - x_{u_j} = w_j xvjxuj=wj.
It is guaranteed that at least one such assignment exists for the given input.

Constraints

2 ≤ N ≤ 2 × 1 0 5 2 \leq N \leq 2 \times 10^5 2N2×105
1 ≤ M ≤ min ⁡ { 2 × 1 0 5 , N ( N − 1 ) / 2 } 1 \leq M \leq \min\{2 \times 10^5,N(N-1)/2\} 1Mmin{2×105,N(N1)/2}
1 ≤ u j , v j ≤ N 1 \leq u_j, v_j \leq N 1uj,vjN
u j ≠ v j u_j \neq v_j uj=vj
If i ≠ j i \neq j i=j, then ( u i , v i ) ≠ ( u j , v j ) (u_i, v_i) \neq (u_j, v_j) (ui,vi)=(uj,vj) and ( u i , v i ) ≠ ( v j , u j ) (u_i, v_i) \neq (v_j, u_j) (ui,vi)=(vj,uj)
∣ w j ∣ ≤ 1 0 9 |w_j| \leq 10^9 wj109
All input values are integers.
There exists at least one assignment satisfying the conditions.

Input

The input is given from Standard Input in the following format:

N N N M M M
u 1 u_1 u1 v 1 v_1 v1 w 1 w_1 w1
u 2 u_2 u2 v 2 v_2 v2 w 2 w_2 w2
⋮ \vdots
u M u_M uM v M v_M vM w M w_M wM

Output

Let x i x_i xi be the integer written on vertex i i i. Print x 1 , x 2 , … , x N x_1, x_2, \dots, x_N x1,x2,,xN in this order, separated by spaces, on a single line. If there are multiple solutions, you may print any of them.

Sample Input 1
3 3
1 2 2
3 2 3
1 3 -1
Sample Output 1
3 5 2

By setting x = ( 3 , 5 , 2 ) x = (3, 5, 2) x=(3,5,2), we have x 2 − x 1 = w 1 = 2 x_2 - x_1 = w_1 = 2 x2x1=w1=2, x 2 − x 3 = w 2 = 3 x_2 - x_3 = w_2 = 3 x2x3=w2=3, x 3 − x 1 = w 3 = − 1 x_3 - x_1 = w_3 = -1 x3x1=w3=1, satisfying the conditions.
For example, x = ( − 1 , 1 , − 2 ) x = (-1, 1, -2) x=(1,1,2) is also a valid answer.

Sample Input 2
4 2
2 1 5
3 4 -3
Sample Output 2
5 0 6 3

For example, x = ( 0 , − 5 , 4 , 1 ) x = (0, -5, 4, 1) x=(0,5,4,1) and x = ( 5 , 0 , 4 , 1 ) x = (5, 0, 4, 1) x=(5,0,4,1) are also valid answers.

Sample Input 3
5 7
2 1 18169343
3 1 307110901
4 1 130955934
2 3 -288941558
2 5 96267410
5 3 -385208968
4 3 -176154967
Sample Output 3
200401298 182231955 -106709603 69445364 278499365

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se secondusing namespace std;const int N = 2e5 + 10, inf = 0x3f3f3f3f3f3f3f3f;int n, m;
int h[N], e[N << 1], ne[N << 1], w[N << 1], idx;
int res[N];void add(int a, int b, int c) {e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++;
}
void dfs(int u) {for (int i = h[u]; ~i; i = ne[i]) {int v = e[i];if (res[v] != inf) continue;res[v] = res[u] + w[i], dfs(v);}
}signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);memset(h, -1, sizeof h);cin >> n >> m;while (m -- ) {int u, v, c;cin >> u >> v >> c;add(u, v, c), add(v, u, -c);}memset(res, 0x3f, sizeof res);for (int i = 1; i <= n; i ++)if (res[i] == inf) {res[i] = 0;dfs(i);}for (int i = 1; i <= n; i ++)cout << res[i] << " ";cout << endl;return 0;
}

E - How to Win the Election

Problem Statement

An election is being held with N N N candidates numbered 1 , 2 , … , N 1, 2, \ldots, N 1,2,,N. There are K K K votes, some of which have been counted so far.
Up until now, candidate i i i has received A i A_i Ai votes.
After all ballots are counted, candidate i i i ( 1 ≤ i ≤ N ) (1 \leq i \leq N) (1iN) will be elected if and only if the number of candidates who have received more votes than them is less than M M M. There may be multiple candidates elected.
For each candidate, find the minimum number of additional votes they need from the remaining ballots to guarantee their victory regardless of how the other candidates receive votes.
Formally, solve the following problem for each i = 1 , 2 , … , N i = 1,2,\ldots,N i=1,2,,N.
Determine if there is a non-negative integer X X X not exceeding K − ∑ i = 1 N A i K - \displaystyle{\sum_{i=1}^{N}} A_i Ki=1NAi satisfying the following condition. If it exists, find the minimum possible such integer.
If candidate i i i receives X X X additional votes, then candidate i i i will always be elected.

Constraints

1 ≤ M ≤ N ≤ 2 × 1 0 5 1 \leq M \leq N \leq 2 \times 10^5 1MN2×105
1 ≤ K ≤ 1 0 12 1 \leq K \leq 10^{12} 1K1012
0 ≤ A i ≤ 1 0 12 0 \leq A_i \leq 10^{12} 0Ai1012
∑ i = 1 N A i ≤ K \displaystyle{\sum_{i=1}^{N} A_i} \leq K i=1NAiK
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N M M M K K K
A 1 A_1 A1 A 2 A_2 A2 … \ldots A N A_N AN

Output

Let C i C_i Ci be the minimum number of additional votes candidate i i i needs from the remaining ballots to guarantee their victory regardless of how other candidates receive votes. Print C 1 , C 2 , … , C N C_1, C_2, \ldots, C_N C1,C2,,CN separated by spaces.
If candidate i i i has already secured their victory, then let C i = 0 C_i = 0 Ci=0. If candidate i i i cannot secure their victory under any circumstances, then let C i = − 1 C_i = -1 Ci=1.

Sample Input 1
5 2 16
3 1 4 1 5
Sample Output 1
2 -1 1 -1 0

14 14 14 votes have been counted so far, and 2 2 2 votes are left.

The C C C to output is ( 2 , − 1 , 1 , − 1 , 0 ) (2, -1, 1, -1, 0) (2,1,1,1,0). For example:
Candidate 1 1 1 can secure their victory by obtaining 2 2 2 more votes, while not by obtaining 1 1 1 more vote. Thus, C 1 = 2 C_1 = 2 C1=2.
Candidate 2 2 2 can never (even if they obtain 2 2 2 more votes) secure their victory, so C 2 = − 1 C_2 = -1 C2=1.

Sample Input 2
12 1 570
81 62 17 5 5 86 15 7 79 26 6 28
Sample Output 2
79 89 111 117 117 74 112 116 80 107 117 106

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se secondusing namespace std;const int N = 2e5 + 10;int n, m, k;
int a[N], b[N], c[N];signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n >> m >> k;int sum = 0;for (int i = 1; i <= n; i ++)cin >> a[i], b[i] = a[i], sum += a[i];if (n == 1) {cout << 0 << endl;return 0;}if (n == m) {for (int i = 1; i <= n; i ++)cout << 0 << " ";cout << endl;return 0;}sort(b + 1, b + 1 + n);for (int i = 1; i <= n; i ++)c[i] = c[i - 1] + b[i];for (int i = 1; i <= n; i ++) {auto check = [&](int x) -> bool {int now = a[i] + x, rest = k - x - sum, pos = upper_bound(b + 1, b + 1 + n, now) - b - 1;if (a[i] < b[n - m + 1]) {if (pos < n - m + 1) return 0;return (pos - n + m) * (now + 1) - c[pos] + c[n - m] > rest;} else {if (pos < n - m) return 0;// if (a[i] > b[pos]) return (pos - n + m + 1) * (now + 1) - c[pos] + c[max(0ll, n - m - 1)] > rest;return (pos - n + m) * (now + 1) - c[pos] + c[max(0ll, n - m - 1)] + a[i] > rest;}};int lo = 0, ro = k - sum, res = -1;while (lo <= ro) {int mid = lo + ro >> 1;if (check(mid)) ro = mid - 1, res = mid;else lo = mid + 1;}cout << res << " ";}cout << endl;return 0;
}

F - Knapsack with Diminishing Values

Problem Statement

There are N N N types of items. The i i i-th type of item has a weight of w i w_i wi and a value of v i v_i vi. Each type has 1 0 10 10^{10} 1010 items available.
Takahashi is going to choose some items and put them into a bag with capacity W W W. He wants to maximize the value of the selected items while avoiding choosing too many items of the same type. Hence, he defines the happiness of choosing k i k_i ki items of type i i i as k i v i − k i 2 k_i v_i - k_i^2 kiviki2. He wants to choose items to maximize the total happiness over all types while keeping the total weight at most W W W. Calculate the maximum total happiness he can achieve.

Constraints

1 ≤ N ≤ 3000 1 \leq N \leq 3000 1N3000
1 ≤ W ≤ 3000 1 \leq W \leq 3000 1W3000
1 ≤ w i ≤ W 1 \leq w_i \leq W 1wiW
1 ≤ v i ≤ 1 0 9 1 \leq v_i \leq 10^9 1vi109
All input values are integers.

Input

The input is given from Standard Input in the following format:

N N N W W W
w 1 w_1 w1 v 1 v_1 v1
w 2 w_2 w2 v 2 v_2 v2
⋮ \vdots
w N w_N wN v N v_N vN

Output

Print the answer.

Sample Input 1
2 10
3 4
3 2
Sample Output 1
5

By choosing 2 2 2 items of type 1 1 1 and 1 1 1 item of type 2 2 2, the total happiness can be 5 5 5, which is optimal.
Here, the happiness for type 1 1 1 is 2 × 4 − 2 2 = 4 2 \times 4 - 2^2 = 4 2×422=4, and the happiness for type 2 2 2 is 1 × 2 − 1 2 = 1 1 \times 2 - 1^2 = 1 1×212=1.
The total weight is 9 9 9, which is within the capacity 10 10 10.

Sample Input 2
3 6
1 4
2 3
2 7
Sample Output 2
14
Sample Input 3
1 10
1 7
Sample Output 3
12

Solution

具体见文末视频。


Code

#include <bits/stdc++.h>
#define int long long
#define fi first
#define se secondusing namespace std;const int N = 3e3 + 10;int n, m;
priority_queue<int> heap[N];
int dp[N];signed main() {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);cin >> n >> m;for (int i = 1; i <= n; i ++) {int w, v;cin >> w >> v, heap[w].push(v - 1);}for (int i = 1; i <= m; i ++)for (int j = 1; j <= m / i && heap[i].size(); j ++) {int v = heap[i].top();heap[i].pop();for (int k = m; k >= i; k --) dp[k] = max(dp[k], dp[k - i] + v);heap[i].push(v - 2);}cout << *max_element(dp + 1, dp + 1 + m) << endl;return 0;
}

视频题解

AtCoder Beginner Contest 373(A ~ F 题讲解)


最后祝大家早日在这里插入图片描述


http://www.ppmy.cn/ops/119178.html

相关文章

本人实现了Prism对Fluent.Ribbon的适应(Fluent.Ribbon支持Prism架构)

#Fluent.Ribbon支持Prism架构# 饶了一些路&#xff0c;最终还是实现了【Fluent.Ribbon支持Prism架构】 最终实现结果描述如下&#xff08;整理一下过一段时间上图&#xff09;&#xff1a;自定义的Modules&#xff0c;为Fluent.Ribbon样式&#xff0c;主界面中包括顶部的Ribb…

Thinkphp5x远程命令执行 靶场攻略

环境配置 靶场&#xff1a;vulhub/thinkphp/5-rce docker-compose up -d #启动环境 漏洞复现 1.访问靶场&#xff1a;http://172.16.1.198:8080/ 2.远程命令执⾏ POC&#xff1a; ?sindex/think\app/invokefunction&functioncall_user_func_array&vars[0]system…

基于SpringBoot的休闲娱乐代理售票系统设计与实现

1.1研究背景 21世纪&#xff0c;我国早在上世纪就已普及互联网信息&#xff0c;互联网对人们生活中带来了无限的便利。像大部分的企事业单位都有自己的系统&#xff0c;由从今传统的管理模式向互联网发展&#xff0c;如今开发自己的系统是理所当然的。那么开发休闲娱乐代理售票…

什么是IPv6

目前国内的网络正在快速的向IPv6升级中&#xff0c;从网络基础设施如运营商骨干网、城域网&#xff0c;到互联网服务商如各类云服务&#xff0c;以及各类终端设备厂商如手机、电脑、路由器、交换机等。目前运营商提供的IPv6线路主要分为支持前缀授权和不支持前缀授权两种。 说…

《家庭无线网络覆盖项目》

家庭无线网络覆盖报项目 目录 家庭无线网络覆盖项目 家庭无线网络覆盖项目 一、项目概述 二、设备清单及报价 三、安装调试费用 四、总报价 五、服务承诺 家庭无线网络覆盖项目 客户姓名:[客户姓名] 联系方式:[电话号码] 家庭地址:[详细地址] 一、项目概述 为客户…

2024版Clion debug无法查看函数内数组内容 解决办法

参考Clion debug查看数组中的内容&#xff0c;新版本有所变化 众所周知&#xff0c;进入函数的数组debug不显示内容&#xff0c;解决办法如下&#xff1a; 在Evaluate expression中输入 *(int(*)[10])(数组名)

redisson使用笔记

文章目录 spring集成redisson maven配置yml配置使用redisTemplate和redisson的区别 其他项目中看到redisson&#xff0c;看样子像是redis相关类库&#xff0c;实际也确实是。 还是老规矩&#xff0c;见到的要了解&#xff0c;需要的必须掌握&#xff0c;了解一下吧。 spring集成…

Rust设计模式

目录 一、创建型 单例模式 懒汉式饿汉式 工厂模式&#xff08;工厂方法和抽象工厂&#xff09; 简单工厂模式工厂方法抽象工厂 建造者模式原型模式 二、结构型 代理模式桥接模式装饰器模式适配器模式门面模式&#xff08;外观模式&#xff09;组合模式&#xff08;部分整体模式…