AtCoder Beginner Contest 297——A-E题讲解

这篇具有很好参考价值的文章主要介绍了AtCoder Beginner Contest 297——A-E题讲解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

蒟蒻来讲题,还望大家喜。若哪有问题,大家尽可提!

Hello, 大家好哇!本初中生蒟蒻讲解一下AtCoder Beginner Contest 297这场比赛的A-E题

今晚比前面几场要简单点,但我在B题翻了下车,第一次提交竟然WA了,做题要仔细啊。

开心的是,今晚终于进到绿名了!
AtCoder Beginner Contest 297——A-E题讲解

===========================================================================================

A - Double Click

原题

Problem Statement

Takahashi turned on a computer at time 0 0 0 and clicked the mouse N N N times. The i i i-th ( 1 ≤ i ≤ N ) (1 \le i \le N) (1iN) click was at time T i T_i Ti.
If he consecutively clicked the mouse at time x 1 x_1 x1 and time x 2 x_2 x2 (where KaTeX parse error: Expected 'EOF', got '&' at position 5: x_1 &̲lt; x_2), a double click is said to be fired at time x 2 x_2 x2 if and only if x 2 − x 1 ≤ D x_2 - x_1 \le D x2x1D.
What time was a double click fired for the first time? If no double click was fired, print -1 instead.

Constraints

1 ≤ N ≤ 100 1 \le N \le 100 1N100
1 ≤ D ≤ 1 0 9 1 \le D \le 10^9 1D109
1 ≤ T i ≤ 1 0 9 ( 1 ≤ i ≤ N ) 1 \le T_i \le 10^9(1 \le i \le N) 1Ti109(1iN)
KaTeX parse error: Expected 'EOF', got '&' at position 5: T_i &̲lt; T_{i+1}(1 \…
All values in the input are integers.

Input

The input is given from Standard Input in the following format:
N N N D D D
T 1 T_1 T1 T 2 T_2 T2 … \dots T N T_N TN

Output

If at least one double click was fired, print the time of the first such event; otherwise, print -1.

Sample Input 1

4 500
300 900 1300 1700

Sample Output 1

1300
Takahashi clicked the mouse at time 900 900 900 and 1300 1300 1300. Since 1300 − 900 ≤ 500 1300 - 900 \le 500 1300900500, a double click was fired at time 1300 1300 1300.
A double click had not been fired before time 1300 1300 1300, so 1300 1300 1300 should be printed.

Sample Input 2

5 99
100 200 300 400 500

Sample Output 2

-1
No double click was fired, so print -1.

Sample Input 3

4 500
100 600 1100 1600

Sample Output 3

600
If multiple double clicks were fired, be sure to print only the first such event.

思路

题目简单,不写了

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define endl '\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)
 
using namespace std;
 
typedef pair<int, int> PII;
 
const int N = 1e2 + 10;
 
int n, d;
int a[N];
 
inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}
 
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> n >> d;
    
    for (int i = 1; i <= n; i ++)
    	cin >> a[i];
    	
    for (int i = 2; i <= n; i ++)
    	if (a[i] - a[i - 1] <= d)
    	{
    		cout << a[i];
    		return 0;
    	}
    	
    cout << -1 << endl;
    
    return 0;
}

B - chess960

原题

Problem Statement
Takahashi is playing a game called Chess960. He has decided to write a code that determines if a random initial state satisfies the conditions of Chess960.
You are given a string $S$ of length eight. $S$ has exactly one K and Q, and exactly two R's, B's , and N's. Determine if $S$ satisfies all of the following conditions. Suppose that the $x$-th and $y$-th $(x < y)$ characters from the left of $S$ are B; then, $x$ and $y$ have different parities. K is between two R's. More formally, suppose that the $x$-th and $y$-th $(x < y)$ characters from the left of $S$ are R and the $z$-th is K; then $x< z < y$. #### Constraints

S S S is a string of length 8 8 8 that contains exactly one K and Q, and exactly two R's, B's , and N's.

