BAPC2018 Financial Planning 题解

题目描述
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值