题目描述
Being a responsible young adult, you have decided to start planning for retirement. Doing some back-of-the-envelope calculations, you figured out you need at least M euros to retire comfortably.
You are currently broke, but fortunately a generous gazillionaire friend has offered to lend you an arbitrary amount of money (as much as you need), without interest, to invest in the stock market. After making some profits you will then return the original sum to your friend, leaving you with the remainder.
Available to you are n investment opportunities, the i-th of which costs ci euros. You also used your computer science skills to predict that the i-th investment will earn you pi euros per day. What is the minimum number of days you need before you can pay back your friend and retire? You can only invest once in each investment opportunity, but you can invest in as many different investment opportunities as you like.
For example, consider the first sample. If you buy only the second investment (which costs 15 euros) you will earn p2 = 10 euros per day. After two days you will have earned 20 euros, exactly enough to pay off your friend (from whom you borrowed 15 euros) and retire with the remaining profits (5 euros). There is no way to make a net amount of 5 euros in a single day, so two days is the fastest possible.
输入
• The first line contains the number of investment options 1 ≤ n ≤ 105 and the minimum amount of money you need to retire 1 ≤ M ≤ 109.
• Then, n lines follow. Each line i has two integers: the daily profits of this investment 1 ≤ pi ≤ 109 and its initial cost 1 ≤ ci ≤ 109.
输出
Print the minimum number of days needed to recoup your investments and retire with at least M euros, if you follow an optimal investment strategy.
样例输入
2 5
4 10
10 15
样例输出
2
开始想的是,把赚回本金需要的天数从小到大排序,这样前面的投资与否就不影响后面的选择。
因为如果转本金需要天数大的都已经产生盈利 那么小的肯定也是盈利了
结果wa了也不知道为什么
后来想到再在这个排序的基础上对答案进行二分
Tips:r写0x3f3f3f3f会wa 往大点写
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
struct node {
int p,c;
double x;
}a[maxn];
int n,m;
bool cmp(node a,node b){
if (a.x==b.x) return a.p>b.p;
return a.x<b.x;
}
int main (){
cin>>n>>m;
for (int i=1;i<=n;++i) {
scanf("%d%d",&a[i].p,&a[i].c);
a[i].x = 1.0*a[i].c/a[i].p;
}
sort(a+1,a+1+n,cmp);
ll l=1,r=50000000000,mid;
while (l<=r){
mid = (l + r) >> 1;
ll sum = 0;
for (int i=1;i<=n;++i){
sum += (a[i].p * mid - a[i].c);
if (sum>=m) break;
}
if (sum>=m) r=mid-1;
else l=mid+1;
}
cout<<l<<endl;
return 0;
}