AtCoder Beginner Contest 077

Author Avatar
tkandi 11月 05, 2017
  • 在其它设备中阅读本文章

A - Rotation

Time limit : 2sec / Memory limit : 256MB

Score : 100 points

Problem Statement

You are given a grid with $2$ rows and $3$ columns of squares. The color of the square at the i-th row and j-th column is represented by the character $C_{ij}$.
Write a program that prints $YES$ if this grid remains the same when rotated $180$ degrees, and prints $NO$ otherwise.

Constraints

  • $C_{i,j} \ (1 \le i \le 2, 1 \le j \le 3)$ is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

$C_{11}C_{12}C_{13}$
$C_{21}C_{22}C_{23}$

Output

Print $YES$ if this grid remains the same when rotated $180$ degrees; print $NO$ otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated $180$ degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated $180$ degrees.

Sample Input 3

eye
eel

Sample Output 3

NO

Solution

给你一个$2 \times 3$的字符矩阵,问你将他选择$180°$以后是否跟原来一样。
满足的条件为$C_11 = C_23 \ \& \& \ C_12 = C_22 \ \& \& \ C_13 = C21$。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main() {
    char c11, c12, c13, c21, c22, c23;
    cin >> c11 >> c12 >> c13 >> c21 >> c22 >> c23;
    if (c11 != c23 || c12 != c22 || c13 != c21) puts("NO");
    else puts("YES");
    return 0;
}



B - Around Square

Time limit : 2sec / Memory limit : 256MB

Score : 200 points

Problem Statement

Find the largest square number not exceeding $N$. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

  • $1 \le N \le 10^9$
  • $N$ is an integer.

Input

Input is given from Standard Input in the following format:

$N$

Output

Print the largest square number not exceeding $N$.

Sample Input 1

10

Sample Output 1

9

$10$ is not square, but $9 = 3 \times 3$ is. Thus, we print $9$.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169

Solution

问你小于等于$n$的最大的完全平方数。
${\lfloor \sqrt{n} \rfloor}^2$。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

int main() {
    int n;
    scanf("%d", &n);
    n = sqrt(n);
    printf("%d\n", n * n);
    return 0;
}



C - Snuke Festival

Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.
He has $N$ parts for each of the three categories. The size of the i-th upper part is $A_i$, the size of the i-th middle part is $B_i$, and the size of the i-th lower part is $C_i$.
To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.
How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

  • $1 \le N \le 10^5$
  • $1 \le A_i \le 10^9 \ (1 \le i \le N)$
  • $1 \le B_i \le 10^9 \ (1 \le i \le N)$
  • $1 \le C_i \le 10^9 \ (1 \le i \le N)$
  • All input values are integers.

Input

Input is given from Standard Input in the following format:
$N$
$A_1 \cdots A_N$
$B_1 \cdots B_N$
$C_1 \cdots C_N$

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part
Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part
Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87

Solution

有$A_1 \cdots A_n, \ B_1 \cdots B_n, \ C_1 \cdots C_n$,问你$A_i \lt B_j \lt B_k \ (1 \le i,j,k \le n)$的对数。
对于某一类,它们之间的顺序没有关系,所以把$A,B,C$都先排序,对于每个$B_i$,$cnt[i] = \sum_{B_i \lt C_j}^{}{1}$,对于每个$A_i$,$sum[i] = \sum_{A_i \lt B_j}^{}{cnt[j]}$,答案即为$\sum_{i = 1}^n{sum[i]}$。排序后,比某个大的是一个区间,所以对于每个维护一下区间的后缀贡献和。
时间复杂度$O(nlog_2n)$

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long LL;

const int N = 100010;

int n;
int a[N], b[N], c[N], cnt[N];
LL sum[N];

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &b[i]);
    sort(b + 1, b + n + 1);
    for (int i = 1; i <= n; ++i)
        scanf("%d", &c[i]);
    sort(c + 1, c + n + 1);
    for (int i = 1, j = 1; i <= n; ++i) {
        while (j < n && b[i] >= c[j]) ++j;
        if (b[i] >= c[j]) break;
        cnt[i] = n - j + 1;
    }
    for (int i = n; i; --i)
        sum[i] = sum[i + 1] + cnt[i];
    LL res = 0;
    for (int i = 1, j = 1; i <= n; ++i) {
        while (j < n && a[i] >= b[j]) ++j;
        if (a[i] >= b[j]) break;
        res += sum[j];
    }
    printf("%lld\n", res);
    return 0;
}



D - Small Multiple

Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of $K$.

Constraints

$2 le K le 10^5$
$K$ is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of $K$.

Sample Input 1

6

Sample Output 1

3

$12 = 6 \times 2$ yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

$11111 = 41 \times 271$ yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36

Solution

问你$k$的正整数倍中各个数位数字之和的最小值。
比赛中,我没有做出这题,我试了随机化,发现效果很差。比赛后,我想出了一个数位DP,$f[i][j]$代表当前数是$i$位数,$\% k = j$的最小数字和。时间复杂度为$O(k \times 10 \times d)$。$d$为答案的位数,具体答案会多大,我也不知道,反正做个$100$位是不会超时,我觉得最小的答案应该不会超过$100$。后来发现比赛时唯一A掉的就是这么做的。
看了题解以后,发现这道题十分高妙。每个自然数都可以从$0$通过$+ 1$和$\times 10$这两个操作得到,前者对数位和贡献$+ 1$,后者数位和不变,且可以在模意义下做,把其转化为图论,有$k$个点,分别为$0 \cdots k - 1$,对于一个点$u$,到$(u + 1) \% k$有一条权为$1$的边,到$u *10 \% k$有一条权为$0$的边。答案即为点$1$到点$0$的最短距离$+ 1$。
题解还说明了某一位加到了$10$的情况。假设当前这位为$i$,我们一定可以找到$10^i ≡ 10^j (mod \ k) \ (i \lt j)$。所以我们可以把第$i$的$1$移到第$j$位,即可以构造出合法方案。
关于最短路,直接跑堆优化的Dijkstra时间复杂度是$O(klog_2k)$,我们考虑这张图的边权为$0$或$1$,可以使用01-BFS(堆优化Dijkstra的优先队列换成双关队列。向队列中加入新点时,通过权为$0$的边更新的点放在队首,通过权为$1$的边更新的点放在队尾)。时间复杂度为$O(k)$,这是相当优秀的复杂度了。