当前位置: 首页 > article >正文

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

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 second

using 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 second

using 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 second

using 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 second

using 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 second

using 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 second

using 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.kler.cn/news/338932.html

相关文章:

  • 45集 ESP32 ADC按键程序编写
  • 【Linux】-----进程第二弹(优先级,环境变量)
  • No.13 笔记 | 网络安全防护指南:从法律法规到技术防御
  • 【LeetCode】动态规划—516. 最长回文子序列(附完整Python/C++代码)
  • ffmpeg源码分析(七)结构体之AVStream
  • day03 笔试练习
  • LeetCode讲解篇之322. 零钱兑换
  • 每天五分钟深度学习pytorch:基于pytorch搭建一元线性回归模型
  • Axure中文版:原型设计新手必备工具,轻松上手!
  • 数据挖掘笔记part one (认识数据挖掘)
  • matplotlib中文显示乱码问题
  • 线性代数书中求解齐次线性方程组、非齐次线性方程组方法的特点和缺陷(附实例讲解)
  • 在 JavaScript 中使用 window 对象来刷新页面
  • 深入浅出:现代JavaScript开发者必知必会的Web性能优化技巧
  • 体系结构论文(五十四):Reliability-Aware Runahead 【22‘ HPCA】
  • 简单认识redis-5 jdbc 与 jedis 使用的区别
  • Redis 缓存策略详解:提升性能的四种常见模式
  • springboot整合mybatis案例
  • Spring Boot助力医院数据管理
  • 【C语言】关于指针各项细节以及与其他知识点关联