Codeforces Round #277 (Div. 2)
来源:互联网 发布:杜兰特伦纳德对决数据 编辑:程序博客网 时间:2024/05/16 13:50
Codeforces Round #277 (Div. 2) 题解报告。。。突然发现这场给漏了,现在补上(277.5还没做)
比赛链接:http://codeforces.com/contest/486
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.
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 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.
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).
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.
2 21 00 0
NO
2 31 1 11 1 1
YES1 1 11 1 1
2 30 1 01 1 1
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]); } } }}
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?
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.
Print the minimum number of presses needed to change string into a palindrome.
8 3aeabcaez
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);}
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277(Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- Codeforces Round #277 (Div. 2)
- 【codeforces】Codeforces Round #277 (Div. 2) 题解
- Codeforces Round #277 (Div. 2) 解题报告
- Codeforces Round #277 (Div. 2) 题解
- 【索引】Codeforces Round #277(Div. 2)
- Codeforces Round #277 (Div. 2)(D)
- Codeforces Round #277 (Div. 2) A B
- Codeforces Round #277 (Div. 2)D题
- Codeforces Round #277 (Div. 2)E题
- Codeforces Round #277 (Div. 2) B
- Codeforces Round #277 (Div. 2) C
- Kendo UI:Grid中单元格为下拉框/时间控件等
- 使用curl来调试你的应用
- 在Tomcat和Eclipse进行远程调试的配置
- Sicily 1198 Substring
- GNU C __attribute__ 机制
- Codeforces Round #277 (Div. 2)
- 多线程技术还是非常重要的
- 君の英語力を試そう…高校生和英翻訳コンテスト 读卖新闻
- jquery 点击元素后,滚动条滚动至该元素位置 - 笔记
- 20个设计优秀的灵动侧边栏
- 有关通知的一些要点
- [iOS-GQiang]iOS md5加密
- 【iOS开源代码】(4):FPPopover
- Java中静态变量与静态方法的继承