C. Coin Rows

题目:https://codeforces.com/contest/1555/problem/C

Alice and Bob are playing a game on a matrix, consisting of 2 rows and m columns. The cell in the i-th row in the j-th column contains ai,j coins in it.

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

The possible moves are:

Move right — from some cell (x,y) to (x,y+1);
Move down — from some cell (x,y) to (x+1,y).
First, Alice makes all her moves until she reaches (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) 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 (1≤t≤104) — the number of testcases.

Then the descriptions of t testcases follow.

The first line of the testcase contains a single integer m (1≤m≤105) — the number of columns of the matrix.

The i-th of the next 2 lines contain m integers ai,1,ai,2,…,ai,m (1≤ai,j≤104) — the number of coins in the cell in the i-th row in the j-th column of the matrix.

The sum of m over all testcases doesn’t exceed 105.

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

Example
input
3
3
1 3 7
3 5 1
3
1 3 9
3 5 1
1
4
7
output
7
8
0

官方的题解竟然理解了几十分钟才懂。。。以下是官方答案

证明:

First, observe that each of the players has only m options for their path — which column to go down in.

Let’s consider a Bob’s response to a strategy chosen by Alice. The easiest way to approach that is to look at the picture of the Alice’s path.

在这里插入图片描述

The path clearly separates the field into two independent pieces — suffix of the first row and the prefix of the second row. Bob can’t grab the coins from both of them at once. However, he can grab either of them fully. So the optimal path for him will be one of these two options.

You can precalculate some prefix sums and become able to get the Bob’s score given the Alice’s path. Alice has m possibly paths, so you can iterate over them and choose the minimum answer.

However, prefix sums are not required, since you can quickly recalculate both needed sums while iterating over the Alice’s column to go down in.

Overall complexity: O(m) per testcase.

AC代码:

#include <iostream>
#include <vector>
using namespace std;

const int inf = 1e9;

void solve() {

	int n;cin >> n;
	
	vector<vector<int>> a(2,vector<int>(n));
	
	for(int i = 0;i<2;i++)
		for(int j = 0;j<n;j++) cin >> a[i][j];
	
	int ans = inf;
	int sum1 = 0,sum2 = 0;
	for(int i = 0;i<n;i++)  sum1 += a[0][i];
	
	for(int int i = 0;i<n;i++){
		
		sum1 -= a[0][i];	//这是第一部分的 
		ans = min(ans,max(sum1,sum2));//先取两个部分中的最大值,再取最小值,因为Alice先走 
		sum2 += a[1][i];	//这是第二部分的 
	}
	
	cout << ans << endl;
	
}

int main() {

	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t; cin >> t;
	for (int i = 1; i <= t; i++) solve();

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值