moxin最近太穷了,穷到只能去卖菜了,moxin进了一批菜,每个菜有过期时间ti与对应的价格wi,由于moxin实在是不会卖菜,富婆弟弟可怜moxin,于是决定每天向moxin买一种菜,moxin这么有节操的人是不会卖过期的菜的,假设时间是无穷的,那么请问moxin最多能卖出多少价值的菜?(时间从1开始)
Input
输入一个n,代表有n种菜(1 <= n <= 105)有多组输入数据
接下来n行,每行有两个数wi,ti(1 <= wi,ti <= 105)
Output
输出总价值
SampleInput
1
1 1
SampleOutput
1
题意:有N种菜,每种菜有期限和他的权值,假设售卖的时间是无限长的,问最多能获益多少。
思路:这题我WA了六发,没有考虑到用优先队列维护答案,至于为什么用优先对列,因为排在后面的菜可能你比你之前的更优秀,总体来说 思路就是 排序+贪心+优先队列维护。
下面献上我的low逼代码 具体看代码注释
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 2e5+5;
struct node
{
int t,v;
}a[N];///结构体存每种菜的期限和价值
bool cmp(node a,node b)///先以时间排 再以权值排
{
if(a.t!=b.t)
return a.t < b.t;
return a.v > b.v;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
priority_queue<int,vector<int>,greater<int> >que;///小根堆使得更小的排在前
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].v,&a[i].t);
sort(a+1,a+1+n,cmp);
LL res=0;
for(int i=1;i<=n;i++)
{
if(que.size() < a[i].t)///满足时间条件的输入
{
res+=a[i].v;
que.push(a[i].v);
}
else
{
if(a[i].v > que.top())///同一时间如果我的权值比你的大,则交换
{
res-=que.top();
que.pop();
res+=a[i].v;
que.push(a[i].v);
}
}
}
printf("%lld\n",res);
}
return 0;
}