Educational Codeforces Round 112 (Rated for Div. 2)-C. Coin Rows-题解

93 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

@TOC

Educational Codeforces Round 112 (Rated for Div. 2)-C. Coin Rows

传送门 Time Limit: 2 seconds Memory Limit: 256 megabytes

Problem Description

Alice and Bob are playing a game on a matrix, consisting of 22 rows and mm columns. The cell in the ii-th row in the jj-th column contains ai,ja_{i, j} coins in it.

Initially, both Alice and Bob are standing in a cell (1,1)(1, 1). They are going to perform a sequence of moves to reach a cell (2,m)(2, m).

The possible moves are:

First, Alice makes all her moves until she reaches (2,m)(2, m). She collects the coins in all cells she visit (including the starting cell).

When Alice finishes, Bob starts his journey. He also performs the moves to reach (2,m)(2, m) and collects the coins in all cells that he visited, but Alice didn't.

The score of the game is the total number of coins Bob collects.

Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally?

Input

The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of testcases.

Then the descriptions of tt testcases follow.

The first line of the testcase contains a single integer mm (1m1051 \le m \le 10^5) — the number of columns of the matrix.

The ii-th of the next 22 lines contain mm integers ai,1,ai,2,,ai,ma_{i,1}, a_{i,2}, \dots, a_{i,m} (1ai,j1041 \le a_{i,j} \le 10^4) — the number of coins in the cell in the ii-th row in the jj-th column of the matrix.

The sum of mm over all testcases doesn't exceed 10510^5.

Output

For each testcase print a single integer — the score of the game if both players play optimally.

Sample Input

3
3
1 3 7
3 5 1
3
1 3 9
3 5 1
1
4
7

Sample Onput

7
8
0

Note

The paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue.


题目大意

2×m2\times m的地图,每个方块儿有一定数量的金币

玩家只能向右或向下走,走过后会吃掉经过方块儿的所有金币。

Alice先走Bob后走。Alice想让Bob吃到尽可能少的金币,Bob想吃到尽可能多的金币。他们都非常聪明。问最终Bob会吃到多少的金币。

解题思路

不难发现玩家只能向下走一次,因为一共只有22行。

关键就在于何时向下。

红色的是Alice,不难发现Bob要么吃绿色的部分,要么吃蓝色的部分。

所以我们就枚举Alice每一个可以向下的位置,对于此位置,Bob的得分是绿色部分和蓝色部分种总分最大的那块儿。

用前缀和可以轻松实现O(1)O(1)复杂度计算水平方向连续几块儿的总分,总复杂度O(m)O(m)

QianZui[i][j]表示第i行前j个块儿的总分,一般使用前缀和的话输入数据要从1开始。初始值QianZui[0][0]=QianZui[1][0]=0,递推公式QianZui[i][j]=QianZui[i-1][j]+a[i][j]。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int a[2][100010];
int qianZui[2][100010];
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cd(n); //scanf
        qianZui[0][0]=qianZui[1][0]=0; // 初始化
        fi(i,1,n+1)cd(a[0][i]),qianZui[0][i]=qianZui[0][i-1]+a[0][i]; // scanf,累加
        fi(i,1,n+1)cd(a[1][i]),qianZui[1][i]=qianZui[1][i-1]+a[1][i];
        int m=INT_MAX;
        for(int i=1;i<=n;i++) // 从第i列下来
        {
            int thisM=max(qianZui[1][i-1], qianZui[0][n]-qianZui[0][i]); // Bob的得分是绿色和蓝色的最大值
            m=min(m, thisM); // Alice让Bob的得分是Bob所有可以的得分中最低的分数。
        }
        cout<<m<<endl; // 输出
    }
    return 0;
}

同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…