poj 3038
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的更多相关文章
- Flying Right POJ - 3038
有一条从南到北的航线,航线上有N个机场1-n从南到北分布,每天早上飞机从1飞到n,傍晚从n飞到1.有k组乘客,他们数量为M[k],从S飞到E,飞机上只有C个座位,计算每天飞机最多能拉多少乘客 贪心可以 ...
- POJ 3038 贪心(multiset)
题意: 思路: 1. 贪心 我们考虑肯定是走最近的最合适 想象自己是一个黑一日游的司机: 1.如果有乘客要上车,那么就让他上,收钱! 2.如果超载了,把距目的地最远的几个乘客踢下去,退钱. 3.行驶到 ...
- 类似区间计数的种类并查集两题--HDU 3038 & POJ 1733
1.POJ 1733 Parity game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5744 Accepted: ...
- poj 并查集
http://poj.org/problem?id=1611 水题 题意:就是找一共有多少个人感染了,0是感染学生的编号. #include <stdio.h> #include < ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
随机推荐
- mybatis 使用经验小结
一.多数据源问题 主要思路是把dataSource.sqlSesstionFactory.MapperScannerConfigurer在配置中区分开,各Mapper对应的包名.类名区分开 <? ...
- JSP基础学习
JQuery教程: http://www.w3school.com.cn/jquery/ HTTP协议的 http://www.w3.org/Protocols/rfc2616/rfc2616.htm ...
- 2015Qcon参会记录(内有show girl)
因为前一段刚去了一趟杭州的阿里TD时代交流,就没有好意思在部门内部申请Qcon的门票.但最后还是没有耐得住心里的痒,请假去听了,这里多谢把票让给我的XX同学(应他要求,马赛克了名字). 今年的Qcon ...
- C/C++实践笔记 004
转义字符 #define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h> void main1() { c ...
- 我为什么还要造轮子?欠踹?Monk.UI表单美化插件诞生记!
背景 目前市场上有很多表单美化的UI,做的都挺不错,但是他们都有一个共同点,那就是90%以上都是前端工程师开发的,导致我们引入这些UI的时候,很难和程序绑定.所以作为程序员的我,下了一个决定!我要自己 ...
- SQLite剖析之编程接口详解
前言 使用过程根据函数大致分为如下几个过程: sqlite3_open() sqlite3_prepare() sqlite3_step() sqlite3_column() sqlite3_fina ...
- SQL数据库分配权限
打开SQL-Server管理工具->安全性->登陆名->右键(新建登陆名) 输入相应的信息(这里要去掉强制实施密码策略,强制密码过期,否则用户在下次登录时必须修改密码) 用户映射-& ...
- Python基础-三次用户验证登录购买商品程序
需求: 一:三次登录锁定 1.用户信息存放于文件中 2.尝试三次都失败,锁定用户 二.购物车功能要求: 要求用户输入总资产,例如:2000显示商品列表,让用户根据序号选择商品,加入购物车购买,如果商品 ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- Leetcode 285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 本题 ...