之前做过一道基本一样的题目,抽象出来就是有个容量为c的载体,一些线段上某个点到另一个点要运输w个东西,求从头到尾最多能运多少东西。

这种模型可以用贪心做,用map,map[r]表示r的那个点,我们准备运多少头牛到那里,但是还没运到

用map的好处是不管是插入还是删除,它都按坐标从小到大排。

那么先把所有的边按左端点从小到大排,对于当前边,

容量未满的时候,直接加入map

容量满的时候,若map中最大的坐标比这条边的右端点要大,那么显然用当前边替换会更好。

运到的怎么处理呢?

当我们枚举到这个点,这个点的map值大于零说明有牛运到这里了,则ans+=map[now],cap-=map[now],并从map中删除这个点。

还有这道题从1到n还要从n到1回来,可以做两遍,也可以把回去的那段倒着拼在后面

map的用法还不是很熟练,写了一个多小时才写完,不过一次AC也是非常爽的= =

 #include<stdio.h>
 #include<string.h>
 #include<algorithm>
 #include<map>
 #define INF 1000000000
 using namespace std;
 ;
 struct node{
     int l,r,w;
 }e[maxn*];
 map<int,int> mp;
 int n,m,c,cap,head,ans;

 bool cmp(node a, node b){
     if (a.l==b.l) return a.r<b.r;
     else return a.l<b.l;
 }

 int main(){
     scanf("%d%d%d", &m, &n, &c);
     ; i<=m; i++){
         scanf("%d%d%d", &e[i].l, &e[i].r, &e[i].w);
         *n-e[i].l,e[i].r=*n-e[i].r;
     }
     sort(e+,e++m,cmp);
     mp.clear();
     mp[-INF]=; cap=;
     map<int,int>::iterator it;
     head=; ans=;
     ; now<=*n-; now++){
         ){
             ans+=mp[now];
             cap-=mp[now];
             it=mp.find(now);
             mp.erase(it);
         }
         while (e[head].l<now) head++;
         while (e[head].l==now){
             int to=e[head].r,num=e[head].w;
             ) cap++,num--,mp[to]++;
             if (cap==c && num)
                 for (it=--mp.end(); it!=mp.begin();){
                     if ((it->first)>to){
                         if ((it->second)>num){
                             (it->second)-=num;
                             mp[to]+=num;
                             break;
                         }else{
                             mp[to]+=(it->second);
                             num-=(it->second);
                             mp.erase(it--);
                         }
                     }else break;
                 }
             head++;
         }
     }
     printf("%d\n", ans);
     ;
 }

bzoj1745: [Usaco2005 oct]Flying Right 飞行航班(贪心+map)的更多相关文章

  1. bzoj1745[Usaco2005 oct]Flying Right 飞行航班*

    bzoj1745[Usaco2005 oct]Flying Right 飞行航班 题意: n个农场,有k群牛要从一个农场到另一个农场(每群由一只或几只奶牛组成)飞机白天从农场1到农场n,晚上从农场n到 ...

  2. [Usaco2005 oct]Flying Right 飞行航班

    Description 为了表示不能输给人类,农场的奶牛们决定成立一家航空公司.她们计划每天早晨,从密歇根湖湖岸的最北端飞向最南端,晚上从最南端飞往最北端.在旅途中,航空公司可以安排飞机停在某些机场. ...

  3. BZOJ 1684: [Usaco2005 Oct]Close Encounter

    题目 1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MB Description Lacking e ...

  4. 1684: [Usaco2005 Oct]Close Encounter

    1684: [Usaco2005 Oct]Close Encounter Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 387  Solved: 181[ ...

  5. BZOJ 1685 [Usaco2005 Oct]Allowance 津贴:贪心【给硬币问题】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1333 题意: 有n种不同币值的硬币,并保证大币值一定是小币值的倍数. 每种硬币的币值为 ...

  6. 【BZOJ】1685: [Usaco2005 Oct]Allowance 津贴(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1685 由于每个小的都能整除大的,那么我们在取完大的以后(不超过c)后,再取一个最小的数来补充,可以证 ...

  7. bzoj:1685 [Usaco2005 Oct]Allowance 津贴

    Description As a reward for record milk production, Farmer John has decided to start paying Bessie t ...

  8. (贪心 map) Flying to the Mars hdu1800

    Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. BZOJ2697 特技飞行 【贪心】

    题目链接 BZOJ2697 题解 好水好水的贪心... 容易发现每种特技只表演两次,多表演没有意义,而且差距越长收益越大 然后就可以贪,最大的放两端,次大的往里,然后是第三大....... 证明很简单 ...

随机推荐

  1. PhpStorm 9.03 集成 开源中国(oschina.net)的Git项目,提交SVN时注意事项

    第一步:配置 git.exe File -> Default Settings -> Version Control -> Git -> Path go Git executa ...

  2. C和指针 第七章 可变参数

    可变参数列表是通过stdarg.h内的宏来实现的: 类型 va_list 三个宏: va_start va_arg va_end 我们可以声明一个va_list变量,与这三个宏配合使用. 可变参数必须 ...

  3. Add Two Numbers LeetCode Java

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. TypeError: test() got multiple values for keyword argument 'key'

    原因是: 1.函数调用的最终形式只会调用两个函数.一个list参数和一个dict参数,格式为call(func, list, dict); 2.如果传入参数中有key参数,那么首先key参数(包括扩展 ...

  5. 两个int的和判断溢出

    long a,b; cin>>a>>b; long i; i = a+b; if((i^a)<0 && (i^b)<0) cout<<& ...

  6. getRealPath("/")弃用

    用request.getSession().getServletContext().getRealPath("/");替换getRealPath("/"):

  7. showModalDialog打开页面有缓存,不走action

    当你设置的弹出网页固定时,ie很可能到临时文件区,下载上次产生的该页面,而没有重新加载,    对于动态加载的页面来说,这样往往产生误会,如没有及时更新数据,也就更不利于开发者测试.所以,你可以采用如 ...

  8. 怎么在myeclipse中导入已经写好的项目

    经常我们需要学习别人写好了的源码来提升自己的编码能力,本文将介绍如何从外部导入别人已经写好的项目到我们myeclipse里面.同时也将介绍怎么给导入的工程改名的问题.                 ...

  9. MIME列表

    .asx,video/x-ms-asf.xml,text/xml.tsv,text/tab-separated-values.ra,audio/x-pn-realaudio.sv4crc,applic ...

  10. ios 模拟器内部网络连接问题

    今日,一运行程序,打印出来头疼的的日志 "Error Domain=kCFErrorDomainCFNetwork Code=-1001 "The request timed ou ...