题目连接:

  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. Unable to connect to database server to retrieve database list; Arcgis 连接不上postsql库;

    在C:\Program Files (x86)\ArcGIS\Desktop10.2\bin 目录下添加 pg依赖的插件 插件下载地址:

  2. poj2481 Cows

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  3. 在EasyUI的DataGrid中嵌入Combobox

    在做项目时,须要在EasyUI的DataGrid中嵌入Combobox,花了好几天功夫,在大家的帮助下,最终看到了它的庐山真面: 核心代码例如以下: <html> <head> ...

  4. 【Android数据存储】- File

    个人学习整理.如有不足之处,请不吝不吝赐教. 转载请注明:@CSU-Max 读写本应用程序数据目录中的文件        此种方法读写的文件在/data/data/<应用程序包名>中   ...

  5. LoadRunner系列之—-04 录制基于https协议的脚本

    实际性能测试过程中,有些需录制脚本的页面或接口是基于https协议的,按原来方法录制脚本,录完了脚本是空的.为解决这个问题,第一步了解https协议的具体实现,这块网上资料很多,可参考页面下方参考资料 ...

  6. udhcp源码详解(四) 之租赁IP的管理

    Server端对于租赁出去的IP的管理是基于结构体dhcpOfferedAddr的,该结构体的定义是在leases.c文件里:(结构体的成员介绍说明见详解之数据结构) 1: struct dhcpOf ...

  7. HDU1251 统计难题 【trie树】

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  8. Spring如何实现IOC和AOP的,说出实现原理。

    用过spring的朋友都知道spring的强大和高深,都觉得深不可测,其实当你真正花些时间读一读源码就知道它的一些技术实现其实是建立在一些最基本的技术之上而已:例如AOP(面向方面编程)的实现是建立在 ...

  9. 如何用DOS命令,获取一个目录下的文件数目

    发信人: GOOGOODALLS (我爱Figo), 信区: DOS 标  题: 如何用DOS命令,获取一个目录下的文件数目? 发信站: 水木社区 (Fri Mar  9 08:40:01 2007) ...

  10. YTU 1012: A MST Problem

    1012: A MST Problem 时间限制: 1 Sec  内存限制: 32 MB 提交: 7  解决: 4 题目描述 It is just a mining spanning tree ( 最 ...