Codeforces Round #277 (Div. 2) A (水題)B(水題) C(貪心)
A. Calculating Function
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.
InputThe single line contains the positive integer n (1 ≤ n ≤ 1015).
OutputPrint f(n) in a single line.
Sample test(s) Input4
2Input
5Output
-3Note
f(4) = - 1 + 2 - 3 + 4 = 2
f(5) = - 1 + 2 - 3 + 4 - 5 = - 3
分奇數和偶數兩種情況,具體看程式碼。
CODE:
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; int main() { ll n; while(cin>>n){ ll ans = 0; if(n%2){ ans = -(n+1)/2; cout<<ans<<endl; } else{ ans = n - n/2; cout<<ans<<endl; } } return 0; }
B. OR in Matrix
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
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.
InputThe 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).
OutputIn 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.
Sample test(s) Input2 2 1 0 0 0Output
NOInput
2 3 1 1 1 1 1 1Output
YES 1 1 1 1 1 1Input
2 3 0 1 0 1 1 1Output
YES 0 0 0 0 1 0
開始選的策略不對, WA了n 多遍。
首先應該明確矩陣a 和矩陣b 的關係,矩陣Bij 為0 時,矩陣a 的第i行和第j 列都為0;若為1,則矩陣a 的第i行 或第j列上的數至少有一個是1。由矩陣b 判斷矩陣a.
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 105;
int a[MAX][MAX], b[MAX][MAX];
int n, m;
bool jd()
{
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
if(b[i][j]){
int ok = 0;
for(int k = 0; k < m; ++k){
if(a[i][k]){
ok = 1; break;
}
}
for(int k = 0; k < n; ++k){
if(a[k][j]){
ok = 1;
break;
}
}
if(!ok) return false;
}
}
}
return true;
}
int main()
{
//freopen("in","r", stdin);
cin>>n>>m;
memset(a, -1, sizeof(a));
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 < m; ++k){
a[i][k] = 0;
}
for(int k = 0; k < n; ++k){
a[k][j] = 0;
}
}
}
}
if(!jd()) cout<<"NO"<<endl;
else{
cout<<"YES"<<endl;
for(int i = 0; i < n; ++i){
cout<<a[i][0];
for(int j = 1; j < m; ++j){
cout<<" "<<a[i][j];
}
cout<<endl;
}
}
return 0;
}
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output
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?
InputThe 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.
OutputPrint the minimum number of presses needed to change string into a palindrome.
Sample test(s) Input8 3 aeabcaezOutput
6Note
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.
思路:
從中間向兩邊,找到不相同的字母,記錄需要旋轉的次數和位置。然後判斷p 點在左邊還是右邊。永遠只改變同一邊的字母得到的答案最小。
CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
int n, p;
char s[100005];
struct node
{
int x, y;
int c;
}step[100005];
bool cmp1(node a, node b)
{
return a.x < b.x;
}
bool cmp2(node a, node b)
{
return a.y < b.y;
}
int main()
{
//freopen("in", "r", stdin);
cin>>n>>p;
scanf("%s", s+1);
int k = 0;
int ok = 1;
for(int i = 1, j = n; i <= j; ++i, --j){
if(s[i] != s[j]){
ok = 0;
int ii = s[i] - 'a', jj = s[j] - 'a';
step[k].x = i;
step[k].y = j;
step[k].c = min(26 - abs(ii - jj), abs(ii - jj));
k++;
}
}
if(ok){
printf("0\n");
}
else{
int nn = n/2;
int ans = 0, ans1 = 0, ans2 = 0;
for(int i = 0; i < k; ++i){
ans += step[i].c;
}
//printf("%d\n", ans);
sort(step, step+k, cmp1);
if(p <= step[0].x){
ans1 = ans + (step[k - 1].x - p);
}
else if(p >= step[k - 1].x){
ans1 = ans + (p - step[0].x);
}
else{
//printf("%d %d\n", step[0].x, step[k - 1].x);
ans1 = ans + min(p - step[0].x, step[k - 1].x - p);
ans1 += (step[k - 1].x - step[0].x);
}
//printf("%d\n", ans1);
sort(step, step+k, cmp2);
if(p <= step[0].y){
ans2 = ans + (step[k - 1].y - p);
}
else if(p >= step[k - 1].y){
ans2 = ans + (p - step[0].y);
}
else{
ans2 = ans + min(p - step[0].y, step[k - 1].y - p);
ans2 += (step[k - 1].y - step[0].y);
}
printf("%d\n", min(ans1, ans2));
}
// for(int i = 0; i < k; ++i){
// printf("%d ", step[i].c);
// }
// printf("\n");
return 0;
}