int x=s.top();if(x

lalalallalalalala 2015-07-28 03:44:17
#include <stdio.h>
#include <algorithm>
#include <queue>
using namespace std;//不可少!!
struct node
{
int day,cost;
}c[2005];
bool cmp(node x,node y)
{
if(x.day<y.day) return true;//按升序排列
if(x.day==y.day&&x.cost<y.cost) return true;
return false;
}
int main()
{
int n,sum;
priority_queue<int,vector<int>,greater<int> >s;
while(scanf("%d",&n)!=EOF)
{
while(!s.empty())
s.pop();
for(int i=0;i<n;i++)
scanf("%d %d",&c[i].day,&c[i].cost);
sort(c,c+n,cmp);
sum=0;
for(int i=0;i<n;i++)
{
if(s.size()<c[i].day)//s.size()可以理解为天数。如果当前天数小于最后完成的天数,证明还是有天数空间的,直接入队列
s.push(c[i].cost);
else//如果当前天数大于最后完成的天数,必须要扣除分数了,为了使得扣除的分数最小,比较s.top()和当前的c[i].cost,舍弃扣除的最小的
{
int x=s.top();
if(x<c[i].cost)
s.pop(),s.push(c[i].cost),sum+=x;
else
sum+=c[i].cost;
}
}
printf("%d\n",sum);
}
return 0;
}
假设输入是
7
1 3
4 2
6 1
4 7
2 6
4 5
3 4
求解~~
...全文
1548 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
fly_dragon_fly 2015-07-28
  • 打赏
  • 举报
回复
s是优先队列, 在stl中就是堆了, 通常是建立大顶堆, 这里用的greater, 就是小顶堆, s.top指的就是当前最小的元素
赵4老师 2015-07-28
  • 打赏
  • 举报
回复
VS IDE中,在不明白的符号上点鼠标右键,选转到定义。
lalalallalalalala 2015-07-28
  • 打赏
  • 举报
回复
来人来人来人吧
lalalallalalalala 2015-07-28
  • 打赏
  • 举报
回复
求解求解求解~

69,375

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