Codeforces Round #369 (Div. 2) 解题报告(A,B,C)

来源:互联网 发布:淘宝无名体育发货慢 编辑:程序博客网 时间:2024/06/06 04:54

Codeforces Round #369 (Div. 2)


711A-Bus to Udayland
ZS the Coder and Chris the Baboon are travelling to Udayland! To get there, they have to get on the special IOI bus. The IOI bus has n rows of seats. There are 4 seats in each row, and the seats are separated into pairs by a walkway. When ZS and Chris came, some places in the bus was already occupied.

ZS and Chris are good friends. They insist to get a pair of neighbouring empty seats. Two seats are considered neighbouring if they are in the same row and in the same pair. Given the configuration of the bus, can you help ZS and Chris determine where they should sit?

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of rows of seats in the bus.

Then, n lines follow. Each line contains exactly 5 characters, the first two of them denote the first pair of seats in the row, the third character denotes the walkway (it always equals ‘|’) and the last two of them denote the second pair of seats in the row.

Each character, except the walkway, equals to ‘O’ or to ‘X’. ‘O’ denotes an empty seat, ‘X’ denotes an occupied seat. See the sample cases for more details.

Output
If it is possible for Chris and ZS to sit at neighbouring empty seats, print “YES” (without quotes) in the first line. In the next n lines print the bus configuration, where the characters in the pair of seats for Chris and ZS is changed with characters ‘+’. Thus the configuration should differ from the input one by exactly two charaters (they should be equal to ‘O’ in the input and to ‘+’ in the output).

If there is no pair of seats for Chris and ZS, print “NO” (without quotes) in a single line.

If there are multiple solutions, you may print any of them.

Examples
input
6
OO|OX
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX

output
YES
++|OX
XO|XX
OX|OO
XX|OX
OO|OO
OO|XX

题目大意:给出n排座位,每一排左边两个位置,右边两个位置,O表示该位置为空,X表示该位置有人,|隔开。问是否存在两个连续的空位置

题目思路:直接暴力就可以了。

#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace std;typedef long long ll;string s[1005];int main(){    int n;    cin >> n;    int ans = -1;    for (int i = 0; i < n; i++)    {        cin >> s[i];        if (s[i][0] == s[i][1] && s[i][0] == 'O')        {            ans = i * 1000 + 1;        }        if (s[i][3] == s[i][4] && s[i][3] == 'O')        {            ans = i * 1000 + 2;        }    }    if (ans == -1) cout << "NO\n";    else    {        int pos = ans / 1000;        int poi = ans % 1000;        cout << "YES\n";        for (int  i = 0; i < n; i++)        {            if (i == pos)            {                if (poi == 1)                {                    s[i][0] = '+'; s[i][1] = '+';                }                else                {                    s[i][3] = '+'; s[i][4] = '+';                }            }            cout << s[i] << endl;        }    }    return 0;}

711B. Chris and Magic Square

ZS the Coder and Chris the Baboon arrived at the entrance of Udayland. There is a n × n magic grid on the entrance which is filled with integers. Chris noticed that exactly one of the cells in the grid is empty, and to enter Udayland, they need to fill a positive integer into the empty cell.

Chris tried filling in random numbers but it didn’t work. ZS the Coder realizes that they need to fill in a positive integer such that the numbers in the grid form a magic square. This means that he has to fill in a positive integer so that the sum of the numbers in each row of the grid (), each column of the grid (), and the two long diagonals of the grid (the main diagonal — and the secondary diagonal — ) are equal.

Chris doesn’t know what number to fill in. Can you help Chris find the correct positive integer to fill in or determine that it is impossible?

Input
The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the number of rows and columns of the magic grid.

n lines follow, each of them contains n integers. The j-th number in the i-th of them denotes ai, j (1 ≤ ai, j ≤ 109 or ai, j = 0), the number in the i-th row and j-th column of the magic grid. If the corresponding cell is empty, ai, j will be equal to 0. Otherwise, ai, j is positive.