Input

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

Output

Print Yes if S S S satisfies the conditions; print No otherwise.

Sample Input 1

RNBQKBNR

Sample Output 1

Yes
The 3 3 3-rd and 6 6 6-th characters are B, and 3 3 3 and 6 6 6 have different parities.
Also, K is between the two R's. Thus, the conditions are fulfilled.

Sample Input 2

KRRBBNNQ

Sample Output 2

No
K is not between the two R's.

Sample Input 3

BRKRBQNN

Sample Output 3

No

思路

题目简单,但要注意不要理解错题意,下标奇偶性不同是字母BK是在两个R之间

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#define endl '\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)
 
using namespace std;
 
typedef pair<int, int> PII;
 
string s;
vector<int> pl;
 
inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}
 
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> s;
    
    int has= 0, k = 0;
    for (int i = 0; i < s.size(); i ++)
    {
    	if (s[i] == 'B')
    		pl.psb(i + 1);
    	if (s[i] == 'K' && has != 1)
    	{
    		cout << "No" << endl;
    		return 0;
    	}
    	if (s[i] == 'R')
    		has ++;
    }
    
    if (pl[0] % 2 != pl[1] % 2)
    	cout << "Yes" << endl;
    else
    	cout << "No" << endl;
    
    return 0;
}

C - PC on the Table

原题

Problem Statement
Planning to place many PCs in his room, Takahashi has decided to write a code that finds how many PCs he can place in his room.
You are given $H$ strings $S_1,S_2,\ldots,S_H$, each of length $W$, consisting of . and T. Takahashi may perform the following operation any number of times (possibly zero): Choose integers satisfying $1\leq i \leq H$ and $1 \leq j \leq W-1$ such that the $j$-th and $(j+1)$-th characters of $S_i$ are both T. Replace the $j$-th character of $S_i$ with P, and $(j+1)$-th with C. He tries to maximize the number of times he performs the operation. Find possible resulting $S_1,S_2,\ldots,S_H$. #### Constraints

1 ≤ H ≤ 100 1\leq H \leq 100 1H100
2 ≤ W ≤ 100 2\leq W \leq 100 2W100
H H H and W W W are integers.
S i S_i Si is a string of length W W W consisting of . and T.

Input

The input is given from Standard Input in the following format:
H H H W W W
S 1 S_1 S1
S 2 S_2 S2
⋮ \vdots
S H S_H SH

Output

Print a sequence of strings, S 1 , S 2 , … , S H S_1,S_2,\ldots,S_H S1,S2,,SH, separated by newlines, possibly resulting from maximizing the number of times he performs the operation.
If multiple solutions exist, print any of them.

Sample Input 1

2 3
TTT
T.T

Sample Output 1

PCT
T.T
He can perform the operation at most once.
For example, an operation with ( i , j ) = ( 1 , 1 ) (i,j)=(1,1) (i,j)=(1,1) makes S 1 S_1 S1 PCT.

Sample Input 2

3 5
TTT…
.TTT.
TTTTT

Sample Output 2

PCT…
.PCT.
PCTPC

思路

找相邻的T更改为PC即可!

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define endl '\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)
 
using namespace std;
 
typedef pair<int, int> PII;
 
const int N = 1e2 + 10;
 
int r, c;
char a[N][N];
 
inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}
 
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> r >> c;
    
    for (int i = 1; i <= r; i ++)
    	for (int j = 1; j <= c; j ++)
    		cin >> a[i][j];
    		
    for (int i = 1; i <= r; i ++)
    	for (int j = 1; j < c; j ++)
    		if (a[i][j] == 'T' && a[i][j] == a[i][j + 1])
    		{
    			a[i][j] = 'P';
    			a[i][j + 1] = 'C';
    			j ++;
    		}
    
    for (int i = 1; i <= r; i ++)
    {
    	for (int j = 1; j <= c; j ++)
    		cout << a[i][j];
    	cout << endl;
    }
    return 0;
}

