BAPC 2018 F Financial Planning

**

BAPC 2018 F Financial Planning(二分+贪心)

**

Description

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

题意

给你n个项目,每个项目有一个花费ci,购买这个项目后,这个项目每天可以获得的利润是pi,问你要多少天才能赚到(你的所有花费+M)的钱。

思路

一开始以为是背包,然后做的时候仔细想了想其实是贪心。
策略:
假如我们最优的方案需要n天。
那么对一个项目而言,如果满足n*pi>=ci,我们的策略中就可以在第一天就选取他,然后在第n天这个项目是稳赚不赔的。好,策略有了,剩下实现。看了别人的题解是用二分做的,我这里没用二分但加了一个判定,或许是因为数据不强所以过了。
这里是别人的二分解法:
转载至:https://blog.csdn.net/weixin_43935894/article/details/89505540

#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;
}

这下面是我自己比赛时写的

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
struct P{
	int get, cost;
	double ju;
}f[100005];
ll M;

bool operator < (P a,P b){
	return a.ju>b.ju;
}
int main(){
	int i, j, n, m;
	scanf("%d%lld",&n, &M);
	for(i = 1; i <= n; i++){
		scanf("%d %d",&f[i].get,&f[i].cost);
		f[i].ju = (double)(f[i].get)/(double)(f[i].cost); 
	}
	ll ans;
	ll now = 0;
	ll sum = 0;
	sort(f+1, f+1+n);
	j = 1;
	for(i = 1, ans = 1; now < M; i++,ans++){
		if(j > n){  //这一条非常重要,因为我们都选完了,所以不需要再遍历了,只需要处理一下边界条件即可。
			ans += (M-now)/sum;
			ans--;
			if(((M-now)%sum)!=0) ans++;
			break;
		}
		for(; j <= n; j++){
				if((double)i*f[j].ju >= 1.0){
				sum += (ll)f[j].get;
				now += ((ll)i*(ll)f[j].get);
				now -= (ll)(f[j].cost+f[j].get);
			}
			else break;	
		}
		now += sum;
	}
	if(now>=M) ans--;
	printf("%d\n",ans);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值