Financial Planning(二分)

为了规划舒适的退休生活,一位年轻人决定利用股票市场投资。通过借款并选择最优的投资组合,在达到所需退休金额的同时,尽快偿还借款。本篇详细介绍了如何通过计算每日利润和成本,使用二分搜索算法确定最快达到目标所需的最小天数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
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

思路
这题每天获得的收益是递增的,因此可以用二分的方法来解决这道题

代码实现

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=1e5+5;
ll n,m;
pair<ll,ll> costp[N];

int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++) scanf("%lld%lld",&costp[i].first,&costp[i].second);
    ll l=1,r=4000000009;
    while(l<r)
    {
        ll mid=(l+r)/2;
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(costp[i].first*mid-costp[i].second>0)
            {
                ans+=costp[i].first*mid-costp[i].second;
                if(ans>=m) break;
            }
        }
        if(ans>=m) r=mid;
        else l=mid+1;
    }
    printf("%lld\n",l);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值