题目链接:Problem - 1555C - Codeforces
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 thej-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?
题意:Alice和bob两个人都从1,1到2, m, a先走1,1的值也是a的,b后走,他们只能向右和向下,因为只有两行,所以a, b只有一次向下走的机会,所以我们只要遍历n列,找出第一行i-n的值和第二行0 - i的值求max在求min就好,如果后面的求值也跑暴力的就相当于O(t * n * n),所以需要用到前缀和将复杂度将为O(t*n)
#include<bits/stdc++.h>
using namespace std;
int arr[5][100005];
long long a[100005];
long long b[100005];
int main(){
ios::sync_with_stdio(false);
int t;
int n;
cin >> t;
while(t--){
cin >> n;
for(int i = 0; i < 2; i++){
for(int l = 0; l < n; l++){
cin >> arr[i][l];
}
}
for(int i = 0; i < n; i++){
if(i == 0){
a[i] = arr[0][i];
b[i] = arr[1][i];
}else{
a[i] = a[i - 1] + arr[0][i];
b[i] = b[i - 1] + arr[1][i];
}
}
long long sum = 100000000005;
for(int i = 0; i < n; i++){
long long ans = 0;
long long cnt = 0;
ans = a[n - 1] - a[i];
cnt = b[i] - arr[1][i];
sum = min(sum, max(ans, cnt));
}
cout << sum << endl;
}
return 0;
}