Matrix Transformation codechef 数学题
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 数学题的更多相关文章
- A Personal Understanding to Matrix Transformation in Graphics
A Personal Understanding to Matrix Transformation in Graphics--------------------------------------- ...
- Computer Transformation(简单数学题+大数)
H - Computer Transformation Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- PCL点云库:对点云进行变换(Using a matrix to transform a point cloud)
点云数据可以用ASCII码的形式存储在PCD文件中(关于该格式的描述可以参考链接:The PCD (Point Cloud Data) file format).为了生成三维点云数据,在excel中用 ...
- UNITY_MATRIX_IT_MV[Matrix] (转载)
转载 http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.n ...
- opencv 之 transformation
getAffineTransform() : calculates an affine transform from three pairs of the corresponding points. ...
- UNITY_MATRIX_IT_MV[Matrix]
http://blog.csdn.net/cubesky/article/details/38682975 前面发了一篇关于unity Matrix的文章. http://blog.csdn.NET/ ...
- SC3聚类 | 拉普拉斯矩阵 | Laplacian matrix | 图论 | R代码
Laplacian和PCA貌似是同一种性质的方法,坐标系变换.只是拉普拉斯属于图论的范畴,术语更加专业了. 要看就把一篇文章看完整,再看其中有什么值得借鉴的,总结归纳理解后的东西才是属于你的. 问题: ...
- 文件浏览器及数码相框 -2.3.2-freetype_arm-2
显示多行文字 两行文字左边对齐 简单使用两个循环显示两行字体 根据上一行字体的宽度来进行下一行左边的计算 #include <sys/mman.h> #include <sys/ty ...
- 文件浏览器及数码相框 -2.3.1freetype_pc
例子 #include <stdio.h> #include <string.h> #include <math.h> #include <ft2build. ...
随机推荐
- python05-09
一.lambda表达式 def f1(): return 123 f2 = lambda : 123 def f3 = (a1,a2): return a1+a2 f4 = lambda a1,a2 ...
- CentOS 使用httpd 配置局域网 yum源
1.上传centos iso而且挂载 mkdir /mnt/cdr/iso mount -o /xxx.iso /mnt/cdr 2.使用createrepo来创建repo文件 cd /mnt/cdr ...
- 2.NetDh框架之简单高效的日志操作类(附源码和示例代码)
前言 NetDh框架适用于C/S.B/S的服务端框架,可用于项目开发和学习.目前包含以下四个模块 1.数据库操作层封装Dapper,支持多种数据库类型.多库实例,简单强大: 此部分具体说明可参考博客: ...
- web 开发之js---ajax 中的两种提交方式ajax post 和 ajax get 实例
()post http://04101334.iteye.com/blog/637695/ ()get function serializeElement(element) { var method ...
- Educational Codeforces Round 18 C. Divide by Three DP
C. Divide by Three A positive integer number n is written on a blackboard. It consists of not more ...
- ubuntu字符界面下显示中文和调整分辨率
1.sudo apt-get install zhcon 2.vi /etc/zhcon.conf 修改下面两行 x_resolution 1024 y_resolution 768 完成这两步后在 ...
- (19)javaWeb项目名称修改
1,选中项目,[右键]-[Refactor]-[Rename] 在弹出面板中,填写New Name 点击[OK] 2,改变运行时目录名称 选中项目[右键]-[Properties] 在弹框中选择选择[ ...
- javaScript查找HTML元素
1.通过id查找 例:查找id="intro"元素 var x=document.getElementById("intro"); 2.通过标签名查找 例:查找 ...
- POJ3692 Kindergarten —— 二分图最大团
题目链接:http://poj.org/problem?id=3692 Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- 计算机学院大学生程序设计竞赛(2015’11)1005 ACM组队安排
1005 ACM组队安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...