题目连接:

  http://codeforces.com/problemset/problem/555/B

题目大意:

  有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个桥,给出每个岛屿的位置和桥的长度,问是否可以把n个岛屿连起来?

解题思路:

  排序+贪心,对于n个岛屿,相邻的两个之间起点和端点可以转化为n-1个连接桥的长度区间,把区间升序排列。

  对于m个桥升序排列,对于每一个桥枚举每个可以合法覆盖的区间,选取最优的,选取的时候满足L<bridge_length<R,L经是有序的了。我们只需选取R最小的那个,因为R越大,这个区间就能适应更多的桥。

  本宝宝对于选取最优区间的时候采用了优先队列,并不是最优的处理方式,大家有好的办法可以留言告诉弱弱。

 #include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std; const int maxn = ;
#define LL __int64
struct bridge
{
LL val, id;
bool operator < (const bridge &a) const
{
return val < a.val;
}
} Bri[maxn]; struct island
{
LL r, l, id;
bool operator < (const island &a) const
{
return l < a.l;
}
} Is[maxn];
struct node
{
LL s, id;
bool operator < (const node &a) const
{
return s > a.s;
}
}; LL vis[maxn], n, m;
bool solve ()
{
priority_queue <node>Q;//当前桥可以搭建成功的区间
int l=;
memset (vis, , sizeof(vis)); for (int i=; i<m; i++)//对于每个桥选取最优的区间搭建
{
node nu;
while (!Q.empty())
{
nu = Q.top();
if (nu.s < Bri[i].val)
Q.pop();//删除队列中的不合法区间
else
break;
} while (Bri[i].val>=Is[l].l && Is[l].r >= Bri[i].val && l<n-)
{
nu.id = Is[l].id;
nu.s = Is[l].r;
Q.push (nu);//区间加进队列
l ++;
}
if (Q.empty())
continue;//没有合适的边,就继续循环加边
nu = Q.top ();
vis[nu.id] = Bri[i].id;//记录连接区间所对应的边
Q.pop();
}
for (int i=; i<n; i++)
if (!vis[i])
return false;
return true;//所有区间都对应有边
}
int main ()
{
while (scanf ("%I64d %I64d", &n, &m) != EOF)
{
island pre, cur;
LL i;
scanf ("%I64d %I64d", &pre.l, &pre.r);
for (i=; i<n; i++)
{
scanf ("%I64d %I64d", &cur.l, &cur.r);
Is[i-].id = i;
Is[i-].l = cur.l - pre.r;
Is[i-].r = cur.r - pre.l;
pre = cur;
}
for (i=; i<m; i++)
{
Bri[i].id = i+;
scanf ("%I64d", &Bri[i].val);
}
sort (Is, Is+n-);
sort (Bri, Bri+m);
if (solve ())
{
printf ("Yes\n");
for (i=; i<n-; i++)
printf ("%I64d ", vis[i]);
printf ("%I64d\n", vis[i]);
}
else
printf ("No\n");
}
return ;
}

codeforces 555B Case of Fugitive的更多相关文章

  1. codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

    题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...

  2. Codeforces 556D - Case of Fugitive

    556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/s ...

  3. CodeForces - 556D Case of Fugitive (贪心+排序)

    Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet ...

  4. Codeforces Round #310 (Div. 1) B. Case of Fugitive set

    B. Case of Fugitive Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/555/p ...

  5. Codeforces 555 B. Case of Fugitive

    \(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...

  6. Codeforces Round #310 (Div. 1) B. Case of Fugitive(set二分)

    B. Case of Fugitive time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  7. [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)

    [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...

  8. codeforces 556C. Case of Matryoshkas 解题报告

    题目链接:http://codeforces.com/contest/556/problem/C 题目意思:有 n 个数(1,2,...,n)组成 k 条链.第 i 条链由 mi 个数组成.每一秒只可 ...

  9. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

随机推荐

  1. java下XML与JSON互相转换的Utils类

    原文:http://heipark.iteye.com/blog/1394844 需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下: < ...

  2. B/S(WEB)系统中使用Activex插件调用扫描仪实现连续扫描并上传图像(IE文件扫描并自动上传)

    IE浏览器下使用Activex插件调用客户端扫描仪扫描文件并山传,可以将纸质档案(如合同.文件.资料等)扫描并将扫描图像保存到服务器,可以用于合同管理.档案管理等. 通过插件方式调用扫描仪扫描并获取图 ...

  3. 查看linux接口进出口流量的命令;linux 网络监控;流量监控

    1.nload,左右键切换网卡 2.sudo iftop 3.sudo iptraf 按连接/端口查看流量 4.sudo nethogs: 按进程查看流量占用 5.ss: 连接查看工具 6.dstat ...

  4. 基于 HTML5 WebGL 的挖掘机 3D 可视化应用

    前言 在工业互联网以及物联网的影响下,人们对于机械的管理,机械的可视化,机械的操作可视化提出了更高的要求.如何在一个系统中完整的显示机械的运行情况,机械的运行轨迹,或者机械的机械动作显得尤为的重要,因 ...

  5. Shell 脚本小试牛刀(5) -- 超便捷脚本之高速ssh 登录其它主机

    假设你也是以Linux 为工作环境的童鞋,那么此文真是捷报!由于我的学习/工作中(特别是近期玩耍树莓派)常常会使用到ssh 登录其它主机,而每次使用ssh 登录都须要输入老长一大串让我非常烦.所以我写 ...

  6. Linux上设置RAID 10

    RAID 10(又叫RAID 1+0或镜像条带)阵列结合了RAID 0和RAID 1两者的功能特性,从而提供了高性能.容错的磁盘输入/输出操作.在RAID 0中,读取/写入操作跨多个驱动器并路执行:在 ...

  7. 实例 tar备份以日期命名

    tar备份以日期命名****************************************************************************************#v ...

  8. Object.getOwnPropertyNames()

    1.Object.getOwnPropertyNames(),遍历实例属性(包括不可枚举),返回属性名组成的数组 var arr = ["a", "b", &q ...

  9. 开源 java CMS - FreeCMS2.2 敏感词管理

    项目地址:http://www.freeteam.cn/ 敏感词管理 管理敏感词.系统会自己主动将敏感词替换为指定字符. 系统进行敏感词处理的功能有: 信息:标题.内容,摘要. 栏目:名称,描写叙述. ...

  10. WWDC笔记:2011 Session 125 UITableView Changes, Tips and Tricks

    What’s New Automatic Dimensions - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSect ...