Codeforces Round #277 (Div. 2)

来源:互联网 发布:杜兰特伦纳德对决数据 编辑:程序博客网 时间:2024/05/16 13:50

Codeforces Round #277 (Div. 2) 题解报告。。。突然发现这场给漏了,现在补上(277.5还没做)


比赛链接:http://codeforces.com/contest/486




A. Calculating Function
time limit per test
1 second
memory limit per test
256 megabytes

For a positive integer n let's define a functionf:

f(n) =  - 1 + 2 - 3 + .. + ( - 1)nn

Your task is to calculate f(n) for a given integer n.

Input

The single line contains the positive integer n (1 ≤ n ≤ 1015).

Output

Print f(n) in a single line.

Sample test(s)
Input
4
Output
2
Input
5
Output
-3
Note

f(4) =  - 1 + 2 - 3 + 4 = 2

f(5) =  - 1 + 2 - 3 + 4 - 5 =  - 3


没啥可说


#include <cstdio>int main(){    long long n, ans;    scanf("%I64d", &n);    if(n % 2 == 0)        ans = n / 2;    else        ans = n / 2 - n;    printf("%I64d\n", ans);}




B. OR in Matrix
time limit per test
1 second
memory limit per test
256 megabytes

Let's define logical OR as an operation on two logical values (i. e. values that belong to the set{0, 1}) that is equal to 1 if either or both of the logical values is set to1, otherwise it is 0. We can define logicalOR of three or more logical values in the same manner:

where is equal to1 if some ai = 1, otherwise it is equal to0.

Nam has a matrix A consisting ofm rows and n columns. The rows are numbered from1 to m, columns are numbered from1 to n. Element at rowi (1 ≤ i ≤ m) and columnj (1 ≤ j ≤ n) is denoted asAij. All elements ofA are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:

.

(Bij isOR of all elements in row i and column j of matrix A)

Nam gives you matrix B and challenges you to guess matrixA. Although Nam is smart, he could probably make a mistake while calculating matrixB, since size of A can be large.

Input

The first line contains two integer m and n (1 ≤ m, n ≤ 100), number of rows and number of columns of matrices respectively.

The next m lines each containn integers separated by spaces describing rows of matrixB (each element of B is either 0 or 1).

Output

In the first line, print "NO" if Nam has made a mistake when calculatingB, otherwise print "YES". If the first line is "YES", then also printm rows consisting of n integers representing matrix A that can produce given matrixB. If there are several solutions print any one.

Sample test(s)
Input
2 21 00 0
Output
NO
Input
2 31 1 11 1 1
Output
YES1 1 11 1 1
Input
2 30 1 01 1 1
Output
YES0 0 00 1 0


根据B推出A,容易发现,若B矩阵的某个点为0,则对应的A矩阵的该点所在行所在列都为0,若B矩阵的某个点为1,则对应的A矩阵的该点所在行或所在列只要有一个1即可.

因此我们可以输出话A全为1,然后根据B将A必须为0的点置为0,最后只需要判断A中1的情况是否符合要求即可


#include <cstdio>#include <cstring>int const MAX = 105;int A[MAX][MAX], B[MAX][MAX], n, m;int main(){    scanf("%d %d", &n, &m);    for(int i = 0; i < n; i++)    {        for(int j = 0; j < m; j++)        {            scanf("%d", &B[i][j]);            A[i][j] = 1;        }    }    for(int i = 0; i < n; i++)    {        for(int j = 0; j < m; j++)        {            if(B[i][j] == 0)            {                for(int k = 0; k < n; k++)                    A[k][j] = 0;                for(int k = 0; k < m; k++)                    A[i][k] = 0;             }        }    }    bool ok = true, flag;    for(int i = 0; i < n; i++)    {        for(int j = 0; j < m; j++)        {            if(B[i][j])            {                flag = false;                for(int k = 0; k < n; k++)                {                    if(A[k][j])                    {                        flag = true;                        break;                    }                }                if(!flag)                {                    for(int k = 0; k < m; k++)                    {                        if(A[i][k])                        {                            flag = true;                            break;                        }                    }                }                if(!flag)                    ok = false;            }        }    }    if(!ok)        printf("NO\n");    else    {        printf("YES\n");        for(int i = 0; i < n; i++)        {            for(int j = 0; j < m; j++)            {                if(j != m - 1)                    printf("%d ", A[i][j]);                else                    printf("%d\n", A[i][j]);            }        }    }}




C. Palindrome Transformation
time limit per test
1 second
memory limit per test
256 megabytes

Nam is playing with a string on his computer. The string consists ofn lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.

There is a cursor pointing at some symbol of the string. Suppose that cursor is at positioni (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 ifi > 1 or to the end of the string (i. e. positionn) otherwise. The same holds when he presses the right arrow key (ifi = n, the cursor appears at the beginning of the string).

When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.

Initially, the text cursor is at position p.

Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?

Input

The first line contains two space-separated integersn (1 ≤ n ≤ 105) andp (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.

The next line contains n lowercase characters of Nam's string.

Output

Print the minimum number of presses needed to change string into a palindrome.

Sample test(s)
Input
8 3aeabcaez
Output
6
Note

A string is a palindrome if it reads the same forward or reversed.

In the sample test, initial Nam's string is: (cursor position is shown bold).

In optimal solution, Nam may do 6 following steps:

The result, , is now a palindrome.


n个字符组成的字符串,从第p位开始将其变成回文串,求最小操作次数,操作包括左右移动和字母值的前后移动,显然改变字母值的操作次数的最小值是确定的,我们只要求出移动的最小步数即可,找到不成回文串的最左最右端点,判断即可。


#include <cstdio>#include <cmath>#include <algorithm>using namespace std;int const MAX = 1e5 + 5;int main(){    int n, p, ans = 0, l = MAX, r = -1, tmp = MAX;    char s[MAX];    scanf("%d %d", &n, &p);    scanf("%s", s);    p--;    for(int i = 0, j = n - 1; i < j; i ++, j --)    {        if(s[i] != s[j])        {            int len = abs(s[i] - s[j]);            ans += min(len, 26 - len);            l = min(l, i);            r = max(r, i);        }    }    int d = r - l;    if(ans)    {        tmp = min(tmp, min(abs(p - l), abs(p - r)) + d);        int L = l, R = r;        l = n - 1 - R;        r = n - 1 - L;        tmp = min(tmp, min(abs(p - l), abs(p - r)) + d);        ans += tmp;    }    printf("%d\n", ans);}



0 0
原创粉丝点击