Codeforces Round #674 (Div. 3) E. Rock, Paper, Scissors

E. Rock, Paper, Scissors
Description

Alice and Bob have decided to play the game “Rock, Paper, Scissors”.

The game consists of several rounds, each round is independent of each other. In each round, both players show one of the following things at the same time: rock, paper or scissors. If both players showed the same things then the round outcome is a draw. Otherwise, the following rules applied:

if one player showed rock and the other one showed scissors, then the player who showed rock is considered the winner and the other one is considered the loser;
if one player showed scissors and the other one showed paper, then the player who showed scissors is considered the winner and the other one is considered the loser;
if one player showed paper and the other one showed rock, then the player who showed paper is considered the winner and the other one is considered the loser.
Alice and Bob decided to play exactly n rounds of the game described above. Alice decided to show rock a1 times, show scissors a2 times and show paper a3 times. Bob decided to show rock b1 times, show scissors b2 times and show paper b3 times. Though, both Alice and Bob did not choose the sequence in which they show things. It is guaranteed that a1+a2+a3=n and b1+b2+b3=n.

Your task is to find two numbers:

the minimum number of round Alice can win;
the maximum number of rounds Alice can win.

Input

The first line of the input contains one integer n (1≤n≤109) — the number of rounds.

The second line of the input contains three integers a1,a2,a3 (0≤ai≤n) — the number of times Alice will show rock, scissors and paper, respectively. It is guaranteed that a1+a2+a3=n.

The third line of the input contains three integers b1,b2,b3 (0≤bj≤n) — the number of times Bob will show rock, scissors and paper, respectively. It is guaranteed that b1+b2+b3=n.

Output

Print two integers: the minimum and the maximum number of rounds Alice can win.

Examples
input
2
0 1 1
1 1 0
output
0 1
input
15
5 5 5
5 5 5
output
0 15
input
3
0 0 3
3 0 0
output
3 3
input
686
479 178 29
11 145 530
outputCopy
22 334
input
319
10 53 256
182 103 34
outputCopy
119 226
Note

In the first example, Alice will not win any rounds if she shows scissors and then paper and Bob shows rock and then scissors. In the best outcome, Alice will win one round if she shows paper and then scissors, and Bob shows rock and then scissors.

In the second example, Alice will not win any rounds if Bob shows the same things as Alice each round.

In the third example, Alice always shows paper and Bob always shows rock so Alice will win all three rounds anyway.

题意: 石头、剪刀、布的游戏,输出Alice最少能赢几回合,最多能赢几回合。

题解:

  1. Alice最多赢的次数,就是Alice能赢则赢,即 m i n ( a [ 1 ] , b [ 1 ] ) + m i n ( a [ 2 ] , b [ 2 ] ) + m i n ( a [ 3 ] , b [ 3 ] ) min(a[1],b[1])+min(a[2],b[2])+min(a[3],b[3]) min(a[1],b[1])+min(a[2],b[2])+min(a[3],b[3])
  2. Alice最少能赢的次数,即Alice能输则输,不能输则平。也就是让a[1]尽可能多的和b[1]抵消,输给b[3];让a[2]尽可能多的和b[2]抵消,输给b[1];让a[3]尽可能多的和b[3]抵消,输给b[2]。全部抵消后最优的状态是0,或者是Alice和Bob各自剩下一种。全为0就是Alice最少一次都赢不到,如果她两抵消完之后各自还有剩余,则剩下的一定是Alice赢(因为抵消的都是对赢没有贡献的,无法抵消肯定是有贡献,也就是alice赢)。

还有一个小问题,alice如果有剩余一定是一种吗,答案是肯定的,因为如果alice剩下了两种:a2个剪刀和a3个布,bob剩下了一种:b1个石头,(a2 + a3 == b1就不用解释了8)此时还可以让alice的剪刀和石头相抵消,也就是某人剩下两种时根本不是最终状态,还可以继续抵消。因此有剩余情况下的最终状态肯定是每人一种。
下面来讨论一下抵消后alice有剩余的情况:

(1)如果剩下a1,那对应剩下的就是b2,a1 == b2。
整个过程让b2尽量消失,所以a1、b2的剩余量均为 b2 - a2 - a3。(这里也可以考虑成让a1尽量地消失,a1、b2的剩余量为a1 - b1 - b3)

(2)如果剩下a2,那对应剩下的就是b3,a2 == b3。

整个过程让b3尽量消失,所以a2、b3的剩余量均为 b3 - a1 - a3。(这里也可以考虑成让a2尽量地消失,a2、b3的剩余量为a2 - b1 - b2)

(3)如果剩下a3,那对应剩下的就是b1,a3 == b1。

整个过程让b1尽量消失,所以a3、b1的剩余量均为 b1 - a1 - a2。(这里也可以考虑成让a3尽量地消失,a3、b1的剩余量为a3 - b2 - b3)

现在来看一下这三个数(b2 - a2 - a3)、(b3 - a1 - a3)、(b1 - a1 - a2)这三个数中最多只有一个正数,证明如下:

假设 b2 - a2 - a3 > 0,即 b2 > a2 + a3

因为 b1 + b2 + b3 == a1 + a2 + a3 == n

所以 b1 + b3 < a1

所以 b1 < a1 + a2 且 b3 < a1 + a3

所以上面三个数中最多只有一个正数。

所以最终的答案就是从(b2 - a2 - a3)、(b3 - a1 - a3)、(b1 - a1 - a2)中选一个最大值就可以了。

如果上面考虑让a1、a2、a3尽量地消失就是从(a1 - b1 - b3)、(a2 - b1 - b2)、(a3 - b2 - b3)中选一个最大值,答案是一样的,因为Alice和Bob剩下的数肯定相同。

c++ AC 代码

#include <bits/stdc++.h>
using namespace std;

int a[3], b[3];

int main()
{
	int n, max_ans = 0, min_ans = 0;
	scanf("%d", &n);
	for (int i = 0; i < 3; i++)
		scanf("%d", a + i);
	for (int i = 0; i < 3; i++)
		scanf("%d", b + i);
	max_ans = min(a[0], b[1]) + min(a[1], b[2]) + min(a[2], b[0]);
	min_ans = max({0, b[1] - a[2] - a[1], b[2] - a[0] - a[2], b[0] - a[1] - a[0]});
	printf("%d %d", min_ans, max_ans);
	// system("pause");
	return 0;
}

转载自:https://blog.csdn.net/weixin_43871207/article/details/108876134

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值