?Kattis - financialplanning?
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?MM?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 profit you will then return the original sum to your friend, leaving you with the remainder.
Available to you are?nn?investment opportunities, the?ii-th of which costs?c_ ici??euros. You also used your computer science skills to predict that the?ii-th investment will earn you?p_ ipi??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?1515?euros) you will earn?p_2 = 10p2?=10?euros per day. After two days you will have earned?2020?euros, exactly enough to pay off your friend (from whom you borrowed?1515?euros) and retire with the remaining profit (55?euros). There is no way to make a net amount of?55?euros in a single day, so two days is the fastest possible.
Input
-
The first line contains the number of investment options?1 \leq n \leq 10^51≤n≤105?and the minimum amount of money you need to retire?1 \leq M \leq 10^91≤M≤109. -
Then,?nn?lines follow. Each line?ii?has two integers: the daily profit of this investment?{1 \leq p_ i \leq 10^9}1≤pi?≤109?and its initial cost?1 \leq c_ i \leq 10^91≤ci?≤109.
Output
Print the minimum number of days needed to recoup your investments and retire with at least?MM?euros, if you follow an optimal investment strategy.
Sample 1
Inputcopy | Outputcopy |
---|
2 5
4 10
10 15
| 2
|
Sample 2
Inputcopy | Outputcopy |
---|
4 10
1 8
3 12
4 17
10 100
| 6
|
Sample 3
Inputcopy | Outputcopy |
---|
3 5
4 1
9 10
6 3
| 1 |
?这个题比赛的时候队友和我都理解错题意了,浪费了不少时间,然后补题的时候想了想,每个股票都有一个最短的回血时间,我们需要的就是回血时间最短的股票,排序后枚举去取最小值;
ac代码:
#include <bits/stdc++.h>
using namespace std;
//#define long long ll;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
//typedef long long;
const long long int N=1e5+7;
long long int n,m;
struct node{
long long int earn,pay;//结构体,利润和投资;
}a[N];
int cmp(node a,node b){//比较的是最短开始回本的时间;
return a.pay*b.earn<b.pay*a.earn;//a.y/a.x<b.y/b.x的变式,防止除数不准确;
}
int main(){
IOS;
cin>>n>>m;
long long int earn=0,pay=0;
for(int i=0;i<n;i++){
cin>>a[i].earn>>a[i].pay;
}
sort(a,a+n,cmp);//
long long int ans=1e18;
for(int i=0;i<n;i++){//枚举
earn+=a[i].earn;
pay+=a[i].pay;
if((pay+m)%earn==0){
ans=min(ans,(m+pay)/earn);
}
else ans=min(ans,(m+pay)/earn+1);
}
cout<<ans<<endl;
return 0;
}
|