题目十六 贪心算法+优先队列
来源:互联网 发布:苹果u盘安装mac系统 编辑:程序博客网 时间:2024/06/01 07:22
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct A
{
int l;
int r;
bool operator< (A a) const{
return r>a.r;
};
}cow[2505];
struct B
{
int f;
int num;
}bot[2505];
bool cmpc(A a,A b)
{
return a.l<b.l;
}
bool cmpb(B a,B b)
{
return a.f<b.f;
}
int main()
{
int C,L;
while(~scanf("%d %d",&C,&L))
{
for(int i=1;i<=C;i++)
scanf("%d %d",&cow[i].l,&cow[i].r);
for(int i=1;i<=L;i++)
scanf("%d %d",&bot[i].f,&bot[i].num);
sort(cow+1,cow+C+1,cmpc);
sort(bot+1,bot+N+1,cmpb);
int cnt=0,u=1;
priority_queue<A> q;
for(int i=1;i<=L;i++)
{
while(bot[i].f>=cow[u].l)
q.push(cow[u++]);
while(q.size()&&bot[i].num)
{
if(bot[i].f>q.top().r)
{
q.pop();
continue;
}
cnt++;
bot[i].num--;
q.pop();
}
}
printf("%d\n",cnt);
}
return 0;
}
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
struct A
{
int l;
int r;
bool operator< (A a) const{
return r>a.r;
};
}cow[2505];
struct B
{
int f;
int num;
}bot[2505];
bool cmpc(A a,A b)
{
return a.l<b.l;
}
bool cmpb(B a,B b)
{
return a.f<b.f;
}
int main()
{
int C,L;
while(~scanf("%d %d",&C,&L))
{
for(int i=1;i<=C;i++)
scanf("%d %d",&cow[i].l,&cow[i].r);
for(int i=1;i<=L;i++)
scanf("%d %d",&bot[i].f,&bot[i].num);
sort(cow+1,cow+C+1,cmpc);
sort(bot+1,bot+N+1,cmpb);
int cnt=0,u=1;
priority_queue<A> q;
for(int i=1;i<=L;i++)
{
while(bot[i].f>=cow[u].l)
q.push(cow[u++]);
while(q.size()&&bot[i].num)
{
if(bot[i].f>q.top().r)
{
q.pop();
continue;
}
cnt++;
bot[i].num--;
q.pop();
}
}
printf("%d\n",cnt);
}
return 0;
}
阅读全文
0 0
- 题目十六 贪心算法+优先队列
- POJ 题目2431 Expedition(贪心,优先队列)
- poj3614-贪心、优先队列
- EOJ1855 贪心+优先队列
- uvalive4254(贪心+优先队列)
- HOJ1062 贪心+优先队列
- poj3465 贪心+优先队列
- 贪心+优先队列
- 贪心+优先队列
- POJ2010 贪心优先队列
- 贪心+优先队列
- POJ3614Sunscreen(优先队列+贪心)
- 优先队列贪心
- HDU6047优先队列+贪心
- POJ2431 优先队列+贪心
- 贪心加优先队列
- 优先队列题目
- POJ2431 二叉堆(优先队列)以及贪心算法
- Dokcer 删除所有容器
- 《TensorFlow实战》学习1——softmax regression
- HBase 性能优化方法总结
- selenium初体验
- 第2章-线程安全性
- 题目十六 贪心算法+优先队列
- 深入理解Spark ML:多项式朴素贝叶斯原理与源码分析
- 测试
- 一个 tflearn 情感分析小例子
- linux查找目录下的所有文件中是否含有某个字符串
- Android--多线程之Looper
- memcpy的速度测试
- hdu 3729 二分图匹配
- Object的equals和hashCode方法