Codeforces Educational Codeforces Round 45 (Rated for Div. 2)题解

A. Commentary Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland Football Cup starts really soon! Commentators from all over the world come to the event.

Organizers have already built nn commentary boxes. mm regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.

If nn is not divisible by mm, it is impossible to distribute the boxes to the delegations at the moment.

Organizers can build a new commentary box paying aa burles and demolish a commentary box paying bb burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.

What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm)?

Input

The only line contains four integer numbers nnmmaa and bb (1n,m10121≤n,m≤10121a,b1001≤a,b≤100), where nn is the initial number of the commentary boxes, mm is the number of delegations to come, aa is the fee to build a box and bb is the fee to demolish a box.

Output

Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by mm). It is allowed that the final number of the boxes is equal to 00.

Examples
input
Copy
9 7 3 8
output
Copy
15
input
Copy
2 7 3 7
output
Copy
14
input
Copy
30 6 17 19
output
Copy
0
Note

In the first example organizers can build 55 boxes to make the total of 1414 paying 33 burles for the each of them.

In the second example organizers can demolish 22 boxes to make the total of 00 paying 77 burles for the each of them.

In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 55

 boxes.

分析】比较一下 n % m * b和 (m - n % m) * a的大小即可

代码

#include
using namespace std;

long long min(long long a, long long b)
{
    if (a > b) return b;
    return a;
}

int main()
{
    long long n, m, ans, c;
    int a, b;
    scanf("%I64d%I64d%d%d", &n, &m, &a, &b);
    c = n % m;
    printf("%I64d", min(c * b, (m - c) * a));
    return 0;
}
B. Micro-World
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a Petri dish with bacteria and you are preparing to dive into the harsh micro-world. But, unfortunately, you don't have any microscope nearby, so you can't watch them.

You know that you have nn bacteria in the Petri dish and size of the ii-th bacteria is aiai. Also you know intergalactic positive integer constant KK.

The ii-th bacteria can swallow the jj-th bacteria if and only if ai>ajai>aj and aiaj+Kai≤aj+K. The jj-th bacteria disappear, but the ii-th bacteria doesn't change its size. The bacteria can perform multiple swallows. On each swallow operation any bacteria ii can swallow any bacteria jjif ai>ajai>aj and aiaj+Kai≤aj+K. The swallow operations go one after another.

For example, the sequence of bacteria sizes a=[101,53,42,102,101,55,54]a=[101,53,42,102,101,55,54] and K=1K=1. The one of possible sequences of swallows is: [101,53,42,102,101––,55,54][101,53,42,102,101_,55,54]  [101,53,42,102,55,54][101,53_,42,102,55,54]  [101––,42,102,55,54][101_,42,102,55,54]  [42,102,55,54][42,102,55,54_]  [42,102,55][42,102,55]. In total there are 33 bacteria remained in the Petri dish.

Since you don't have a microscope, you can only guess, what the minimal possible number of bacteria can remain in your Petri dish when you finally will find any microscope.

Input

The first line contains two space separated positive integers nn and KK (1n21051≤n≤2⋅1051K1061≤K≤106) — number of bacteria and intergalactic constant KK.

The second line contains nn space separated integers a1,a2,,ana1,a2,…,an (1ai1061≤ai≤106) — sizes of bacteria you have.

Output

Print the only integer — minimal possible number of bacteria can remain.

Examples
input
Copy
7 1
101 53 42 102 101 55 54
output
Copy
3
input
Copy
6 5
20 15 10 15 20 25
output
Copy
1
input
Copy
7 1000000
1 1 1 1 1 1 1
output
Copy
7
Note

The first example is clarified in the problem statement.

In the second example an optimal possible sequence of swallows is: [20,15,10,15,20,25][20,15,10,15,20_,25]  [20,15,10,15,25][20,15,10,15_,25]  [20,15,10,25][20,15,10_,25] [20,15,25][20,15_,25]  [20,25][20_,25]  [25][25].

In the third example no bacteria can swallow any other bacteria.

分析】排序一下,a[i] < a[i + 1] && a[i] + k >= a[i + 1] 就吃,注意判断一下a[i] == a[i + 1] 的情况。

代码

#include
#include

using namespace std;

priority_queue, greater> q;

int main()
{
    int n, k, i, x, a1, a2, cnt = 1;
    scanf("%d%d", &n, &k);
    for (i = 1; i <= n; ++i)
    {
        scanf("%d", &x);
        q.push(x);
    }
    a1 = q.top();
    q.pop();
    if (q.empty())
    {
        printf("1");
    }
    else
    {
        while (!q.empty())
        {
            a2 = q.top();
            q.pop();
            if (a2 > a1 && a1 + k >= a2)
                n -= cnt, cnt = 1;
            else if (a2 > a1) cnt = 1;
            else if (a2 == a1) ++cnt;
            a1 = a2;
        }
        printf("%d", n);
    }
    return 0;
}
C. Bracket Sequences Concatenation Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A bracket sequence is a string containing only characters "(" and ")".

A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

You are given nn bracket sequences s1,s2,,sns1,s2,…,sn. Calculate the number of pairs i,j(1i,jn)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence. Operation ++ means concatenation i.e. "()(" + ")()" = "()()()".

If si+sjsi+sj and sj+sisj+si are regular bracket sequences and iji≠j, then both pairs (i,j)(i,j) and (j,i)(j,i) must be counted in the answer. Also, if si+sisi+si is a regular bracket sequence, the pair (i,i)(i,i) must be counted in the answer.

Input

