Sicily 11159. Year Award

11159. Year Award

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB , Special Judge

Description

Melita has just returned from the annual pig slaughter. Don't worry, this is a regular thing in Croatia. The best part was the abundance of food! There was everything, starting from good spicy sausages, ham, black pudding, up to teewurst, top quality bacon and ?varci, all with warm white bread and butter. After these appetizers, it was the perfect time to whip up a deep pot full of sarma (Melita ate twentyish of them) as well as a large platter of fine roast pork, so soft that it almost melts in your mouth. They watered all of this down with copious gulps of the best dry white wine that made them even hungrier.

 
Butcher Bajs kept his award-winning ham for the very end. N people attented the annual pig slaughter, labeled with numbers from 1 to N. These people already ate a lot of meat: the kth person ate A[k] kilograms of meat so far. Bajs will distribute his ham to the people in the ratio B[1] : B[2] : … : B[N], exactly in that order, but he doesn't know the total amount (number of kilos) of ham which he will be distributing yet.
 
At the end of the slaughter, the Man of the Year will be declared. A ranking list is made according to the total kilos of meat eaten. Bajs impacts directly on this list by choosing the amount of ham to distribute. Although Bajs has been offered bribes many times, he refused each time, saying that he was an honest man who would not hurt a fly.
 
Bajs cares about order, because he's a nice gentleman, and wants to have the order of people in the exact form of 1, 2, 3, ..., N, respectively from the one who ate the most kilos of meat to those who ate less, allowing ties between participants. Help Bajs select the total amount of ham that he will distribute (in the ratio mentioned before) to achieve his intention.
 

Input

The first line of input contains an integer N (2 ≤ N ≤ 1000), the number of competitors for the Man of the Year award.

Each of the following k lines contains integers A[k] i B[k], as mentioned in the text (0 ≤ A[k], B[k] ≤ 10^6). At least one of the numbers B[k] will not be equal to 0.

Output

The first and only line of output must contain -1 if the required order cannot be achieved. Otherwise, output the required amount of ham in kilos, a real number (rounded up to 12 decimal places) between 0 and 10^7 (inclusive). If there are multiple possible solutions, output any.

Sample Input

样例1:
3
7 1
3 2
10 0
样例2:
3
2 1
4 0
0 3
样例3:
5
15 4
6 7
12 5
9 6
1 7

Sample Output

样例1:
10.5
样例2:
-1
样例3:
87

Hint

Clarification of the first example: 10.5 kilos of ham is distributed in the ratio 1 : 2 : 0, which gives us 3.5, 7 and 0 kilos of ham, respectively. If we add this to the already eaten amount of meat, we conclude that the participants ate 10.5, 10 and 10 kilos in total, which is a valid order.

Problem Source

2014年每周一赛第五场

题意就是求出一个x,使得a[i] + (b[i] / sum) * x >= a[i + 1] + (b[i + 1] / sum) * x,其中i从0到n-1,sum是所有b[i]的和,化简公式可以发现对于i从0到n-1,每个上述不等式都能得到x的一个范围,对于每个范围取交集即可。

这题测试样例比较坑,怒交20次才得出下面正确的- -。

#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
using namespace std;

#define INF 2000000000

int main() {

    //std::ios::sync_with_stdio(false);

    int n;

    cin >> n;
    
    vector<int> a(n);
    vector<int> b(n);
    long long int sum = 0;

    for (int i = 0; i < n; i++) {
        
        cin >> a[i] >> b[i];

        sum += b[i];

    }

    double up = 10000000.0, down = 0;
    bool impossible = false;

    for (int i = 0; i < n - 1; i++) {

        if (b[i + 1] - b[i] > 0) {

            double newUp = double(sum * (long long)(a[i] - a[i + 1])) / (double)(b[i + 1] - b[i]);

            if (newUp < up) up = newUp;

        } else if (b[i + 1] - b[i] < 0) {

            double newDown = double(sum * (long long)(a[i] - a[i + 1])) / (double)(b[i + 1] - b[i]);

            if (newDown > down) down = newDown;

        } else if (a[i] < a[i + 1]) {

            cout << -1 << endl;
            return 0;

        }

    }

    if (down <= up)
        printf("%lf\n", (up + down) / 2);
    else
        cout << -1 << endl;

    //getchar();
    //getchar();
    
    return 0;
}                   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值