D - Count Subtractions

原题

Problem Statement

You are given positive integers A A A and B B B.
You will repeat the following operation until A = B A=B A=B:
compare A A A and B B B to perform one of the following two:
if KaTeX parse error: Expected 'EOF', got '&' at position 3: A &̲gt; B, replace A A A with A − B A-B AB;
if KaTeX parse error: Expected 'EOF', got '&' at position 3: A &̲lt; B, replace B B B with B − A B-A BA.
How many times will you repeat it until A = B A=B A=B? It is guaranteed that a finite repetition makes A = B A=B A=B.

Constraints

1 ≤ A , B ≤ 1 0 18 1 \le A,B \le 10^{18} 1A,B1018
All values in the input are integers.

Input

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

Output

Print the answer.

Sample Input 1

3 8

Sample Output 1

4
Initially, A = 3 A=3 A=3 and B = 8 B=8 B=8. You repeat the operation as follows:
KaTeX parse error: Expected 'EOF', got '&' at position 2: A&̲lt;B, so replace B B B with B − A = 5 B-A=5 BA=5, making A = 3 A=3 A=3 and B = 5 B=5 B=5.
KaTeX parse error: Expected 'EOF', got '&' at position 2: A&̲lt;B, so replace B B B with B − A = 2 B-A=2 BA=2, making A = 3 A=3 A=3 and B = 2 B=2 B=2.
KaTeX parse error: Expected 'EOF', got '&' at position 2: A&̲gt;B, so replace A A A with A − B = 1 A-B=1 AB=1, making A = 1 A=1 A=1 and B = 2 B=2 B=2.
KaTeX parse error: Expected 'EOF', got '&' at position 2: A&̲lt;B, so replace B B B with B − A = 1 B-A=1 BA=1, making A = 1 A=1 A=1 and B = 1 B=1 B=1.
Thus, you repeat it four times.

Sample Input 2

1234567890 1234567890

Sample Output 2

0
Note that the input may not fit into a 32-bit integer type.

Sample Input 3

1597 987

Sample Output 3

15

题意

A和B两个数,每次操作将大数变成大数减小数的差值,直到A和B相等时停止,问一共需要操作多少次?

思路

对于A和B,因为最终A==B,所以操作过程中,谁是A,谁是B并不重要。我们不妨设A始终大于B,即把大数给A,那么会出现以下情况:
设cnt=0,用来记录操作数。
情况1:如果A=B,直接输出cnt;
情况2:如果A能整除B,那么cnt+=A/B,输出cnt;
情况3:如果A不能整除B,那么cnt+=A/B,A=A%B;
此时重新回到开始,比较新的A和B,让A是大数,B是小数,再判断以上三种情况。

本题比较简单,如果有兴趣,可以做一下AtCoder Regular Contest 159的B题,难度比这个题目高不少,但作法类似。

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
#define endl '\n'
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)
 
using namespace std;
 
typedef pair<int, int> PII;
 
int a, b;
 
inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}
 
signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> a >> b;
    	
    int cnt = 0;
    while (1)
    	if (a == b)
    	{
    		cout << cnt << endl;
    		return 0;
    	}
    	else if (a < b)
    		swap(a, b);
    	else
    	{
    		if (a % b == 0)
    		{
    			cnt += a / b - 1;
    			cout << cnt << endl;
    			return 0;
    		}
    		else
    		{
    			cnt += a / b;
    			a = a % b;
    		}
    	}
    
    return 0;
}

E - Kth Takoyaki Set

原题

Problem Statement

In AtCoder Kingdom, N N N kinds of takoyakis (ball-shaped Japanese food) are sold. A takoyaki of the i i i-th kind is sold for A i A_i Ai yen.
Takahashi will buy at least one takoyaki in total. He is allowed to buy multiple takoyakis of the same kind.
Find the K K K-th lowest price that Takahashi may pay. Here, if there are multiple sets of takoyakis that cost the same price, the price is counted only once.

