codeforces#289_B Polo the Penguin and Matrix
2014-03-23 18:06
537 查看
题目地址:戳这里
其实就是利用一个很简单的高中数学题
x1<x2<x3<...<xn
f(x)=sigma |x-xi| 那么n为奇数时 取x=x(n+1)/2 当n为偶数时 取x属于 x(n/2)~ x(n/2+1) 都行
代码:
其实就是利用一个很简单的高中数学题
x1<x2<x3<...<xn
f(x)=sigma |x-xi| 那么n为奇数时 取x=x(n+1)/2 当n为偶数时 取x属于 x(n/2)~ x(n/2+1) 都行
代码:
#include<iostream> #include<vector> #include<algorithm> #include<cmath> using namespace std; int p[105][105]; int main() { int n,m,d; cin>>n>>m>>d; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>p[i][j]; int standard=p[0][0]; bool ok=1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if((p[i][j]-standard)%d!=0) { ok=0; break; } if(!ok) cout<<-1<<endl; else { vector<int> v; for(int i=0;i<n;i++) for(int j=0;j<m;j++) v.push_back((p[i][j]-standard)/d); sort(v.begin(),v.end()); int size=v.size(); int ans=0; standard=v[size/2]; for(int i=0;i<size;i++) ans+=abs(v[i]-standard); cout<<ans<<endl; } }
相关文章推荐
- codeforces#289_B Polo the Penguin and Matrix
- Codeforces Polo the Penguin and Matrix
- codeforces 289 B. Polo the Penguin and Matrix
- codeforces 289 B. Polo the Penguin and Matrix
- Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)
- B. Polo the Penguin and Matrix
- codechef Polo the Penguin and the Tree
- 【Educational Codeforces Round 48 (Rated for Div. 2) D】Vasya And The Matrix
- Codeforces-289b J Polo the Penguin and Matrix
- codeforces 289B - Polo the Penguin and Matrix 二分+dp
- Codeforces Round #215 (Div. 1) C. Sereja and the Arrangement of Numbers(欧拉图)
- Codeforces Round #384 B. Chloe and the sequence
- Mahmoud and Ehab and the xor CodeForces - 862C (异或性质) 4000
- 【Educational Codeforces Round 48 (Rated for Div. 2) C】 Vasya And The Mushrooms
- Codeforces Round #430 (Div. 2) 842C Ilya And The Tree(暴力)
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree dfs+set
- Codeforces Round #384 B. Chloe and the sequence
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- Codeforces Round #250 (Div. 1) A. The Child and Toy
- Codeforces Round #250 (Div. 2)——The Child and Set