本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@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 rows and columns. The cell in the -th row in the -th column contains coins in it.
Initially, both Alice and Bob are standing in a cell . They are going to perform a sequence of moves to reach a cell .
The possible moves are:
First, Alice makes all her moves until she reaches . 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 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 () — the number of testcases.
Then the descriptions of testcases follow.
The first line of the testcase contains a single integer () — the number of columns of the matrix.
The -th of the next lines contain integers () — the number of coins in the cell in the -th row in the -th column of the matrix.
The sum of over all testcases doesn't exceed .
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.
题目大意
的地图,每个方块儿有一定数量的金币
玩家只能向右或向下走,走过后会吃掉经过方块儿的所有金币。
Alice先走Bob后走。Alice想让Bob吃到尽可能少的金币,Bob想吃到尽可能多的金币。他们都非常聪明。问最终Bob会吃到多少的金币。
解题思路
不难发现玩家只能向下走一次,因为一共只有行。
关键就在于何时向下。
红色的是Alice,不难发现Bob要么吃绿色的部分,要么吃蓝色的部分。
所以我们就枚举Alice每一个可以向下的位置,对于此位置,Bob的得分是绿色部分和蓝色部分种总分最大的那块儿。
用前缀和可以轻松实现复杂度计算水平方向连续几块儿的总分,总复杂度。
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…