AtCoder Beginner Contest 171 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 - αlphabet

Problem Summary

Input an English letter $a$ and determine whether it is uppercase or lowercase.

Input Format

$a$

Output Format

If $a$ is lowercase, output a;
If $a$ is uppercase, output A.

Sample Input 1

1
B

Sample Output 1

1
A

Since B is uppercase, output A.

Sample Input 2

1
a

Sample Output 2

1
a

Since a is lowercase, output a.

Code

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

int main(int argc, char** argv)
{
    char a = getchar();
    if('a' <= a && a <= 'z') putchar('a');
    else putchar('A');
    return 0;
}

B - Mix Juice

Problem Summary

Given an array $p_1, p_2, p_3, ..., p_N$ of length $N$, select $K$ elements such that their sum is minimized.

$1\le K\le N\le 1000$
$1\le p_i\le 1000$ ($1\le i\le N$)

Input Format

$N K$
$p_1~p_2~p_3~\dots~p_N$

Output Format

One line containing the minimal sum.

Sample Input 1

1
2
5 3
50 100 80 120 80

The minimal sum is $50+80+80=210$.

Sample Output 1

1
210

Since B is uppercase, output A.

Sample Input 2

1
2
1 1
1000

Sample Output 2

1
1000

Only one element exists, so the minimal sum is $1000$. (Note added by author)

Code

To minimize the sum, select the $K$ smallest elements in the array. Use the sort() function from <algorithm> to sort the array in ascending order, then sum the first $K$ elements.

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

int a[maxn];

int main(int argc, char** argv)
{
    int n, k, sum = 0;
    scanf("%d%d", &n, &k);
    for(int i=0; i<n; i++)
        scanf("%d", a + i);
    sort(a, a + n);
    for(int i=0; i<k; i++)
        sum += a[i];
    printf("%d\n", sum);
    return 0;
}

C - One Quadrillion and One Dalmatians

Problem Summary

There are $1000000000000001$ ($10^{15}+1$) dogs named as:
a, b, .., z, aa, ab, .., az, ba, bb, .., bz, .., za, zb, .., zz, aaa, aab, .., aaz, aba, abb, .., abz, …, zzz, aaaa, …
Question: What is the name of the $N$-th dog?

$1\le N\le 10^{15}+1$

Input Format

$N$

Output Format

One line containing the name of the $N$-th dog.

Samples

Multiple samples are consolidated into a table for brevity:

Input Output
2 b
27 aa
123456789 jjddja

Code

This is equivalent to converting a decimal number to a base-26 representation:

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

int main(int argc, char** argv)
{
    char s[12];
    int cnt = 0;
    long long n;
    scanf("%lld", &n);
    while(n > 0)
    {
        n --;
        s[cnt++] = n % BASE + 'a';
        n /= BASE;
    }
    for(int i=cnt-1; i>=0; i--) putchar(s[i]);
    putchar('\n');
    return 0;
}

D - Replacing

Problem Summary

Given an array $A_1, A_2, \dots, A_N$, perform $Q$ operations:

  • In the $i$-th operation, replace all occurrences of $B_i$ with $C_i$.
  • Output the sum of all elements in $A$ after each operation (denoted as $S_i$).

$1\le N, Q, A_i, B_i, C_i\le 10^5$ ($1\le i\le N$)
$B_i\ne C_i$

Input Format

$N$
$A_1~A_2~...~A_N$
$Q$
$B_1~C_1$
$B_2~C_2$
$:$
$B_Q~C_Q$

Output Format

$S_1$
$S_2$
$:$
$S_N$

Sample Input 1

1
2
3
4
5
6
4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

1
2
3
11
12
16
Step Array $A$
Initial $\{1, 2, 3, 4\}$
$i=1$ $\{2, 2, 3, 4\}$
$i=2$ $\{2, 2, 4, 4\}$
$i=3$ $\{4, 4, 4, 4\}$

Sample Input 2

1
2
3
4
5
6
4
1 1 1 1
3
1 2
2 1
3 5

Note: $B_i$ may not exist in the array.

Sample Output 2

1
2
3
8
4
4

Sample Input 3

1
2
3
4
5
6
2
1 2
3
1 100
2 100
100 1000

Sample Output 3

1
2
3
102
200
2000

Code

Use an array to track the frequency of each value in $A$. Also maintain the total sum of the array.

 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
30
31
32
33
34
35
36
37
38
#include <cstdio>
#define maxn 100005
using namespace std;

typedef long long LL;

int cnt[maxn];
LL sum = 0;

inline LL s(const LL& i)
{
    return cnt[i] * i;
}

int main(int argc, char** argv)
{
    int n, q;
    scanf("%d", &n);
    for(int i=0; i<n; i++)
    {
        int t;
        scanf("%d", &t);
        sum += t;
        cnt[t] ++;
    }
    scanf("%d", &q);
    while(q--)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        sum -= s(x) + s(y);
        cnt[y] += cnt[x];
        cnt[x] = 0;
        sum += s(y);
        printf("%lld\n", sum);
    }
    return 0;
}
Built with Hugo
Theme Stack designed by Jimmy