dp问题

There is a grid, consisting of 22 rows and mm columns. The rows are numbered from 11 to 22 from top to bottom. The columns are numbered from 11 to mm from left to right.

The robot starts in a cell (1,1)(1,1). In one second, it can perform either of two actions:

  • move into a cell adjacent by a side: up, right, down or left;
  • remain in the same cell.

The robot is not allowed to move outside the grid.

Initially, all cells, except for the cell (1,1)(1,1), are locked. Each cell (i,j)(i,j) contains a value ai,jai,j — the moment that this cell gets unlocked. The robot can only move into a cell (i,j)(i,j) if at least ai,jai,j seconds have passed before the move.

The robot should visit all cells without entering any cell twice or more (cell (1,1)(1,1) is considered entered at the start). It can finish in any cell.

What is the fastest the robot can achieve that?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains a single integer mm (2≤m≤2⋅1052≤m≤2⋅105) — the number of columns of the grid.

The ii-th of the next 22 lines contains mm integers ai,1,ai,2,…,ai,mai,1,ai,2,…,ai,m (0≤ai,j≤1090≤ai,j≤109) — the moment of time each cell gets unlocked. a1,1=0a1,1=0. If ai,j=0ai,j=0, then cell (i,j)(i,j) is unlocked from the start.

The sum of mm over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.

Example

input

Copy

4
3
0 0 1
4 3 2
5
0 4 8 12 16
2 6 10 14 18
4
0 10 10 10
10 10 10 10
2
0 0
0 0

output

Copy

5
19
17
3
#include <bits/stdc++.h>
using namespace std;
const int N=200010;
int a[4][N];
int dp[4][N];
int main()
{int t,m,i,j;scanf("%d",&t);while(t--){scanf("%d",&m);for(i=1;i<=2;i++)for(j=1;j<=m;j++)scanf("%d",&a[i][j]);a[1][1]=-1;dp[1][m+1]=dp[2][m+1]=1;for(j=m;j>=1;j--)for(i=1;i<=2;i++)dp[i][j]=max(max(a[i][j]+2*(m-j+1),a[3-i][j]+1),dp[i][j+1]+1);int ans=5e9;int num=0;for(j=1;j<=m;j++){int k=3-(j%2+1);ans=min(ans,max(num,dp[k][j]));num=max(num,a[k][j]+2*(m-j+1));num=max(num,a[3-k][j]+2*(m-j+1)-1);}cout<<ans<<endl;}return 0;
}

C. Robot in a Hallway Educational Codeforces Round 133 (Rated for Div. 2)dp相关推荐

  1. Educational Codeforces Round 133 (Rated for Div. 2)(CD题解)

    Educational Codeforces Round 133 (Rated for Div. 2)CD题解 过AB补CD C. Robot in a Hallway 题意 题意:现有 2∗m 的方 ...

  2. Educational Codeforces Round 133 (Rated for Div. 2) 题解 CD

    D: 这是一道好dp(对我来说) 我做的时候有想过正解那个方式当考虑到可能有重复就没有深入思考了 假设dp[i][j]代表第i次j的方案数 此时我们可以发现dp[i][j]=sum(dp[i-1][j ...

  3. 【细节很多的dp】Educational Codeforces Round 133 (Rated for Div. 2) C. Robot in a Hallway

    参考题解 题意: 有一个 2 2 2 行, m m m 列的方格,初始在 ( 1 , 1 ) (1,1) (1,1),每个格子有一个开放时间,开放时间后才能到达,每个格子只能被到达一次,你可以任意选择 ...

  4. Educational Codeforces Round 133 (Rated for Div. 2) D题

    题目链接:Problem - D - Codeforces 一道非常经典的完全背包求方案数题: 首先先写好我们的状态转移方程,那什么代表体积,什么代表物品数目呢: 其实很清晰k, k + 1 ... ...

  5. Educational Codeforces Round 133 (Rated for Div. 2)

    本来也没啥的这A题也不改花多久时间 大家都在用暴力我来给出O(1)的解法 确实不容易写出这的时候已经崩溃了放上一张图片共参考 说我菜吧确实- 呃..... 再讲解也下吧: 题目就是在一个数轴上有一个小 ...

  6. Educational Codeforces Round 100 (Rated for Div. 2)

    文章目录 Educational Codeforces Round 100 (Rated for Div. 2) A. Dungeon B. Find The Array C. Busy Robot ...

  7. Educational Codeforces Round 114 (Rated for Div. 2) (A ~ F)全题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...

  8. Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...

  9. Educational Codeforces Round 37 (Rated for Div. 2) 1

    Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...

最新文章

  1. alert,confirm和prompt
  2. Python-OS平台编程
  3. Docker Toolbox:Docker Toolbox的简介、安装、使用方法之详细攻略
  4. win10专业版占多少空间_win10技巧:使用win10轻松自动恢复系统!
  5. wxWidgets:wxIntProperty类用法
  6. NEFU394 素数价值
  7. uni-app实现上拉加载更多
  8. Java中的Google ClientLogin实用程序
  9. Survival analysis
  10. 从零开始学习mitmproxy源码阅读
  11. EBS R12.2 创建应用层的启动和关闭脚本
  12. 现有Android项目中集成Flutter/Flutter混合开发实战(一)
  13. KnockOutlook:针对Outlook的红队安全研究工具
  14. Vue 快速原型开发
  15. Rstudio MarkDown使用教程
  16. android 简单的闪屏页,闪屏页的实现简单原理
  17. 个人总结出来的git仓库迁移方案
  18. ZYThumbnailTableView类似于小型阅读器
  19. cordova在使用jqmobile中出现的问题(使用$(window).on与window.addEventListener)
  20. vue脚手架实现留言板功能

热门文章

  1. 期刊论文重复率多少算合格?
  2. Android Studio 模拟器无法启动问题(x86 emulation currently requires hardware acceleration。。。)
  3. 奔跑吧机器人发货_中国最大智能机器人仓库启用 天猫送货再提速上午下单下午到...
  4. Java爬虫Jsoup简易使用
  5. rpm打包遇到的问题记录
  6. 【个人博客】利用Hexo快速建立自己的博客网站
  7. ucd-snmp agent
  8. 组装电脑一些选购常识
  9. 时间序列预测之ARMA、ARIMA序列及季节性序列matlab实现
  10. vue v-html 富文本图片附件前图标显示问题