AtCoder题解 —— AtCoder Beginner Contest 184 —— B - Quizzes

题目相关

题目链接

AtCoder Beginner Contest 184 B 题,https://atcoder.jp/contests/abc184/tasks/abc184_b

Problem Statement

Takahashi will answer N quiz questions.
Initially, he has X points. Each time he answers a question, he gains 1 point if his answer is correct and loses 1 point if it is incorrect.
However, there is an exception: when he has 0 points, he loses nothing for answering a question incorrectly.

You will be given a string S representing whether Takahashi's answers are correct.
If the i-th character of S from the left is o, it means his answer for the i-th question is correct; if that character is x, it means his answer for the i-th question is incorrect.
How many points will he have in the end?

Input

Input is given from Standard Input in the following format:

N X
S

Output

Print the number of points Takahashi will have in the end.

Samples1

Sample Input 1

3 0
xox

Sample Output 1

0

Explaination

Initially, he has 0 points.
He answers the first question incorrectly but loses nothing because he has no point.
Then, he answers the second question correctly, gains 1 point, and now has 1 point.
Finally, he answers the third question incorrectly, loses 1 point, and now has 0 points.
Thus, he has 0 points in the end. We should print 0.

Samples2

Sample Input 2

20 199999
oooooooooxoooooooooo

Sample Output 2

200017

Samples3

Sample Input 3

20 10
xxxxxxxxxxxxxxxxxxxx

Sample Output 3

0

Constraints

  • 1≤N≤2×10^5
  • 0≤X≤2×10^5
  • S is a string of length N consisting of o and x.

题解报告

题目翻译

高桥要回答 N 个测试问题。开始的时候有 X 分。每次正确回答一个测试问题,分数加一分。每次错误回答一个测试问题,分数减一分。但是,有一个特殊情况,也就是如果当前分数为零分的时候,回答错误不扣分。

我们给一个字符串 S 表示着 N 个问题的回答。其中第 i 个问题是字符串 S 的第 i 个字符,如果字符是 o,表示回答正确,如果字符是 x,表示回答错误。

请计算高桥的最后得分。

题目分析

本题是一个模拟题,只需要从头到尾分析字符串 S 的第 i 个字符,根据字符的情况计算对应的积分即可。唯一注意,不能出现负分。

数据范围估计

字符串的最大长度为 2*10^5,初始分数最大值为 2*10^5,因此最高分为 2*10^5+2^10*5=4*10^5。最低分自然就是 0 分了。因此用 int 表示足够。

AC 参考代码

//https://atcoder.jp/contests/abc184/tasks/abc184_b
//B - Quizzes
#include <bits/stdc++.h>

using namespace std;

//如果提交到OJ,不要定义 __LOCAL
//#define __LOCAL

int main() {
#ifndef __LOCAL
    //这部分代码需要提交到OJ,本地调试不使用
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    int n,x;
    string s;
    cin>>n>>x>>s;

    for (int i=0; i<n; i++) {
        if ('o'==s[i]) {
            x++;
        } else if ('x'==s[i] && x>0) {
            x--;
        }
    }

    cout<<x<<"\n";

#ifdef __LOCAL
    //这部分代码不需要提交到OJ,本地调试使用
    system("pause");
#endif
    return 0;
}

时间复杂度

O(N)。

空间复杂度

O(N)。

AtCoder Beginner Contest 134 是一场 AtCoder 的入门级比赛,以下是每道的简要题解: A - Dodecagon 目描述:已知一个正十二边形的边长,求它的面积。 解思路:正十二边形的内角为 $150^\circ$,因此可以将正十二边形拆分为 12 个等腰三角形,通过三角形面积公式计算面积即可。 B - Golden Apple 目描述:有 $N$ 个苹果和 $D$ 个盘子,每个盘子最多可以装下 $2D+1$ 个苹果,求最少需要多少个盘子才能装下所有的苹果。 解思路:每个盘子最多可以装下 $2D+1$ 个苹果,因此可以将苹果平均分配到每个盘子中,可以得到最少需要 $\lceil \frac{N}{2D+1} \rceil$ 个盘子。 C - Exception Handling 目描述:给定一个长度为 $N$ 的整数序列 $a$,求除了第 $i$ 个数以外的最大值。 解思路:可以使用两个变量 $m_1$ 和 $m_2$ 分别记录最大值和次大值。遍历整个序列,当当前数不是第 $i$ 个数时,更新最大值和次大值。因此,最后的结果应该是 $m_1$ 或 $m_2$ 中较小的一个。 D - Preparing Boxes 目描述:有 $N$ 个盒子和 $M$ 个物品,第 $i$ 个盒子可以放入 $a_i$ 个物品,每个物品只能放在一个盒子中。现在需要将所有的物品放入盒子中,每次操作可以将一个盒子内的物品全部取出并分配到其他盒子中,求最少需要多少次操作才能完成任务。 解思路:首先可以计算出所有盒子中物品的总数 $S$,然后判断是否存在一个盒子的物品数量大于 $\lceil \frac{S}{2} \rceil$,如果存在,则无法完成任务。否则,可以用贪心的思想,每次从物品数量最多的盒子中取出一个物品,放入物品数量最少的盒子中。因为每次操作都会使得物品数量最多的盒子的物品数量减少,而物品数量最少的盒子的物品数量不变或增加,因此这种贪心策略可以保证最少需要的操作次数最小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值