codeforces 289 B. Polo the Penguin and Matrix
2013-06-04 17:11
393 查看
题目链接
题目意思是在n*m的矩阵中,你可以对矩阵中的每个数加或者减d,求最少的操作次数,使得矩阵中所有的元素相同。
虽然在condeforces中被分到了dp一类,但完全可以通过排序,暴力的方法解决。
题目意思是在n*m的矩阵中,你可以对矩阵中的每个数加或者减d,求最少的操作次数,使得矩阵中所有的元素相同。
虽然在condeforces中被分到了dp一类,但完全可以通过排序,暴力的方法解决。
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; const int maxn = 10005; int a[maxn]; int main() { int n, m, d; while (scanf("%d %d %d", &n, &m, &d) != EOF) { int t = n*m; for (int i = 0; i < t; i++) { scanf("%d", &a[i]); } sort(a, a+t); int f = 1; int x = t/2; int ans = 0; for (int i = 0; i < t; i++) { if (abs(a[i] - a[x]) % d) { f = 0; puts("-1"); break; } ans += abs(a[i] - a[x])/d; } if (f) printf("%d\n", ans); } return 0; }
相关文章推荐
- codeforces 289 B. Polo the Penguin and Matrix
- codeforces#289_B Polo the Penguin and Matrix
- codeforces 289B - Polo the Penguin and Matrix 二分+dp
- Codeforces-289b J Polo the Penguin and Matrix
- codeforces#289_B Polo the Penguin and Matrix
- B. Polo the Penguin and Matrix
- CodeForces 288C Polo the Penguin and XOR operation (位运算,异或)
- Codeforces Polo the Penguin and Matrix
- Codeforces-289a I Polo the Penguin and Segments
- codeforces 288A:Polo the Penguin and Strings
- CodeForces 289A Polo the Penguin and Segments
- CodeForces 288C Polo the Penguin and XOR operation
- CodeForces 288C - Polo the Penguin and XOR operation(思维)
- CodeForces 288C - Polo the Penguin and XOR operation
- CF 288C (Polo the Penguin and XOR operation)
- Codeforces Round #177 (Div. 2)---D. Polo the Penguin and Houses (组合数学+暴力)
- A. Polo the Penguin and Strings
- Codeforces Round #177 (Div. 1) C. Polo the Penguin and XOR operation(贪心)
- CF_288B_PoloThePenguinAndHouses
- Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)