https://www.codechef.com/problems/MTRNSFRM

我只能说codechef的题好劲爆,这题居然是easy的题,太可怕了。而且还有一点就是codechef的题解很难看懂╮( ̄▽ ̄")╭

这题可以这样做,首先把两个矩阵合并在一起,然后就是变成了在一个矩阵C中,操作行和列的+1或者-1,最终使得整个矩阵为0。

那么。对于每一行,的操作,我设为row[i],意思就是这一行进行的是什么操作,要么+,要么-,不可能又加又减的,因为都是整行的操作。然后同理设出col[j]。

然后,如果有解,那么需要每个c[i][j] + row[i] + col[j] == 0恒成立。

在上面的方程中,有两个未知数,因为其是互相独立的,那么特殊值一个先,先设i = 1

有col[j] = -row[1] - c[1][j]  ① (这个就是col[j]的方程,因为只有j这个未知数)row[1]可以暴力出来,或者二分出来,反正是常数,那么带入去原来的式子,有:row[i] = row[1] + c[1][j] - c[i][j]

同样是因为独立,所以特殊值那个j = 1,所以row[i] = row[1] + c[1][1] - c[i][1] ②

有了上面两条式子,要判定c[i][j] + row[i] + col[j] == 0是否成立就简单了,带进去即可。

求解:

解的大小是sigma abs(row[i])   + sigma abs(col[j])

目标是最小化这个函数。

其中有一个x(row[1])是还没确定的呢。

把公式拆开,就是sigma(abs(c[i][1] - c[1][1] - x)) + sigma(-c[1][j] - x)

那么就是求一个点x,到c[i][1] - c[1][1]   和  -c[1][j]  这n + m个点的总和最小。

中间值即可。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
vector<LL>a[maxn], ta[maxn];
vector<LL>vc;
void work() {
int n, m;
scanf("%d%d", &n, &m);
vc.clear();
for (int i = ; i <= n; ++i) {
a[i].clear();
ta[i].clear();
a[i].push_back();
}
for (int i = ; i <= ; ++i) {
for (int j = ; j <= n; ++j) {
for (int k = ; k <= m; ++k) {
LL x;
if (i == ) {
scanf("%lld", &x);
a[j].push_back(x);
} else {
scanf("%lld", &x);
a[j][k] -= x;
}
}
}
}
for (int i = ; i <= n; ++i) ta[i] = a[i];
for (int i = ; i <= n; ++i) {
for (int j = ; j <= m; ++j) {
if (a[i][j] + a[][] - a[i][] - a[][j] != ) {
cout << - << endl;
return;
}
}
}
vc.push_back(-((1LL) << ));
for (int i = ; i <= n; ++i) {
vc.push_back(a[i][] - a[][]);
}
for (int j = ; j <= m; ++j) {
vc.push_back(-a[][j]);
}
sort(vc.begin(), vc.end());
LL ans = ;
for (int i = ; i < vc.size(); ++i) {
ans += abs(vc[i] - vc[(n + m + ) / ]);
}
cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}

Matrix Transformation codechef 数学题的更多相关文章

  1. A Personal Understanding to Matrix Transformation in Graphics

    A Personal Understanding to Matrix Transformation in Graphics--------------------------------------- ...

  2. Computer Transformation(简单数学题+大数)

    H - Computer Transformation Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  3. PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)

    点云数据可以用ASCII码的形式存储在PCD文件中(关于该格式的描述可以参考链接:The PCD (Point Cloud Data) file format).为了生成三维点云数据,在excel中用 ...

  4. UNITY_MATRIX_IT_MV[Matrix] (转载)

    转载 http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.n ...

  5. opencv 之 transformation

    getAffineTransform() : calculates an affine transform from three pairs of the corresponding points. ...

  6. UNITY_MATRIX_IT_MV[Matrix]

    http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.NET/ ...

  7. SC3聚类 | 拉普拉斯矩阵 | Laplacian matrix | 图论 | R代码

    Laplacian和PCA貌似是同一种性质的方法,坐标系变换.只是拉普拉斯属于图论的范畴,术语更加专业了. 要看就把一篇文章看完整,再看其中有什么值得借鉴的,总结归纳理解后的东西才是属于你的. 问题: ...

  8. 文件浏览器及数码相框 -2.3.2-freetype_arm-2

    显示多行文字 两行文字左边对齐 简单使用两个循环显示两行字体 根据上一行字体的宽度来进行下一行左边的计算 #include <sys/mman.h> #include <sys/ty ...

  9. 文件浏览器及数码相框 -2.3.1freetype_pc

    例子 #include <stdio.h> #include <string.h> #include <math.h> #include <ft2build. ...

随机推荐

  1. Deepin-安装(读写文件)权限

    在安装NODE管理模块N时,遇到了权限问题 1.给予程序读写权限(仅限文件夹) 查看权限:ls -l 或 ls 添加权限: 示例:chmod +rw xx 实例:chmod +rw node 关于权限 ...

  2. 从头认识java-15.1 填充容器(3)-填充Map

    这一章节我们来讨论一下填充容器的还有一个方面Map.之前的两个章节我们都是用list来作为容器.这一章节我们使用Map. 还有在这里解释一下为什么一直都使用生成器这个东西,事实上他就是建造者设计模式, ...

  3. [IT学习]阿铭Linux 微信公众号 每日一题 解析

    1.shell习题171020公布的昨日答案 习题171019 - 打印正方形 #!/bin/bash read -p "please input a number:" sum a ...

  4. PR 修改保存的增强 ME_UPDATE_REQUISITION

    FUNCTION me_update_requisition."""""""""""&qu ...

  5. 【POJ 3107】 Godfather

    [题目链接] 点击打开链接 [算法] 这题描述有些繁琐,先简化一下题意 : 对于一棵无根树,删除一个节点,使得其余的联通块中,最大的联通块最小 那么,这题就很好做了 对这棵树进行一遍DFS,求出每个节 ...

  6. bzoj1047 [HAOI2007]理想的正方形——二维单调队列

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1047 就是先对行做一遍单调队列,再对那个结果按列做一遍单调队列即可. 代码如下: #incl ...

  7. UI:SQL语句

    sql语句一般不区分大小写,但是我们默认的是关键字要大写是一种好的习惯,比如SELECT 等效于 select.,但是表中的字段,属性区分大小写.Oracle 数据库是一种区分大小写的. Sql语句命 ...

  8. Java Socket实战之三:传输对象

    转自:https://i.cnblogs.com/EditPosts.aspx?opt=1 前面两篇文章介绍了怎样建立Java Socket通信,这一篇说一下怎样使用Java Socket来传输对象. ...

  9. COLORREF

    COLORREF含义及在VC++中的使用 转载 原创 2016年03月11日 23:40:19 4019 所谓真彩色是指显示出来的图像的颜色与真实世界中的颜色非常自然逼真,使得人眼难以区分它们之间的差 ...

  10. JS获得本月的第一天和最后一天

    <script> //本月第一天  function showFirstDay()  {      var Nowdate=new Date();      var MonthFirstD ...