Constraints

1 ≤ N ≤ 10 1 \le N \le 10 1N10
1 ≤ K ≤ 2 × 1 0 5 1 \le K \le 2 \times 10^5 1K2×105
1 ≤ A i ≤ 1 0 9 1 \le A_i \le 10^9 1Ai109
All values in the input 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 … \dots A N A_N AN

Output

Print the answer as an integer.

Sample Input 1

4 6
20 25 30 100

Sample Output 1

50
The four kinds of takoyakis sold in AtCoder Kingdom cost 20 20 20 yen, 25 25 25 yen, 30 30 30 yen, and 100 100 100 yen.
The six lowest prices that Takahashi may pay are 20 20 20 yen, 25 25 25 yen, 30 30 30 yen, 40 40 40 yen, 45 45 45 yen, and 50 50 50 yen. Thus, the answer is 50 50 50.
Note that at least one takoyaki must be bought.

Sample Input 2

2 10
2 1

Sample Output 2

10
Note that a price is not counted more than once even if there are multiple sets of takoyakis costing that price.

Sample Input 3

10 200000
955277671 764071525 871653439 819642859 703677532 515827892 127889502 881462887 330802980 503797872

Sample Output 3

5705443819

题意

给定一个N个元素的整数序列a,序列中元素任意组合(同一元素可以多次)的和,判断第K小的和是多少。
如样例1:
a={20,25,30,100}
那么最小的数是20,其次依次是25、30、40(20+20)、45(20+25)、50(20+30 或者 25+25)、55(25+20)、60(30+30)……
所以K=6时,第6小的是50。

思路

这个题目可以这样理解,以样例1为例,最小的数是直接可以知道的,即20,那么从20往后,每次都是用当前数加一遍序列里的所有数,然后把当前数抛掉,再找下一个最小数,这个最小数再把序列里的所有数加一遍,再重复,直到第K个数,用个数轴来表示更清晰一点:
AtCoder Beginner Contest 297——A-E题讲解


代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#define endl '\n'
#define int long long
#define psb(i) push_back(i)
#define ppb() pop_back()
#define psf(i) push_front(i)
#define ppf() pop_front()
#define ps(i) push(i)

using namespace std;

typedef pair<int, int> PII;

const int N = 15;

int n, k;
int a[N];
set<int> s;

inline int read()
{
    int w = 1, s = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') w = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') s = s * 10 + c - '0', c = getchar();
    
    return w * s;
}

signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    
    cin >> n >> k;
    
    for (int i = 1; i <= n; i ++)
    	cin >> a[i];
    
	s.insert(0);
    	
    int cnt = 0;
    while (cnt < k)
    {
    	for (int i = 1; i <= n; i ++)
    		s.insert(*s.cbegin() + a[i]);
    		
    	s.erase(*s.cbegin());
    	cnt ++;
    }
	
	cout << *s.cbegin() << endl;
    
    return 0;
}

今天就到这里了!

大家有什么问题尽管提,我都会尽力回答的!

吾欲您伸手,点的小赞赞。吾欲您喜欢,点得小关注!文章来源地址https://www.toymoban.com/news/detail-438231.html