It is guaranteed that there is exactly one pair of integers i, j (1 ≤ i, j ≤ n) such that ai, j = 0.

Output
Output a single integer, the positive integer x (1 ≤ x ≤ 1018) that should be filled in the empty cell so that the whole grid becomes a magic square. If such positive integer x does not exist, output  - 1 instead.

If there are multiple solutions, you may print any of them.

input
3
4 0 2
3 5 7
8 1 6

output
9

题目大意:给出一个n * n 的格子。其中保证有且只有一个格子为空,即为0.问是否能填入一个正整数,使得满足

A[] = 各单行的和
B[] = 各单列的和
C[] = 各对角线的和(两条)
A = B = C

题目思路:也是直接暴力就可以了,但是要注意一些细节。

以下是代码:

#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace std;typedef long long ll;ll num[505][505];ll r[505];ll c[505];int main(){    int n;    cin >> n;    ll ans = -1;    for (int i = 0; i < n; i++)    {        ll s = 0;        for (int j = 0; j < n; j++)        {            cin >> num[i][j];            s += num[i][j];        }        r[i] = s;    }    sort(r,r + n);    if (n == 1) ans = 1;    else    {        ll res = r[1] - r[0];        if (n > 2 && r[2] != r[1]) ans = -1;        else        {            for (int i = 0; i < n; i++)            {                ll s = 0;                for (int j = 0; j < n; j++)                {                    s += num[j][i];                    if (num[j][i] == 0) s += res;                }                c[i] = s;            }            sort(c, c + n);            if (c[0] != c[1]) ans = -1;            else            {                ll s1 = 0,s2 = 0;                for (int i = 0; i < n; i++)                {                    s1 += num[i][i];                    if (num[i][i] == 0) s1 += res;                }                for (int i = 0,j = n - 1; i < n; i++,j--)                {                    s2 += num[i][j];                    if (num[i][j] == 0) s2 += res;                }                if (s1 != s2) ans = -1;                else if (s1 != r[n - 1]) ans = -1;                else if (s1 != c[n - 1]) ans = -1;                else ans = res;            }        }    }    if (ans == 0) ans = -1;    cout << ans << endl;    return 0;}

711C. Coloring Trees

ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the park where n trees grow. They decided to be naughty and color the trees in the park. The trees are numbered with integers from 1 to n from left to right.

Initially, tree i has color ci. ZS the Coder and Chris the Baboon recognizes only m different colors, so 0 ≤ ci ≤ m, where ci = 0 means that tree i is uncolored.

ZS the Coder and Chris the Baboon decides to color only the uncolored trees, i.e. the trees with ci = 0. They can color each of them them in any of the m colors from 1 to m. Coloring the i-th tree with color j requires exactly pi, j litres of paint.

