Tozan and Gezan(x*y<a*b)

本文探讨了一个涉及两个玩家Tozan和Gezan的博弈问题。玩家的目标是在一系列操作后使分配给宠物Takahashi的糖果数量达到最优。问题要求找出在双方都采取最优策略的情况下,最终给予Takahashi的糖果总数。

E - Tozan and Gezan


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

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

  • 1≤N≤2×105
  • 0≤Ai,Bi≤109(1≤iN)
  • 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

题意:求满足条件x*y<a*b最多的组数,其中a,b已知。
题解:要满足<a*b,x,y中必定存在<sqrt(a*b)的数;
① 如果t2*t2==a*b,在满足a==b的条件下,因为是<a*b,所以t2*2—2,因为a,b被计算了两次;否则在t2*t2==a*b的各种情况中还存在a!=t2&&a!=b的情况,所以还要-1;
② 如果满足t2*t2为最后一组满足条件的数时,只需要-1,(重复计算了t2*t2);
③ 否则的话-2(即在分别为a和b的条件时的两种情况;

AC代码:
//#include    <bits/stdc++.h>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define N  500005
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define INF 0x3f3f3f3f
#define MOD 998244353
#define Mod 1e9 + 7
template<typename T> inline T max(T a,T b,T c){
    return max(a,max(b,c));
}
template<typename T> inline T min(T a,T b,T c){
    return min(a,min(b,c));
}
template<typename T> inline T max(T a,T b,T c,T d){
    return max(a,max(b,c,d));
}
template<typename T> inline T min(T a,T b,T c,T d){
    return min(a,min(b,c,d));
}
const int  dx[]={0,1,0,-1,0,1,-1,1,-1};
const int  dy[]={0,0,1,0,-1,1,-1,-1,1};
typedef long long ll;
using namespace std;
int main(){
   ll n,a,b;
   scanf("%lld",&n);
   for (int i=1;i<=n;i++){
    scanf("%lld%lld",&a,&b);
    ll t1=a*b;
    ll t2=sqrt(t1);
     if (t2*t2==t1){
         if (a==b) printf("%lld\n",t2*2-2);
         else printf("%lld\n",t2*2-3);
     }
     else if (t2*(t2+1)<t1) printf("%lld\n",t2*2-1);
     else printf("%lld\n",t2*2-2);
   }
   return 0;
}

 

转载于:https://www.cnblogs.com/lisijie/p/8746998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值