• C. Robot in a Hallway Educational Codeforces Round 133 (Rated for Div. 2)dp


    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
    
    1. #include
    2. using namespace std;
    3. const int N=200010;
    4. int a[4][N];
    5. int dp[4][N];
    6. int main()
    7. {
    8. int t,m,i,j;
    9. scanf("%d",&t);
    10. while(t--)
    11. {
    12. scanf("%d",&m);
    13. for(i=1;i<=2;i++)
    14. for(j=1;j<=m;j++)
    15. scanf("%d",&a[i][j]);
    16. a[1][1]=-1;
    17. dp[1][m+1]=dp[2][m+1]=1;
    18. for(j=m;j>=1;j--)
    19. for(i=1;i<=2;i++)
    20. dp[i][j]=max(max(a[i][j]+2*(m-j+1),a[3-i][j]+1),dp[i][j+1]+1);
    21. int ans=5e9;
    22. int num=0;
    23. for(j=1;j<=m;j++)
    24. {
    25. int k=3-(j%2+1);
    26. ans=min(ans,max(num,dp[k][j]));
    27. num=max(num,a[k][j]+2*(m-j+1));
    28. num=max(num,a[3-k][j]+2*(m-j+1)-1);
    29. }
    30. cout<
    31. }
    32. return 0;
    33. }

  • 相关阅读:
    什么是动画效果?什么是过渡效果?
    [请回答C++] C++11&&类默认函数&&final&&override&&可变参数模板&&emplace
    视频通话中的Camera操作
    containerd拉取私库镜像失败(kubelet)
    Java学习----Set接口
    基于遗传算法和布谷鸟搜索优化算法的特征选择(Matlab代码实现)
    Quartz,更优雅地管理你的定时任务
    Golang 协程、主线程
    AI:74-基于深度学习的宠物品种识别
    第五章《类的继承》第4节:属性的屏蔽
  • 原文地址:https://blog.csdn.net/weixin_62848089/article/details/126184218