题目连接:

  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. javascript array-like object

    http://www.nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/ ...

  2. 字符设备之poll机制

    poll机制作用:相当于一个定时器.时间到了还没有资源就唤醒进程. 主要用途就是:进程设置一段时间用来等待资源,假设时间到了资源还没有到来,进程就立马从睡眠状态唤醒不再等待.当然这仅仅是使用于这段时间 ...

  3. C#如何引用定义好的dll文件

    1 添加引用,找到dll文件   2 引用类的名称空间,生成类的实例,调用类的方法,测试OK.

  4. strtok函数

    strtok函数是cstring文件里的函数 strtok函数是cstring文件里的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...

  5. UC技术博客开放通知

    国内知名浏览器UC开放技术博客( http://tech.uc.cn/),技术博客所涵盖技术点有: Hadoop Linux MySQL 前端与client技术 图像处理 开发语言和框架 数据存储 数 ...

  6. python 线程 进程 标识

    s = '%s%s%s%s%s%s%s%s' % ( time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time())), ' os.getpp ...

  7. CMake 入门实战【转】

    本文转载自:http://www.hahack.com/codes/cmake/ 什么是 CMake All problems in computer science can be solved by ...

  8. YTU 2438: 三人三鬼

    2438: 三人三鬼 时间限制: 1 Sec  内存限制: 128 MB 提交: 35  解决: 9 题目描述 目标是将东岸的3人3鬼通过一只小船转移到西岸,希望以尽可能少的摆渡次数. 船的容量有限, ...

  9. 闲得蛋疼,JavaScript版本BadApple

    参考Vim版本的BadApple改写而成.由于加载数据比较大,可能网速不给力的童鞋效果不太好,多刷新几次就好了,^_^.运行环境:支持HTML5 Canvas的浏览器.​1. 代码:$(functio ...

  10. Oracle 删除用户和表空间////Oracle创建删除用户、角色、表空间、导入导出、...命令总结/////Oracle数据库创建表空间及为用户指定表空间

    Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用如下命令来完成. 步骤一:  删除user drop ...