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. ...
随机推荐
- js 计算两个日期之间 相差几年几月几日
1.计算日期差 Mine.vue <!-- 我的 --> <template> <div> <!-- 标题栏 --> <x-header :lef ...
- vux tabbar 组件
1.App.vue <!-- 入口文件 --> <template> <div id="app"> <!-- 视图层 --> < ...
- 我的gulp.js清单
var gulp = require('gulp'), cssmin = require('gulp-clean-css'), //压缩css文件 concat = require('gulp-con ...
- Mac Chrome-点击书签页在新的标签打开之方法
PS:一直使用的是Firefox,但是现在Firefox有些不能满足我现在的需求,所以下载了chrome.可是当使用chrome时发现有一个很实用的功能它不能设置,这个让我很抓狂. 当点击标签时不能新 ...
- oracle 导出导入不含数据的空库
10g或之前,用exp导出,imp导入,带上rows=n参数 11g或以上,用expdp导出,impdp导入,带上CONTENT = METADATA_ONLY 参数 expdp带上此参数,不导出数据 ...
- Linux中的mysql.redis
1,Linux上的mysql MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源 ...
- AndroidEventBus总结
什么是AndroidEventBus? android事件总线,是一个发布 / 订阅的事件总线 github地址:https://github.com/greenrobot/EventBus Andr ...
- Java内部类用法
内部类可以是静态(static)的,可以使用 public.protected 和 private 访问控制符,而外部类只能使用 public,或者默认. 成员式内部类 在外部类内部直接定义(不在方法 ...
- silverlight DataGrid 显示篇
silverlight DataGrid 显示篇 分类: Silverlight2012-05-12 21:55 693人阅读 评论(0) 收藏 举报 datagridsilverlightbindi ...
- Masonry复杂ScrollView布局
前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自动布局,也有人使用frame来适配.对于前者,笔者并不喜欢,也不支持.对于后者,更是麻烦,到处计算高度.宽度等,千万 ...