题解 biology
赛时靠spfa求最长路骗了30pts
- spfa的时间复杂度是\(O(k|E|)\),不是\(O(k|N|)\)!
- dijkstra 时间复杂度\(O((n+m)logn)\)
- 特别注意这两个的复杂度都和边数密切相关
spfa的话按a值分层,按层建边即可
正解是个dp,考场上想到dp,但dp思路错了
令\(dp[i][j]\)为在\((i, j)\)位置结束时的最大和
则
\]
又一个带绝对值的题,可以分情况讨论
对于一个点\((i, j)\),可以用四个二维树状数组分别维护\(i,j\)为正,负时的最大值,
根据点\((i, j)\)和\((i', j')\)的位置关系查询即可,复杂度\(O(nmlognlogm)\)
但是还有一种\(O(nm)\)的解法:
题面里那个 \(|i-i'|+|j-j'|\) 其实是曼哈顿距离
这里我们要求其最大值
- 曼哈顿距离在处理绝对值时,常转换为切比雪夫距离以简化计算
考虑转化为切比雪夫距离,那方程可以化为
\]
要求距离尽可能大,所以我们可以维护全局 \(max\ \{dp[i'][j']+i', dp[i'][j']-i', dp[i'][j']+j', dp[i'][j']-j'\}\), 转移时用这个分情况转移
yysy,我因为脑残没仔细看题面a=0的点舍弃从下午3点调到6点
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffffffff
#define N 2010
#define ll long long
#define ld long double
#define usd unsigned
#define ull unsigned long long
//#define int long long
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
char buf[1<<21], *p1=buf, *p2=buf;
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n, m;
namespace force{
int tot, pos;
int head[N*N], size, lim[N*N], lcnt; ll dis[N*N];
bool vis[N*N];
int ss, st;
struct point{int a, b, x, y, rk; ll w;}mp[N*N];
inline bool operator < (point a, point b) {return a.a<b.a;}
struct edge{int to, next, val;}e[N*N*3];
inline void add(int s, int t, int w) {edge* k=&e[++size]; k->to=t; k->val=w; k->next=head[s]; head[s]=size;}
void spfa(int s) {
queue<int> q;
dis[s]=0; vis[s]=1; q.push(s);
int t;
while (q.size()) {
t=q.front(); q.pop();
vis[t]=0;
for (int i=head[t],v; i; i=e[i].next) {
v = e[i].to;
//cout<<t<<": "<<dis[t]<<' '<<mp[t].w<<' '<<e[i].val<<' '<<dis[v]<<endl;
if (dis[t]+mp[t].w+e[i].val < dis[v]) {
dis[v] = dis[t]+mp[t].w+e[i].val;
if (!vis[v]) q.push(v);
}
}
}
//cout<<"dis: "; for (int i=1; i<=tot+2; ++i) cout<<dis[i]<<' '; cout<<endl;
}
void solve() {
tot=0; for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) mp[++tot].a=read(), mp[tot].x=i, mp[tot].y=j, mp[tot].rk=tot;
tot=0; for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) mp[++tot].w=-read();
sort(mp+1, mp+tot+1);
//for (int i=1; i<=tot; ++i) cout<<mp[i].a<<' '; cout<<endl;
for (pos=1; pos<=tot; ++pos) if (mp[pos].a!=mp[pos-1].a) lim[++lcnt]=pos;
lim[++lcnt]=tot+1;
for (int i=1; i<=tot+10; ++i) dis[i]=INF;
ss=tot+1; st=tot+2;
for (int i=lim[1]; i<lim[2]; ++i) add(ss, i, 0); //, cout<<"ss: "<<ss<<' '<<i<<endl;
for (int i=lim[lcnt-1]; i<=tot; ++i) add(i, st, 0); //, cout<<"st: "<<i<<' '<<st<<endl;
for (int i=1; i<lcnt; ++i) {
for (int j=lim[i]; j<lim[i+1]; ++j) {
for (int k=lim[i+1]; k<lim[i+2]; ++k) add(j, k, -(abs(mp[j].x-mp[k].x)+abs(mp[j].y-mp[k].y))); //, cout<<"add "<<j<<' '<<k<<' '<<-(abs(mp[j].x-mp[k].x)+abs(mp[j].y-mp[k].y))<<endl;
}
}
spfa(ss);
printf("%lld\n", -dis[st]);
}
}
namespace task{
// 0->i 1->-i 2->j 3->-j
int tot, lcnt, pos[4], pos2;
ll maxn[4], dp[N*N], ans;
struct point{int a, i, j; ll b;}p[N*N];
inline bool operator < (point a, point b) {return a.a<b.a;}
struct line{int l, r; inline void build(int l_, int r_) {l=l_; r=r_;}}lin[N*N];
void solve() {
memset(maxn, 128, sizeof(maxn));
tot=0; for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) p[++tot].a=read(), p[tot].i=i+j, p[tot].j=i-j;
tot=0; for (int i=1; i<=n; ++i) for (int j=1; j<=m; ++j) p[++tot].b=read();
sort(p+1, p+tot+1);
for (pos2=1; !p[pos2].a; ++pos2) ;
for (int i=pos2; i<=tot; ++i) {
lin[++lcnt].l=i;
for (int j=i+1; j<=tot&&(p[j].a==p[j-1].a); ++i,++j) ;
lin[lcnt].r=i;
}
//for (int i=1; i<=lcnt; ++i) cout<<lin[i].l<<","<<lin[i].r<<endl;
//for (int i=1; i<=tot; ++i) cout<<p[i].i<<' '<<p[i].j<<endl;
int i1, j1;
for (int j=lin[1].l; j<=lin[1].r; ++j) {
i1=p[j].i, j1=p[j].j;
dp[j] = p[j].b;
ans = max(ans, dp[j]);
}
for (int j=lin[1].l; j<=lin[1].r; ++j) {
i1=p[j].i, j1=p[j].j;
//cout<<"ij: "<<i1<<' '<<j1<<endl;
maxn[0] = max(maxn[0], dp[j]+i1);
maxn[1] = max(maxn[1], dp[j]-i1);
maxn[2] = max(maxn[2], dp[j]+j1);
maxn[3] = max(maxn[3], dp[j]-j1);
}
//cout<<"ans: "<<ans<<endl;
//cout<<"maxn: "; for (int i=0; i<4; ++i) cout<<maxn[i]<<' '; cout<<endl;
for (int i=2; i<=lcnt; ++i) {
for (int j=lin[i].l; j<=lin[i].r; ++j) {
i1=p[j].i, j1=p[j].j;
dp[j] = p[j].b+max(max(max(maxn[0]-i1, maxn[1]+i1), maxn[2]-j1), maxn[3]+j1);
//cout<<"ans in "<<maxn[0]-i1<<' '<<maxn[1]+i1<<' '<<maxn[2]-j1<<' '<<maxn[3]+j1<<endl;
ans = max(ans, dp[j]);
}
for (int j=lin[i].l; j<=lin[i].r; ++j) {
i1=p[j].i, j1=p[j].j;
maxn[0] = max(maxn[0], dp[j]+i1);
maxn[1] = max(maxn[1], dp[j]-i1);
maxn[2] = max(maxn[2], dp[j]+j1);
maxn[3] = max(maxn[3], dp[j]-j1);
}
//cout<<"ans: "<<ans<<endl;
//cout<<"maxn: "; for (int i=0; i<4; ++i) cout<<maxn[i]<<' '; cout<<endl;
}
printf("%lld\n", ans);
}
}
signed main()
{
#ifdef DEBUG
freopen("1.in", "r", stdin);
#endif
n=read(); m=read();
task::solve();
return 0;
}
题解 biology的更多相关文章
- HDU 5590 ZYB's Biology 水题
ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- HDU1560 DNA sequence(IDA*)题解
DNA sequence Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- Biology(湖南集训)
题目大意:n个字符串,m个操作,可以插入字符串,也可以询问某T个字符串的最长后缀 题解:Trie+lca Trie树的插入与查询操作.把字符串反转就相当于求公共前缀. lca的深度就是公共前缀的长度. ...
- 遗传编程(GA,genetic programming)算法初探,以及用遗传编程自动生成符合题解的正则表达式的实践
1. 遗传编程简介 0x1:什么是遗传编程算法,和传统机器学习算法有什么区别 传统上,我们接触的机器学习算法,都是被设计为解决某一个某一类问题的确定性算法.对于这些机器学习算法来说,唯一的灵活性体现在 ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
随机推荐
- 第16章 pyinstaller库的使用
pyinstaller库概述 将.py源代码,转换成无需源代码的可执行文件 .py文件通过pyinstaller转换成windows.linux以及mac的可执行文件 pyinstaller库是第三方 ...
- WPF教程二:理解WPF的布局系统和常用的Panel布局
WPF的布局系统 了解元素的测量和排列方式是理解布局的第一步.在测量(measure)阶段容器遍历所有子元素,并询问子元素它们所期望的尺寸.在排列(arrange)阶段,容器在合适的位置放置子元素.理 ...
- java基础---数组的排序算法(3)
一.排序的基本概念 排序:将一个数据元素集合或序列重新排列成按一个数据元素某个数据项值有序的序列 稳定排序:排序前和排序后相同元素的位置关系与初始序列位置一致(针对重复元素来说,相对位置不变) 不稳定 ...
- 「AGC021E」Ball Eat Chameleons
「AGC021E」Ball Eat Chameleons 考虑如何判定一个合法的颜色序列. 不妨设颜色序列中有 \(R\) 个红球,\(B\) 个蓝球,所以有 \(R+B=k\). 考虑分情况讨论: ...
- python + pytest基本使用方法(参数化)
import pytestimport math#pytest 参数化#'base,exponent,expected'用来定义参数的名称.# 通过数组定义参数时,每一个元组都是一条测试用例使用的测试 ...
- C++第五十篇 -- 获取串口的描述信息
如何知道自己的电脑上有无串口呢? -- 手动 1. 查看电脑,看是否有串口器件(串口是一个九针的D型接口) 2. 在设备管理器上查看 乍一看,还以为是有两个串口,其实仔细看描述就知道,这是蓝牙虚拟串口 ...
- Table类
Interpreter类, class Interpreter: public CC_INTERP_ONLY(CppInterpreter) NOT_CC_INTERP(TemplateInterpr ...
- ifix中嵌入3d模型初探(一)
在ifix项目中插入3d模型,是当前工控上位机的一个发展趋势,故而我也来尝尝鲜.利用现有条件,初步打算完成一个工厂俯视3d全景. 基本思路:利用webbrowser+3dmax+three.js来嵌入 ...
- 微信开发者工具获取位置错误(定位到北京)---调用wx.getLocation不出现获取定位提示
微信开发者工具获取不到自己当前的位置可能是以下几个原因: 1.调用wx.getLocation方法之后需要在app.json中声明permission字段 { "pages": [ ...
- Pytorch Torchvision Transform
Torchvision.Transforms Transforms包含常用图像转换操作.可以使用Compose将它们链接在一起. 此外,还有torchvision.transforms.functio ...