ABS(AtCoder-3671)

本文探讨了一个游戏中的得分策略,两名玩家交替从牌堆顶部抽取任意数量的牌,并以最后一张牌替换手中的牌,目标是最小化或最大化两人手中牌的差值。文章详细解析了最优策略,即先手玩家应取a[n]或a[n-1]以最大化差值,后手玩家则取不大于a[n-1]的牌以最小化差值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is ai.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.
The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

  • All input values are integers.
  • 1≤N≤2000
  • 1≤Z,W,ai≤109

Input

Input is given from Standard Input in the following format:

N Z W
a1 a2 … aN

Output

Print the score.

Example

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900
If X draws two cards first, Y will draw the last card, and the score will be |1000−100|=900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900
If X draws all the cards first, the score will be |1000−100|=900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999

题意:开始时两人手上分半有 z、w 两张牌,有 n 张牌再桌面上,每轮取任意数目的牌,将手上的牌替换成取得牌最下面的那张,然后将剩余的牌扔掉,双方轮流操作直到 n 张牌用完,最后的结果是两人手上的牌的差值 k,先手令 k 尽量大,后手令 k 尽量少,求最后的结果

思路:

由于先手要最大化差值,因此开局他可以取 a[n-1] 剩下 a[n] 或者直接取 a[n] 直接结束游戏,如果先手不这样做,他取得的点数必须大于等于 a[n-1],否则后手会直接拿掉 a[n],但这样先手违背了他最大化差值的任务

同理,后手取小于等于 a[n-1] 的牌,否则会违背他最小化差值的任务,因此最终一定是分别拿到 a[n] 与 a[n-1],故答案是 max(a[n]-a[n-1],a[n]-w)

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int main() {
    int n,z,w;
    scanf("%d%d%d",&n,&z,&w);
    int maxx=-INF,minn=INF;
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);

    int res=0;
    if(n==1)
        res=abs(a[n]-w);
    else
        res=max(abs(a[n-1]-a[n]),abs(a[n]-w));
    printf("%d\n",res);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值