AtCoder Beginner Contest 189 A~D Solutions

Translation Notice
This article was machine-translated using DeepSeek-R1.

  • Original Version: Authored in Chinese by myself
  • Accuracy Advisory: Potential discrepancies may exist between translations
  • Precedence: The Chinese text shall prevail in case of ambiguity
  • Feedback: Technical suggestions regarding translation quality are welcomed

A - Slots

Problem Statement

Given three uppercase English letters $C_1,C_2,C_3$, determine if they are all identical.

Input Format

$C_1C_2C_3$

Output Format

Print Won if $C_1,C_2,C_3$ are equal; otherwise, print Lost.

Sample

Input Output
SSS Won
WVW Lost

Analysis

If you can’t solve this problem, you might as well have not learned C++

Code

Note: Do not write Won as Yes or Lost as No!

1
2
3
4
5
6
7
8
9
#include <cstdio>
using namespace std;

int main(int argc, char** argv)
{
    char a = getchar(), b = getchar(), c = getchar();
    puts((a == b && b == c)? "Won": "Lost");
    return 0;
}

B - Alcoholic

Problem Statement

A person will drink $N$ cups of alcohol in order. The $i$-th cup has $V_i$ milliliters with $P_i\%$ alcohol content ($1\le i\le N$).
He gets drunk when the total alcohol consumed exceeds $X$ milliliters (consuming exactly $X$ milliliters won’t cause drunkenness).
After drinking which cup will he get drunk for the first time?

$1\le N\le 10^3$
$0\le X\le 10^6$
$1\le V_i\le 10^3$
$0\le P_i\le 100$

Input Format

$N~X$
$V_1~P_1$
$\vdots$
$V_N~P_N$

Output Format

Print the cup number $i$ if he gets drunk after drinking the $i$-th cup. If he never gets drunk, print -1.

Sample

Sample Input1

1
2
3
2 15
200 5
350 3

Sample Output1

1
2

The first cup contains $200\times5\%=10$ ml of alcohol.
The second cup contains $350\times3\%=10.5$ ml of alcohol.
After drinking the second cup, the total $20.5$ ml exceeds $15$ ml, so output $2$.

Sample Input2

1
2
3
2 10
200 5
350 3

Sample Output2

1
2

He doesn’t get drunk when exactly $X$ ml is consumed.

Sample Input3

1
2
3
4
3 1000000
1000 100
1000 100
1000 100

Sample Output3

1
-1

Seems he’s immune to alcohol…

Analysis

The alcohol in the $i$-th cup is $V_i\times P_i\%$, i.e., $V_i\times P_i/100$ ml.
We need to find the smallest $i$ where the cumulative sum exceeds $X$ ml. However, due to floating-point precision issues in C++, direct calculation may yield incorrect results.

$$V_1\times P_1 + V_2\times P_2 + \dots + V_i\times P_i > 100X$$


This allows integer arithmetic to avoid precision errors.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cstdio>
using namespace std;

int main(int argc, char** argv)
{
    int n, x;
    scanf("%d%d", &n, &x);
    x *= 100;
    for(int i=1; i<=n; i++)
    {
        int v, p;
        scanf("%d%d", &v, &p);
        x -= v * p;
        if(x < 0)
        {
            printf("%d\n", i);
            return 0;
        }
    }
    puts("-1");
    return 0;
}

C - Mandarin Orange

Problem Statement

There are $N$ bowls arranged in a row. The $i$-th bowl contains $A_i$ oranges.
Takahashi selects a triple $(l,r,x)$ satisfying:

  • $1\le l\le r\le N$
  • $x\le A_i$ for all $l\le i\le r$

He eats $x$ oranges from each bowl between $l$ and $r$ (inclusive). Find the maximum number of oranges he can eat.

$1\le N\le 10^4$
$1\le A_i\le 10^5$

Input Format

$N$
$A_1~\dots~A_N$

Output Format

Print the maximum number of oranges.

Sample

Sample Input1

1
2
6
2 4 4 9 4 9

Sample Output1

1
20

Choosing $(2,6,4)$ yields $5\times4=20$ oranges.

Sample Input2

1
2
6
200 4 4 9 4 9

Sample Output2

1
200

Choosing $(1,1,200)$ yields $200$ oranges.

Analysis

For each $(l,r)$, the optimal $x$ is the minimum $A_i$ in $[l,r]$. Compute $(r-l+1)\times\min(A_l,\dots,A_r)$ for all possible $(l,r)$ and find the maximum.

Time complexity: $\mathcal O(n^2)$

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <cstdio>
#define maxn 10005
#define INF 2147483647
using namespace std;

int a[maxn];

inline void setmin(int& a, int b) {if(b < a) a = b;}
inline void setmax(int& a, int b) {if(b > a) a = b;}

int main(int argc, char** argv)
{
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++)
        scanf("%d", a + i);
    int ans = 0;
    for(int l=0; l<n; l++)
    {
        int m = INF;
        for(int r=l; r<n; r++)
        {
            setmin(m, a[r]);
            setmax(ans, (r - l + 1) * m);
        }
    }
    printf("%d\n", ans);
    return 0;
}

D - Logical Expression

Problem Statement

Given $N$ strings $S_1,S_2,...,S_N$ (each being AND or OR), count the number of tuples $(x_0,x_1,...,x_N)$ satisfying:

  • $x_i$ is $\text{True}$ or $\text{False}$
  • $y_0=x_0$
  • For $i\ge 1$: $y_i=y_{i-1}\land x_i$ if $S_i$ is AND, else $y_i=y_{i-1}\lor x_i$

$1\le N\le 60$

Input Format

$N$
$S_1$
$\vdots$
$S_N$

Output Format

Print the count.

Sample

See original contest page for details.

Analysis

$$f(N)=\begin{cases} f(N-1) & (S_N=\text{AND})\\ f(N-1)\times2^N & (S_N=\text{OR}) \end{cases}$$


This allows efficient computation while processing input.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <cstdio>
using namespace std;

int main(int argc, char** argv)
{
    int n;
    scanf("%d", &n);
    char c[4];
    long long ans = 1LL, x = 1LL;
    while(n--)
    {
        x <<= 1LL;
        scanf("%s", c);
        if(c[0] == 'O')
            ans ^= x; // Equivalent to ans += x; this is faster
    }
    printf("%lld\n", ans);
    return 0;
}
Built with Hugo
Theme Stack designed by Jimmy