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

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;
}

 

posted @ 2018-04-08 19:55  你的女孩居居  阅读(523)  评论(0编辑  收藏  举报