算法:投票Voting

题目

投票(Voting)

  • 时间限制:C/C++ 2秒,其他语言4秒
  • 空间限制:C/C++ 262144K,其他语言524288K
  • 64bit IO Format: %lld

题目描述

牛牛和牛妹在竞争技术组组长,于是同事们进行了一次不记名投票活动。
牛牛有特别的能力,可以在投票过程中对投票结果比窥探N次, 第i次窥探牛牛能够知道当前他的票数和牛妹的票数之比是n : m ,但是他没法窥探到确切的票数是多少。
在窥探完N次之后,牛牛想知道在满足所有窥探结果比的前提下,当前他和牛妹所得的票数之和最小为多少?
Alice and Bob are engaged in a competition for tech group leader, for which all colleagues are voting between them.
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’s in a ratio of n:m,however, she cannot see the actual amount of votes each time.
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.
输入描述:
输入包括N+1行。
第一行为一个正整数N(1≤N≤1000)
接下来N行,每行两个正整数m, n ( m, n≤1000),保证两个数互质。
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.

输出描述:
输出最小可能的票数之和,保证答案不会超过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 .

示例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.

示例2

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

示例3

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

代码实现

分析:每次求得x的最小值minX,y的最小值minY。

  1. 如果(x >= minX && y >= minY) 那么直接赋值minX = x; minY = y;
  2. 否则就求最大的比例Math.ceil(((double)1.0 * minX) / x)Math.ceil(((double)1.0 * minY) / y), 如果有小数则进一。用x, y都乘以求得的最大倍数。赋值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);
        
    }
}

问题??

上面的给出Case都能通过,但是有的Case没有过去,完全不知道问题在哪里?Case也不提供,有知道问题在哪里的,帮忙评论指出。谢谢
在这里插入图片描述

参考

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

发布了127 篇原创文章 · 获赞 12 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/zgpeace/article/details/103468086
今日推荐