• 题意:给你一个\(n\)x\(m\)的矩阵,可以对矩阵的所有元素进行\(\pm d\),问能否使得所有元素相等.

  • 题解:我们可以直接记录一个\(n*m\)的数组存入所有数,所以\((a_1+xd)=(a_2+yd)=...=(a_{n*m}+zd)\),我们但看任意一个等式可得:\(a_{x}+nd=a_{y}+md\),移项得:\(a_x-a_y=(n-m)d\),所以可以推出任意两个元素的值一定是\(d\)的倍数,再对数组排序取中位数后直接求答案就好了.

  • 代码:

    int n,m,d;
    vector<int> v; int main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>n>>m>>d; for(int i=1;i<=n*m;++i){
    int x;
    cin>>x;
    v.pb(x);
    } sort(v.begin(),v.end()); bool flag=true;
    for(int i=0;i<n*m;++i){
    for(int j=i+1;j<n*m;++j){
    if(abs(v[i]-v[j])%d){
    flag=false;
    break;
    }
    }
    if(!flag) break;
    } if(!flag){
    cout<<-1<<endl;
    return 0;
    } int mid=v[n*m/2]; int ans=0; for(int i=0;i<n*m;++i){
    ans+=abs(v[i]-mid)/d;
    } cout<<ans<<endl; return 0;
    }

Codeforces Round #177 (Div. 2) B. Polo the Penguin and Matrix (贪心,数学)的更多相关文章

  1. Codeforces Round #177 (Div. 1) 题解【ABCD】

    Codeforces Round #177 (Div. 1) A. Polo the Penguin and Strings 题意 让你构造一个长度为n的串,且里面恰好包含k个不同字符,让你构造的字符 ...

  2. Codeforces Round #177 (Div. 2) 题解

    [前言]咦?如今怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛.一般我报一个数字A,随便再拉一个人选一个数字B.然后開始做第A^B场.假设认为机 ...

  3. Codeforces Round #177 (Div. 1 + Div. 2)

    A. Polo the Penguin and Segments 模拟. B. Polo the Penguin and Matrix 每个数字模d余数必须一样. 枚举结果,可计算操作次数,取最小. ...

  4. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  5. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  6. Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

    A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  7. Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)

    题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...

  8. Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)

    https://codeforces.com/contest/1059/problem/E 题意 给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 ...

  9. Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心

    D. Dasha and Very Difficult Problem 题目连接: http://codeforces.com/contest/761/problem/D Description Da ...

随机推荐

  1. oracle字符集与乱码(转)

    作者:hcling97    http://blog.sina.com.cn/hcling97 2013年5月15日 转载请注明出处 字符集问题一直叫人头疼,究其原因还是不能完全明白其运作原理. 在整 ...

  2. 使用 TensorBoard 可视化模型、数据和训练

    使用 TensorBoard 可视化模型.数据和训练 在 60 Minutes Blitz 中,我们展示了如何加载数据,并把数据送到我们继承 nn.Module 类的模型,在训练数据上训练模型,并在测 ...

  3. window安装nvm

    先说一下背景,最近做的两个项目一个是祖传angularjs1.X版本另一个是react hooks结合tailwindcss,前者angularjs的node版本比较低,而tailwindcss的no ...

  4. Eclipse中给jar包导入JavaDoc的方法

    原文转载自:http://blog.csdn.net/mr_von/article/details/7740138 在使用Java语言开发的过程中,开发人员经常需要用到一些开源的工具包.在使用别人的j ...

  5. 特斯拉Toolbox诊断检测仪工具Tesla诊断电脑 Tesla Toolbox

    Tesla特斯拉Toolbox诊断工具Tesla诊断电脑检测仪 Tesla Toolbox, Tesla Toolbox Diagnostic Tester.Language: English,Deu ...

  6. Python 日志打印之自定义logger handler

    日志打印之自定义logger handler By:授客 QQ:1033553122 #实践环境 WIN 10 Python 3.6.5 #实践代码 handler.py #!/usr/bin/env ...

  7. 深度学习DeepLearning技术实战研修班

    深度学习DeepLearning(Python)实战培训班 时间地点: 2020 年 12 月 18 日-2020 年 12 月 21日 (第一天报到 授课三天:提前环境部署 电脑测试) 一.培训方式 ...

  8. 请谨慎使用 avaliable 方法来申请缓冲区

    问题 今天开始尝试用 Java 写 http 服务器,开局就遇到 Bug. 我先写了一个多线程的.BIO 的 http 服务器,其中接收请求的部分,会将请求的第一行打印出来. 下面是浏览器发出的请求和 ...

  9. :setting:`task_soft_time_limit` celery 异步任务 执行时间限制 内存限制

    https://docs.celeryproject.org/en/stable/userguide/configuration.html?highlight=control_exchange#new ...

  10. 从epoll构建muduo-1 mini-muduo介绍

    https://blog.csdn.net/voidccc/article/details/8719752 ========== https://blog.csdn.net/liangzhao_jay ...