Algorithm: Voting Voting

topic

Voting (Voting)

  • Time limit: C / C ++ 2 seconds and 4 seconds in other languages
  • Space limitations: C / C ++ 262144K, other languages ​​524288K
  • 64bit IO Format: %lld

Title Description

Beef and cattle sister in competing technologies team leader, so his colleagues conducted a secret ballot activity.
Taurus has a special ability, can vote than snooping N times during the voting process, the i-th pry beef can know the current ratio of the number of votes of his votes and cattle sister is n: m, but he could not pry the exact number of votes how many.
After completion of spy N times, Taurus want to know the premise of meeting all snoop results than the current number of votes he obtained the minimum number of cattle and sister is?
Alice and Bob are Engaged in A Competition for Tech Group Leader, for Which All colleagues now are Voting BETWEEN Them.
The Given that Alice IS Able to See The Voting Results for N Times. At The I-TH Time, SHE IS to know The votes SHE has GOT Against Bob apos in A ratio of n-: m, HOWEVER, SHE CAN Not See The Actual AMOUNT of votes each Time.
the After Seeing N Times, GIVEN that All The ratios SHE sees are correct, Alice Wants to know The minimal AMOUNT of votes that . sums up both hers and Bob's
input description:
input comprises a line N + 1.
Conduct a first positive integer N (1≤N≤1000)
Next N rows, each row two positive integers m, n (m, n≤1000) , to ensure the two prime numbers.
input contains \mathit N+1N+1 lines.
The 1st line is a positive integer N(1≤N≤1000)
Then N lines follow, each line containing two positive integers n,m
It is ensured that gcd(n_i, m_i) == 1gcd(ni ,mi)==1.

Output Description: The
output of the smallest possible number of votes and ensure that the answer does not exceed 10 ^ 18.
Output the minimal amount of votes that sums up both Alice's and Bob's, it is ensured that the answer does not exceed 10 ^ 18.

Example 1

输入
复制
3
2 3
1 1
3 2
输出
复制
10
说明
票数变化可能是:(2, 3) -> (3, 3) -> (6, 4)
所以最终的最小的票数之和为10
the change of amount of votes can be : (2,3) -> (3, 3) -> (6, 4),
so the answer is 10.

Example 2

输入
4
1 1
1 1
1 5
1 100
输出
101

Example 3

输入
5
3 10
48 17
31 199
231 23
3 2
输出
6930

Code

Analysis: calculated per minX x minimum, the minimum value of minY y.

  1. If (x >= minX && y >= minY)so direct assignmentminX = x; minY = y;
  2. Otherwise, ask for the largest proportion Math.ceil(((double)1.0 * minX) / x), Math.ceil(((double)1.0 * minY) / y)if there is a decimal into a. With x, y are obtained by multiplying the maximum magnification. Assignment minX, minY.
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
       
        int n = in.nextInt();
        
        long minX = 0;
        long minY = 0;
        
        for(int i = 0; i < n;i++) {
          int x = in.nextInt();
          int y = in.nextInt();
          
          //write your code
          if (x >= minX && y >= minY) {
              minX = x;
              minY = y;
            } else {
              double mulX = 1;
              double mulY = 1;
              if (x > 0) {
                mulX = Math.ceil(((double)1.0 * minX) / x);
              }
              if (y > 0) {
                mulY = Math.ceil(((double)1.0 * minY) / y);
              }
              long mulMax = (long) Math.max(mulX, mulY);
              minX = Math.max(minX, x * mulMax);
              minY = Math.max(minY, y * mulMax);
            }
        }

    
        long result = minX + minY;
        System.out.print(result);
        
    }
}

problem? ?

Case can be accessed through the above given, but some Case is not over, I do not know where the problem? Case does not provide, to know where the problem is, comments that help. Thank you
Here Insert Picture Description

reference

https://ac.nowcoder.com/acm/contest/3286/B

Published 127 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/zgpeace/article/details/103468086