http://poj.org/problem?id=3038

这个题我是在一个关于并查集的博客中找到的,结果我就觉得这个应该是个贪心,真想不出这个与并查集有什么鬼关系,看discuss里面也都是贪心,我是不懂大神的想法,最后,我点开链接才发现那是杭电的3038.。。我也是醉了,然后一早上就搞了这一道题。贪心还是不怎么会。

题意:就是一个牛搞了一个航空公司,想让这个航空公司可以在一天来回运送最多的客人。求最多可以运送多少客人。

思路:discuss里面有个大神说的很简单,也很有道理。

1.碰到人,上车。

2.超载,最远的乘客下车。

3.行驶到下一站。

就这三句话,解决了这个题,可我还是有点懵,感觉思路还是不是很清晰。

最后看到了一个人的博客, 才最终懂了。

 #include <stdio.h>
#include <string.h>
#include <queue>
#include <stdlib.h> #define maxn 50005 using namespace std; int ans = ,c,exist[ maxn ]; struct note{
int x,y,c;
}to[ maxn ],ba[ maxn ]; int cmp(const void *a,const void *b) //排序
{
if((*(note *)a).x != (*(note * )b).x)
return (*(note *)a).x - (*(note * )b).x;
else
return (*(note *)a).y - (*(note * )b).y;
} void deal(note tmp[],int x)
{
priority_queue<int,vector<int>,greater<int> >small;
priority_queue<int,vector<int>,less<int> >most;
int people = ,i;
memset( exist , , sizeof( exist ) );
for( i = ; i < x ; i++ )
{
small.push(tmp[i].y);
most.push(tmp[i].y);
exist[tmp[i].y] += tmp[i].c;
while(tmp[i].x>=small.top()) //有人到达了目的地,下车。
{
people -= exist[ small.top() ];
exist[ small.top() ] = ;
small.pop();
}
people += tmp[ i ].c; //上车。
ans += tmp[ i ].c;
if(people >c) //是否超载。
{
int t = people-c;
ans -= t;
people = c;
while(t)
{
if( exist[ most.top() ] > t)
{
exist[ most.top() ] -= t;
t = ;
}
else
{
t -= exist[ most.top() ];
exist[ most.top() ] = ;
most.pop();
}
}
}
}
} int main()
{
//freopen("in.txt","r",stdin);
int k,m,s,w,p,t = ,e = ;
scanf("%d%d%d",&k,&m,&c);
while( k-- )
{
scanf("%d%d%d",&s,&w,&p);
if( s < w )
{
to[ t ].x = s;
to[ t ].y = w;
to[ t++ ].c = p;
}
else
{
ba[ e ].x = w;
ba[ e ].y = s;
ba[ e++ ].c = p;
}
}
qsort( to , t , sizeof( to[ ] ) , cmp );
deal( to , t );
qsort( ba , e , sizeof( ba[ ] ) , cmp );
deal( ba , e );
printf("%d\n",ans);
return ;
}

poj 3038的更多相关文章

  1. Flying Right POJ - 3038

    有一条从南到北的航线,航线上有N个机场1-n从南到北分布,每天早上飞机从1飞到n,傍晚从n飞到1.有k组乘客,他们数量为M[k],从S飞到E,飞机上只有C个座位,计算每天飞机最多能拉多少乘客 贪心可以 ...

  2. POJ 3038 贪心(multiset)

    题意: 思路: 1. 贪心 我们考虑肯定是走最近的最合适 想象自己是一个黑一日游的司机: 1.如果有乘客要上车,那么就让他上,收钱! 2.如果超载了,把距目的地最远的几个乘客踢下去,退钱. 3.行驶到 ...

  3. 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733

    1.POJ 1733 Parity game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5744   Accepted: ...

  4. poj 并查集

    http://poj.org/problem?id=1611 水题 题意:就是找一共有多少个人感染了,0是感染学生的编号. #include <stdio.h> #include < ...

  5. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

随机推荐

  1. 如何使iframe透明

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. 【原】KMeans与深度学习模型结合提高聚类效果

    这几天在做用户画像,特征是用户的消费商品的消费金额,原始数据(部分)是这样的: id goods_name goods_amount 男士手袋 1882.0 淑女装 2491.0 女士手袋 345.0 ...

  3. 更好的pip工作流

    转自:http://codingpy.com/article/a-better-pip-workflow-recommended-by-kenneth/ 现在大家开发Python应用时,在代码库的根目 ...

  4. windows下使用vs进行Proctocol Buffer开发(C++篇)

    因工作原因接触Proctocol Buffer(protobuf),至于什么是protobuf,为何使用protobuf,我就不赘述了,百度下都是答案. 今天我介绍的是在windows下使用vs进行p ...

  5. 微信签名算法的服务端实现(.net版本)

    一.概要 微信此次开放JS接口,开放了一大批api权限,即使在未认证的订阅号也可以使用图像接口,音频接口,智能接口,地理位置,界面操作,微信扫一扫等功能.要知道:以前订阅号只能接受和被动回复用户消息而 ...

  6. nginx文件管理

    管理文件下载nginx 可以自己实现,无需写代码即可: 修改配置文件: location /doc { autoindex on; autoindex_exact_size on; autoindex ...

  7. String()与 toString()

    我们知道String()与 .toString()都是可以转换为字符串类型,但是String()与 .toString()的还是有区别的 1..toString()可以将所有的的数据都转换为字符串,但 ...

  8. Select Top在七种数据库中的使用方法(包含mysql)

    1. Oracle数据库 SELECT * FROM TABLE1 WHERE ROWNUM<=N 2. Infomix数据库 SELECT FIRST N * FROM TABLE1 3. D ...

  9. 100 个 Linux 常用命令大全

    1.ls [选项] [目录名 | 列出相关目录下的所有目录和文件 -a 列出包括.a开头的隐藏文件的所有文件 -A 通-a,但不列出"."和".." -l 列出 ...

  10. <<< request.getParameterMap()方法

    request.getParameterMap()方法返回的值,是一个Map类型的,记录着所提交的请求中请求参数和请求参数值的映射关系. 当不知道前台传过来的是什么,且不知道传过来的对象时,可用这个, ...