思路
很明显的贪心问题,根据题意肯定是最便宜的巧克力买最多,但保质期又是我们需要考虑的因素,我们选择从单价低到高一颗一颗的选,选到达到标准就退出,这样可以a大半的数据,优化的代码想不出来
代码
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PLL;
const int N = 2e5;
typedef long long ll;
pair<int,PLL> d[N]; //d的结构:<单价,<保质期,数量>>
int x,n; //x为需要的天数
bool cmp(pair<int,PLL> a,pair<int,PLL> b)
{
return a.x < b.x;
}
int main()
{
cin >> x >> n;
for(int i = 0 ; i < n ; i ++)
{
int a,b,c;
cin >> a >> b >> c;
PLL tmp = {b,c};
d[i] = {a,tmp};
}
sort(d,d + n,cmp);
ll res = 0; //花多少钱
int day = 0; //过了多少天
for(int i = 0 ; i < n ; i ++)
{
while(day < d[i].y.x && d[i].y.y > 0) //d[i].y.y为数量
{
day ++;
d[i].y.y --;
res += d[i].x;
x --;
if(x == 0)
{
cout << res << endl;
return 0; //技巧,无论什么循环里面写return 0是直接结束main函数的
}
}
}
cout << "-1" << endl;
return 0;
}