#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