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. redux-saga 异步流

    前言 React的作用View层次的前端框架,自然少不了很多中间件(Redux Middleware)做数据处理, 而redux-saga就是其中之一,目前这个中间件在网上的资料还是比较少,估计应用的 ...

  2. Binder系列8—如何使用Binder(转)

    一.Native层Binder 源码结构: ClientDemo.cpp: 客户端程序 ServerDemo.cpp:服务端程序 IMyService.h:自定义的MyService服务的头文件 IM ...

  3. MySQL基础笔记(一) SQL简介+数据类型

    MySQL是一个关系型数据库管理系统(RDBMS),它是当前最流行的 RDBMS 之一.MySQL分为社区版和企业版,由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发 ...

  4. 怎样用Google APIs和Google的应用系统进行集成(8)----怎样把Google Blogger(博客)的JSON Schema转换成XML的Schema(XSD)?

    在Google RESTFul API中,Google Blogger API(Google博客API)应该和我们的生活离得近期:由于差点儿非常多人每天都在看博客,都在写博客,都听说过博客.在前面的G ...

  5. 【JAVA】java中Future、FutureTask的使用

    如今的系统基本都是分布式的,各个系统各司其职的,不可能一个系统干了全部系统的事. 所以系统之间的交互就越来越多了.那么系统之间的交互仅仅有通过网络来交互了,而网络必定会存在延时的情况. 比方A系统的一 ...

  6. ASP.NET for WebApi

    WebApi,听说过吧?呵呵. 感觉比WebService,WCF要强.尤其是那个啥WCF,啥鬼东西,真难懂.真难搞.真难用. 说比WebService要强,是因为不用在本地先生成个代理.而且XML也 ...

  7. socket.io实现在线群聊

    我自己在用socket.io开发,对官方网站上的文档,进行简单的整理,然后自己写了一个简单的聊天程序.最最开始 先安装socket.io: npm install socket.io 利用Node的搭 ...

  8. Machine Learning Note - Note 1

    I am working on the Andrew Ng's course on Machine Learing. I have a question on the week2 session. I ...

  9. hta+vbs+js+div+css (javascript是原生态的)

    talbe是javascript动态生成的,根据你的sql语句来的,分页是vbs用数组来造的轮子,vbs这脚本虽然强大,却没有返回数据集的东东,数组来做简单的分页还是比较简单的,批量跟新呢?是上传ex ...

  10. Consistent Hashing算法

    前几天看了一下Memcached,看到Memcached的分布式算法时,知道了一种Consistent Hashing的哈希算法,上网搜了一下,大致了解了一下这个算法,做下记录. 数据均衡分布技术在分 ...