Codeforces Round #277 (Div. 2) 题解报告。。。突然发现这场给漏了,现在补上(277.5还没做)
比赛链接:http://codeforces.com/contest/486
For a positive integer n let's define a function f:
f(n) = - 1 + 2 - 3 + .. + ( - 1)nn
Your task is to calculate f(n) for a given integer n.
The single line contains the positive integer n (1 ≤ n ≤ 1015).
Print f(n) in a single line.
4
2
5
-3
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);
}
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 to 1, otherwise it is 0. We can define logical OR of three or more logical values in the same manner:
where
is equal to 1 if some ai = 1, otherwise it is equal to 0.
Nam has a matrix A consisting of m rows and n columns. The rows are numbered from 1 to m, columns are numbered from 1 to n. Element at row i (1 ≤ i ≤ m) and column j (1 ≤ j ≤ n) is denoted as Aij. All elements of A are either 0 or 1. From matrix A, Nam creates another matrix B of the same size using formula:
.
(Bij is OR of all elements in row i and column j of matrix A)
Nam gives you matrix B and challenges you to guess matrix A. Although Nam is smart, he could probably make a mistake while calculating matrix B, since size of A can be large.
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 contain n integers separated by spaces describing rows of matrix B (each element of B is either 0 or 1).
In the first line, print "NO" if Nam has made a mistake when calculating B, otherwise print "YES". If the first line is "YES", then also print m rows consisting of n integers representing matrix A that can produce given matrix B. If there are several solutions print any one.
2 2
1 0
0 0
NO
2 3
1 1 1
1 1 1
YES
1 1 1
1 1 1
2 3
0 1 0
1 1 1
YES
0 0 0
0 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]);
}
}
}
}
Nam is playing with a string on his computer. The string consists of n 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 position i (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 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = 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?
The first line contains two space-separated integers n (1 ≤ n ≤ 105) and p (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.
Print the minimum number of presses needed to change string into a palindrome.
8 3
aeabcaez
6
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);
}
869



被折叠的 条评论
为什么被折叠?



