Educational Codeforces Round 112 (Rated for Div. 2)-C. Coin Rows-题解
Posted Tisfy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 112 (Rated for Div. 2)-C. Coin Rows-题解相关的知识,希望对你有一定的参考价值。
目录
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 2 2 2 rows and m m m columns. The cell in the i i i-th row in the j j j-th column contains a i , j a_{i, j} ai,j coins in it.
Initially, both Alice and Bob are standing in a cell ( 1 , 1 ) (1, 1) (1,1). They are going to perform a sequence of moves to reach a cell ( 2 , m ) (2, m) (2,m).
The possible moves are:
First, Alice makes all her moves until she reaches ( 2 , m ) (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) (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 t t t ( 1 ≤ t ≤ 1 0 4 1 \\le t \\le 10^4 1≤t≤104) — the number of testcases.
Then the descriptions of t t t testcases follow.
The first line of the testcase contains a single integer m m m ( 1 ≤ m ≤ 1 0 5 1 \\le m \\le 10^5 1≤m≤105) — the number of columns of the matrix.
The i i i-th of the next 2 2 2 lines contain m m m integers a i , 1 , a i , 2 , … , a i , m a_{i,1}, a_{i,2}, \\dots, a_{i,m} ai,1,ai,2,…,ai,m ( 1 ≤ a i , j ≤ 1 0 4 1 \\le a_{i,j} \\le 10^4 1≤ai,j≤104) — the number of coins in the cell in the i i i-th row in the j j j-th column of the matrix.
The sum of m m m over all testcases doesn’t exceed 1 0 5 10^5 105.
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 × m 2\\times m 2×m的地图,每个方块儿有一定数量的金币
玩家只能向右或向下走,走过后会吃掉经过方块儿的所有金币。
Alice先走Bob后走。Alice想让Bob吃到尽可能少的金币,Bob想吃到尽可能多的金币。他们都非常聪明。问最终Bob会吃到多少的金币。
解题思路
不难发现玩家只能向下走一次,因为一共只有 2 2 2行。
关键就在于何时向下。
红色的是Alice,不难发现Bob要么吃绿色的部分,要么吃蓝色的部分。
所以我们就枚举Alice每一个可以向下的位置,对于此位置,Bob的得分是绿色部分和蓝色部分种总分最大的那块儿。
用前缀和可以轻松实现 O ( 1 ) O(1) O(1)复杂度计算水平方向连续几块儿的总分,总复杂度 O ( m ) 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;
}
原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/119272545
以上是关于Educational Codeforces Round 112 (Rated for Div. 2)-C. Coin Rows-题解的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 7 A
Educational Codeforces Round 7
Educational Codeforces Round 90
Educational Codeforces Round 33