AtCoder Regular Contest 094 E - Tozan and Gezan (博弈论 2个和相同序列双方轮流减一得最后完全相同序列)

Problem Statement

You are given sequences A and B consisting of non-negative integers. The lengths of both A and B are N, and the sums of the elements in A and B are equal. The i-th element in A is Ai, and the i-th element in B is Bi.

Tozan and Gezan repeats the following sequence of operations:

  • If A and B are equal sequences, terminate the process.
  • Otherwise, first Tozan chooses a positive element in A and decrease it by 1.
  • Then, Gezan chooses a positive element in B and decrease it by 1.
  • Then, give one candy to Takahashi, their pet.

Tozan wants the number of candies given to Takahashi until the process is terminated to be as large as possible, while Gezan wants it to be as small as possible. Find the number of candies given to Takahashi when both of them perform the operations optimally.

Constraints

  • 1N2×105
  • 0Ai,Bi109(1iN)
  • The sums of the elements in A and B are equal.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A1 B1
:
AN BN

Output

Print the number of candies given to Takahashi when both Tozan and Gezan perform the operations optimally.


Sample Input 1

Copy
2
1 2
3 2

Sample Output 1

Copy
2

When both Tozan and Gezan perform the operations optimally, the process will proceed as follows:

  • Tozan decreases A1 by 1.
  • Gezan decreases B1 by 1.
  • One candy is given to Takahashi.
  • Tozan decreases A2 by 1.
  • Gezan decreases B1 by 1.
  • One candy is given to Takahashi.
  • As A and B are equal, the process is terminated.

Sample Input 2

Copy
3
8 3
0 1
4 8

Sample Output 2

Copy
9

Sample Input 3

Copy
1
1 1

Sample Output 3

Copy
0

题意:给出A B 2个序列,他们的序列总和相同,2个人T和G,双方轮流选择序列中一个正整数,进行减一,

T想尽可能的使步数大,G想尽可能的使步数小,计算使得2个序列完全相同的步数

当所有的Bi=Ai的时候输出0

G:为了使得步数尽可能的小,当Bi>Ai的时候只要减少Bi即可,当Bi

T:为了使得步数尽可能的大,当Bi>Ai的时候,Ai必定最后为0,当Bi

综上得:结果为sum-minb

#include
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int main()
{
	int t;
	cin>>t;
	ll sum=0,a,b,minb=INF,flag=0;
	while(t--){
        cin>>a>>b;
        sum+=b;
        if(bb)
            minb=b;
        if(b!=a)
            flag=1;
	}
 
	if(flag==1)
        cout<


你可能感兴趣的:(ACM_数字处理与数论,ACM_博弈论,Atcoder)