Codeforces Round #277(Div. 2) (A Calculating Function, B OR in Matrix, C Palindrome Transformation)

#include<iostream>
#include<cstring>
#include<cstdio>
/*
 题意:计算f(n) = -1 + 2 -3 +4.....+(-1)^n *n的值
 思路:偶数和 - 奇数和(或者用等差数列计算化简得到结果)
*/
#include<algorithm>
#define N 10000
using namespace std;

int main(){
    long long n;
    cin>>n;
    if(n%2==0)  cout<<n/2<<endl;
    else cout<<(n-1)/2 - n<<endl;
    return 0;
} 

/*
题意:给定B矩阵,判定能否通过A矩阵得到,如果能,打印出这样的A矩阵
    Bij = Ai1||Ai2....||Ain || A1j || A2j .....|| Amj
思路:如果Bij == 0, 那么A矩阵的第i行和第j列的值都为0;
      然后检查Bij == 1的时候,那么A矩阵的第i行元素和第j列元素至少要一个1!
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 105
using namespace std;

int a[N][N];
int b[N][N];
int row[N], col[N];
int main(){
    int n, m;
    cin>>n>>m;
    for(int i=1; i<=100; ++i)
        for(int j=1; j<=100; ++j)
            a[i][j] = 1;
    bool flag = true;
    for(int i=1; i<=n; ++i)
        for(int j=1; j<=m; ++j){
            cin>>b[i][j];
            if(b[i][j] == 0){
                for(int k=1; k<=m; ++k)
                    a[i][k] = 0;
                for(int k=1; k<=n; ++k)
                    a[k][j] = 0;
            }
        }
    for(int i=1; flag && i<=n; ++i)
        for(int j=1; flag&& j<=m; ++j)
            if(b[i][j] == 1){
                int k, h;
                for(k=1; k<=m; ++k)
                    if(a[i][k]==1) break;
                for(h=1; h<=n; ++h)
                      if(a[h][j] ==1 ) break;
                if(k>m && h>n) flag = false;
            }
    if(flag){
        cout<<"YES"<<endl;
        for(int i=1; i<=n; ++i){
            cout<<a[i][1];
            for(int j=2; j<=m; ++j)
                cout<<" "<<a[i][j];
              cout<<endl;
        }
    }
    else cout<<"NO"<<endl;
    return 0;
} 

/*
题意:从字符串的某一个位置开始,执行向左,向右的操作到达某一个字符的位置,
    通过向上,向下来完成字符的转换,知道字符串变成一个回文串为止!

思路:贪心,在一半的区间完成这些操作一定是最少的(回文串是对称的,所以我们只考虑
      一半的区间是对的),并且最多转一次弯儿!
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std;
char str[N];
int num;
int main(){
    int n, p;
    cin>>n>>p;
    cin>>str+1;
    int len = n;
    if(p>len/2) p = len-p+1;
    int lx = N, rx = -1;
    for(int i=1; i<=len/2; ++i)//找到个需要更改字符区间
        if(str[i] != str[len-i+1]){
            num += min(abs(str[i]-str[len-i+1]), 'z'-max(str[i], str[len-i+1])+(min(str[i], str[len-i+1])-'a')+1);
            if(lx > i) lx=i;
            if(rx < i) rx=i;
        }

    if(lx != N){
        if(lx<=p && rx>=p){
            int d1 = abs(rx-p);
            int d2 = abs(lx-p);
            if(d1>d2) num+=2*d2+d1;
            else num+=2*d1+d2;
        }
        else if(rx<=p)  num+=p-lx;
        else if(lx>=p)  num+=rx-p;
        cout<<num<<endl;
    } else cout<<0<<endl;
    return 0;
}
时间: 2024-11-29 10:17:01

Codeforces Round #277(Div. 2) (A Calculating Function, B OR in Matrix, C Palindrome Transformation)的相关文章

Codeforces Round #157 (Div. 1) C. Little Elephant and LCM (数学、dp)

C. Little Elephant and LCM time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output The Little Elephant loves the LCM (least common multiple) operation of a non-empty set of positive integers. The

Codeforces Round #205 (Div. 2) / 353C Find Maximum (贪心)

Valera has array a, consisting of n integers a0,a1,...,an-1, and function f(x), taking an integer from 0 to 2n-1 as its single argument. Value f(x) is calculated by formula , where value bit(i) equals one if the binary representation of number xconta

Codeforces Round #201 (Div. 1) / 346A Alice and Bob:数论&amp;amp;想法题

A. Alice and Bob http://codeforces.com/problemset/problem/346/A time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output It is so boring in the summer holiday, isn't it? So Alice and Bob have inven

580A. Kefa and First Steps (Codeforces Round #321 (Div. 2))

A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 

codeforces Round #320 (Div. 2) C. A Problem about Polyline(数学) D. &quot;Or&quot; Game(暴力,数学)

解题思路:就是求数 n 对应的二进制数中有多少个 1 解题思路:对(strength, i, j)按照strength进行递减排序,从左到右进行遍历,用b[N]表示i和j有关系!  如果发现b[i]或者b[j]有关系了,则跳过这个strength, 否则b[i] =j, b[j] = i #include <iostream> #include <algorithm> #include<cstdio> using namespace std; struct node{

Codeforces Round #334 (Div. 2) B. More Cowbell

B. More Cowbell time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kevin Sun wants to move his precious collection of n cowbells from Naperthrill to Exeter, where there is actually grass inst

Codeforces 584 B. Kolya and Tanya (Codeforces Round #324 (Div. 2))

B. Kolya and Tanya time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting

Codeforces Round #311 (Div. 2) B. Pasha and Tea

题目链接:http://codeforces.com/contest/557/problem/B 题意:给你n个boy,n个girl ,然后w表示茶壶的最大容量,然后n个茶杯,每个都有不同的容量,要求boy的茶杯里的茶水是girl的两倍,且boy和boy的容量一样,girl和girl的容量一样,问如何倒茶,求最大的倒茶量 #include <iostream> #include <cstdio> #include <algorithm> using namespace

Codeforces 584 D. Dima and Lisa ( Codeforces Round #324 (Div. 2))

D. Dima and Lisa time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dima loves representing an odd number as the sum of multiple primes, and Lisa loves it when there are at most three primes.