当前位置: 首页 > 工具软件 > PlanOut > 使用案例 >

Workout plan CodeForces - 1218F(优先队列贪心)

姚星宇
2023-12-01

Alan decided to get in shape for the summer, so he created a precise workout plan to follow. His plan is to go to a different gym every day during the next N days and lift X[i]X[i] grams on day ii. In order to improve his workout performance at the gym, he can buy exactly one pre-workout drink at the gym he is currently in and it will improve his performance by AA grams permanently and immediately. In different gyms these pre-workout drinks can cost different amounts C[i]C[i] because of the taste and the gym’s location but its permanent workout gains are the same. Before the first day of starting his workout plan, Alan knows he can lift a maximum of KK grams. Help Alan spend a minimum total amount of money in order to reach his workout plan. If there is no way for him to complete his workout plan successfully output −1−1.

Input
The first one contains two integer numbers, integers NN (1≤N≤105)(1≤N≤105) and KK (1≤K≤105)(1≤K≤105) – representing number of days in the workout plan and how many grams he can lift before starting his workout plan respectively. The second line contains NN integer numbers X[i]X[i] (1≤X[i]≤109)(1≤X[i]≤109) separated by a single space representing how many grams Alan wants to lift on day ii. The third line contains one integer number AA (1≤A≤109)(1≤A≤109) representing permanent performance gains from a single drink. The last line contains NN integer numbers C[i]C[i] (1≤C[i]≤109)(1≤C[i]≤109) , representing cost of performance booster drink in the gym he visits on day ii.

Output
One integer number representing minimal money spent to finish his workout plan. If he cannot finish his workout plan, output -1.

Examples
Input
5 10000
10000 30000 30000 40000 20000
20000
5 2 8 3 6
Output
5
Input
5 10000
10000 40000 30000 30000 20000
10000
5 2 8 3 6
Output
-1
Note
First example: After buying drinks on days 2 and 4 Alan can finish his workout plan. Second example: Alan cannot lift 40000 grams on day 2.
题意:每天这个人想举mi克的重量,一开始他可以举k g的重量,每天在商店里买的重量数是相同的,但是价格不一样。问要想完成所有的目标,需要的最小花费是多少。效果是持久的。
思路:因为效果一样并且持久,肯定是买价格小的。所以优先队列将价格放进去,每次取价格小的,直到满足条件了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=1e5+100;
int x[maxx],c[maxx];
int n,k,mb;

int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++) scanf("%d",&x[i]);
	scanf("%d",&mb);
	for(int i=1;i<=n;i++) scanf("%d",&c[i]);
	priority_queue<int,vector<int>,greater<int> >p;
	ll ans=0;
	int flag=0;
	for(int i=1;i<=n;i++)
	{
		p.push(c[i]);
		if(k<x[i])
		{
			while(p.size()&&k<x[i])
			{
				k+=mb;
				ans+=p.top();
				p.pop();
			}
			if(k<x[i]) flag=1;
		}
		if(flag) break;
	}
	if(flag) puts("-1");
	else cout<<ans<<endl;
	return 0;
}

努力加油a啊,(o)/~

 类似资料: