前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >codeforces 289 B. Polo the Penguin and Matrix

codeforces 289 B. Polo the Penguin and Matrix

作者头像
xindoo
发布2021-01-21 18:36:38
3260
发布2021-01-21 18:36:38
举报
文章被收录于专栏:XINDOO的专栏XINDOO的专栏

题目链接

题目意思是在n*m的矩阵中,你可以对矩阵中的每个数加或者减d,求最少的操作次数,使得矩阵中所有的元素相同。

虽然在condeforces中被分到了dp一类,但完全可以通过排序,暴力的方法解决。

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档