The first line contains one integer n(1n3105)n(1≤n≤3⋅105) — the number of bracket sequences. The following nn lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed 31053⋅105.

Output

In the single line print a single integer — the number of pairs i,j(1i,jn)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence.

Examples
input
Copy
3
)
()
(
output
Copy
2
input
Copy
2
()
()
output
Copy
4
Note

In the first example, suitable pairs are (3,1)(3,1) and (2,2)(2,2).

In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)(1,1),(1,2),(2,1),(2,2).

【分析】用cnt统计每一串括号的情况,遇到'(' +1 遇到 ')' -1,那么cnt为相反数的括号是相互匹配的。但是要注意类似')('这种括号是不能与任何括号匹配的,其特征为 前缀cnt  < 整串cnt && 前缀cnt < 0 ,枚举这个前缀即可。比赛的时候ans用longlong了,但是中间爆了。。debug了一个小时,不知道在玩什么。
代码
#include
#include
#define ZJ 310000
using namespace std;

long long d[710000];
char s[310000];
int js[310000];
int main()
{
    int i, j, n, cnt;
    bool a, b;
    long long ans = 0;
    scanf("%d", &n);
    for (i = 1; i <= n; ++i)
    {
        a = 0;
        cnt = 0;
        scanf("%s", s);
        
        for (j = 0; j < strlen(s); ++j)
        {
            if (s[j] == '(') ++cnt;
            else --cnt;
            js[j + 1] = cnt;
        }
        for (j = 1; j <= strlen(s); ++j)
        {
            if (js[j] < 0 && js[strlen(s)] > js[j]) a = 1;
            js[j] = 0;
        }
        if (a) continue;
        else d[ZJ + cnt]++;
    }
    for (i = 0; i <= ZJ; ++i) ans += d[ZJ + i] * d[ZJ - i];
    printf("%I64d", ans);
    return 0;
}
D. Graph And Its Complement
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given three numbers n,a,bn,a,b. You need to find an adjacency matrix of such an undirected graph that the number of components in it is equal to aa, and the number of components in its complement is bb. The matrix must be symmetric, and all digits on the main diagonal must be zeroes.

In an undirected graph loops (edges from a vertex to itself) are not allowed. It can be at most one edge between a pair of vertices.

The adjacency matrix of an undirected graph is a square matrix of size nn consisting only of "0" and "1", where nn is the number of vertices of the graph and the ii-th row and the ii-th column correspond to the ii-th vertex of the graph. The cell (i,j)(i,j) of the adjacency matrix contains 11 if and only if the ii-th and jj-th vertices in the graph are connected by an edge.

A connected component is a set of vertices XX such that for every two vertices from this set there exists at least one path in the graph connecting this pair of vertices, but adding any other vertex to XX violates this rule.

The complement or inverse of a graph GG is a graph HH on the same vertices such that two distinct vertices of HH are adjacent if and only if they are not adjacent in GG.

Input

In a single line, three numbers are given n,a,b(1n1000,1a,bn)n,a,b(1≤n≤1000,1≤a,b≤n): is the number of vertexes of the graph, the required number of connectivity components in it, and the required amount of the connectivity component in it's complement.

Output

If there is no graph that satisfies these constraints on a single line, print "NO" (without quotes).

Otherwise, on the first line, print "YES"(without quotes). In each of the next nn lines, output nn digits such that jj-th digit of ii-th line must be 11 if and only if there is an edge between vertices ii and jj in GG (and 00 otherwise). Note that the matrix must be symmetric, and all digits on the main diagonal must be zeroes.

If there are several matrices that satisfy the conditions — output any of them.

Examples
input
Copy
3 1 2
output
Copy
YES
001
001
110
input
Copy
3 3 3
output
Copy
NO
【分析】画下图可以发现,a和b至少有一个为1才能符合题意。针对b == 1 随意分成a块,补图一定只有一块,针对
a == 1 ,可以swap(a, b) , 然后输出其补图即可。a == 1 && b == 1 的几种特殊情况要特判。

【代码】

#include
using namespace std;

int d[1100][1100];
int main()
{
    int i, j, k, a, b, n;
    scanf("%d%d%d", &n, &a, &b);
    if (a != 1 && b != 1)
    {
        printf("NO");
    }
    else
    {
        if (a == 1 && b == 1)
        {
            if (n == 1) printf("YES\n0");
            else if (n == 2 || n == 3) printf("NO");
            else
            {
                printf("YES\n");
                for (i = 1; i <= n; ++i)
                    for (j = 1; j <= n; ++j)
                        if (j - 1 == i) d[i][j] = d[j][i] = 1;
                for (i = 1; i <= n; ++i)
                {
                    for (j = 1; j <= n; ++j) printf("%d", d[i][j]);
                    printf("\n");
                }
            }
        }
        else if (b == 1)
        {
            printf("YES\n");
            for (i = a; i < n; ++i) d[i][i + 1] = d[i + 1][i] = 1;
            for (i = 1; i <= n; ++i)
            {
                for (j = 1; j <= n; ++j) printf("%d", d[i][j]);
                printf("\n");
            }
        }
        else
        {
            printf("YES\n");
            for (i = b; i < n; ++i) d[i][i + 1] = d[i + 1][i] = 1;
            for (i = 1; i <= n; ++i)
            {
                for (j = 1; j <= n; ++j)
                    if (i != j) printf("%d", !d[i][j]);
                    else printf("%d", d[i][j]);
                printf("\n");
            }
        }
    }
    return 0;
}

你可能感兴趣的:(Codeforces Educational Codeforces Round 45 (Rated for Div. 2)题解)