到了这里,关于AtCoder Beginner Contest 297——A-E题讲解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Atcoder Beginner Contest 311 C - E题讲解

    Problem Statement There is a directed graph with N N N vertices and N N N edges. The i i i -th edge goes from vertex i i i to vertex A i A_i A i ​ . (The constraints guarantee that i ≠ A i i neq A_i i  = A i ​ .) Find a directed cycle without the same vertex appearing multiple times. It can be shown that a solution exists under the constraints of t

    2024年02月15日
    浏览(30)
  • AtCoder Beginner Contest 314(A~D题 + G题)讲解

    Problem Statement The number pi to the 100 100 100 -th decimal place is 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679 . You are given an integer N N N between 1 1 1 and 100 100 100 , inclusive. Print the value of pi to the N N N -th decimal place. More precisely, truncate the value of pi to N N N decim

    2024年02月13日
    浏览(30)
  • AtCoder Beginner Contest 310

    感觉F又双叒叕写复杂了 点杯咖啡,要 (p) 元,但可以用一个优惠券,使得咖啡只要 (q) 元,但你需要额外购买 (n) 个商品中(价格为 (a_i) )的一个。 问点杯咖啡的最小价格。 考虑直接买还是使用优惠券,使用优惠券的话就选 (n) 个商品中价格最小的。 两种情况取最小

    2024年02月16日
    浏览(28)
  • AtCoder Beginner Contest 320

    给定 (a,b) ,输出 (a^b + b^a) 。 因为数不超过 (10) ,可以直接用 pow 计算然后转成 (int) 。不会有精度损失。 神奇的代码 给定一个字符串 (s) ,求长度最长的回文串。 因为长度只有 (100) ,可以直接枚举子串,然后暴力判断是不是回文串(翻转后是否和自身相同),复杂

    2024年02月08日
    浏览(38)
  • AtCoder Beginner Contest 348

    给定 (n) ,输出 (ooxooxoox...) ,长度为 (n) 。 按题意模拟即可。 神奇的代码 给定 (n) 个点,对每个点,求出与其距离最远的点的下标。 (n) 只有 (100) ,对于每个点,花 (O(n)) 遍历每个点最大化距离,时间复杂度为 (O(n^2)) 。 神奇的代码 (n) 个豌豆,每个豌豆有美味值

    2024年04月08日
    浏览(42)
  • AtCoder Beginner Contest 314

    怎么好多陌生单词 审核怎么这么逆天,半小时还没审完 给定 (pi) 的值以及数 (n) ,要求保留 (n) 位小数输出,不四舍五入。 字符串形式储存然后截取末尾即可。 神奇的代码 (n) 个人玩轮盘赌游戏,简单说就是一个转盘有 (37) 个数字以及一个箭头,箭头会等概率停在某

    2024年02月13日
    浏览(40)
  • AtCoder Beginner Contest 335

    给定一个字符串,将最后一位改成 4 。 模拟即可。 神奇的代码 给定 (n) ,按字典序输出非负整数 (i,j,k) ,使得 (i+j+kleq n) (n) 只有 (21) ,直接 (O(n^3)) 枚举判断即可。 神奇的代码 二维网格,贪吃蛇,移动,进行 (q) 次操作,分两种 指定贪吃蛇下一步移动的方向 指定

    2024年01月19日
    浏览(32)
  • AtCoder Beginner Contest 322

    给定一个字符串,找到最先出现 ABC 的位置。 直接查找判断即可。 神奇的代码 给定字符串 s 和 t ,问 s 是不是 t 的前缀和后缀。 根据前后缀定义判断即可。这里试了下 python 神奇的代码 (n) 天,有 (m) 天会放烟花。 问每一天,距离未来最近的放烟花的天数。 两个双指针一

    2024年02月08日
    浏览(33)
  • AtCoder Beginner Contest 301

    给定一个字符串表示高桥和青木每局的获胜情况。 如果高桥获胜局数多,或者两个胜局相等,但高桥率先取得那么多胜场,则高桥获胜,否则青木获胜。 问谁获胜。 按照题意,统计两者的获胜局数比较即可。 如果两者局数相等,可以看最后一局谁胜,青木胜则意味着高桥率

    2024年02月04日
    浏览(55)
  • AtCoder Beginner Contest 303

    给定两个字符串,问这两个字符串是否相似。 两个字符串相似,需要每个字母,要么完全相同,要么一个是 1 一个是 l ,要么一个是 0 一个是 o 按照题意模拟即可。 可以将全部 1 换成 l ,全部 0 换成 o ,再判断相等。 神奇的代码 给定 m 个 n 的排列,问有多少对 ((i, j),i j

    2024年02月07日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包