The two friends define the beauty of a coloring of the trees as the minimum number of contiguous groups (each group contains some subsegment of trees) you can split all the n trees into so that each group contains trees of the same color. For example, if the colors of the trees from left to right are 2, 1, 1, 1, 3, 2, 2, 3, 1, 3, the beauty of the coloring is 7, since we can partition the trees into 7 contiguous groups of the same color : {2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.

ZS the Coder and Chris the Baboon wants to color all uncolored trees so that the beauty of the coloring is exactly k. They need your help to determine the minimum amount of paint (in litres) needed to finish the job.

Please note that the friends can’t color the trees that are already colored.

Input
The first line contains three integers, n, m and k (1 ≤ k ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of trees, number of colors and beauty of the resulting coloring respectively.

The second line contains n integers c1, c2, …, cn (0 ≤ ci ≤ m), the initial colors of the trees. ci equals to 0 if the tree number i is uncolored, otherwise the i-th tree has color ci.

Then n lines follow. Each of them contains m integers. The j-th number on the i-th of them line denotes pi, j (1 ≤ pi, j ≤ 109) — the amount of litres the friends need to color i-th tree with color j. pi, j’s are specified even for the initially colored trees, but such trees still can’t be colored.

Output
Print a single integer, the minimum amount of paint needed to color the trees. If there are no valid tree colorings of beauty k, print  - 1.

input
3 2 2
0 0 0
1 2
3 4
5 6

output
10

题目大意:有n课树,存在m种颜色,希望能分成k段。
分段即:

2, 1, 1, 1, 3, 2, 2, 3, 1, 3,{2}, {1, 1, 1}, {3}, {2, 2}, {3}, {1}, {3}.这是7段

然后给出每棵树的初始颜色。
然后给出每棵树染成1~m颜色各自的花费

问最终变成k端花费最少是多少钱。
注意,初始有颜色的树不能染色

题目思路:dp就可以了

dp[105][105][105];  //前i个树,第i个树染成j这种颜色,构成k段的最小花费遍历到第i课树,判断一下这棵树是否已经被染色。* 如果已经被染色,那么花费等于上一棵树的花费* 如果没有被染色,那么遍历该课树可以被染成的颜色(1~m),花费等于上一棵树的花费+染该棵树的花费

以下是代码:

#include <iostream>#include <iomanip>#include <fstream>#include <sstream>#include <cmath>#include <cstdio>#include <cstring>#include <cctype>#include <algorithm>#include <functional>#include <numeric>#include <string>#include <set>#include <map>#include <stack>#include <vector>#include <queue>#include <deque>#include <list>using namespace std;typedef long long ll;#define INF 99999999999ll dp[105][105][105];  //前i个树,第i个树染成j这种颜色,构成k段的最小花费int c[105];  //最初树的颜色int p[105][105];  //第i个树染成j颜色的花费int main(){    int n,m,k;    cin >> n >> m >> k;    for (int i = 0; i < n; i++)    {        cin >> c[i];    }    for (int i = 0; i < n; i++)    {        for (int j = 1; j <= m; j++)        {            cin >> p[i][j];        }    }    memset(dp,1,sizeof(dp)); //花费初始化为很大值    //处理第一课树    if (c[0])  //如果第一棵树有颜色    {        dp[0][c[0]][1] = 0;    }    else    {        for (int i = 1; i <= m; i++) dp[0][i][1] = p[0][i];    }    for (int i = 1; i < n; i++)  //到第i个树    {        if (!c[i])  //如果这棵树可以被染色        {            for (int j = 1; j <= m; j++)  //第i个树染成j这种颜色            {                for (int k = 1; k <= i + 1; k++)  //分成了k段                {                    for(int h = 1; h <= m; h++)  //前面那个树是h这种颜色                    {                        if (j == h)  //如果当前这棵树和前面那棵树染的颜色相同                        {                            dp[i][j][k] = min(dp[i][j][k], dp[i - 1][h][k] + p[i][j]);                        }                        else  //不同,段数 + 1                        {                            dp[i][j][k] = min(dp[i][j][k],dp[i - 1][h][k - 1] + p[i][j]);                        }                    }                }            }        }        else        {            for (int k = 1; k <= i + 1; k++)  //分成了k段            {                for(int h = 1; h <= m; h++)  //前面那个树是h这种颜色                {                    if (c[i] == h)  //如果当前这棵树和前面那棵树染的颜色相同                    {                        dp[i][c[i]][k] = min(dp[i][c[i]][k], dp[i - 1][h][k]);                    }                    else  //不同,段数 + 1                    {                        dp[i][c[i]][k] = min(dp[i][c[i]][k],dp[i - 1][h][k - 1]);                    }                }            }        }    }    ll ans = INF;    for (int i = 1; i <= m; i++)    {        ans = min(ans,dp[n - 1][i][k]);    }    if (ans == INF) ans = -1;    cout << ans << endl;    return 0;}
0 0
原创粉丝点击