题目连接:

  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. Python的环境变量设置

    python安装完成后,它的配置很简单,只需要配置下环境变量就可以了. 具体来讲,就是将python的安装目录加入到系统的path中即可.

  2. 30分钟学会如何使用Shiro(转)

    本文转自http://www.cnblogs.com/learnhow/p/5694876.html 感谢作者 本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jin ...

  3. linux动态库的种种要点

    linux下使用动态库,基本用起来还是非常easy.但假设我们的程序中大量使用动态库来实现各种框架/插件,那么就会遇到一些坑,掌握这些坑才有利于程序更稳健地执行. 本篇先谈谈动态库符号方面的问题. 測 ...

  4. Office2010,PPT,EXCEL如何插入日历控件

    1 在Office2010中插入其他控件,然后找到日历控件   2 十字架随便在Excel中绘制一下,得到一个日历控件,注意此时还是在设计模式下,在设计模式下日历控件不是正常状态,你还是可以双击这个控 ...

  5. POST &amp; GET &amp; Ajax 全解

    GET&POST&Ajax 全解 一.POST和GET的差别 GET:GET方法提交数据不安全,数据置于请求行.客户段地址栏可见:GET方法提交的数据限制大小在255个字符之内.參数直 ...

  6. Vs2017添加引用时报错 未能正确加载“ReferenceManagerPackage”包。

    Vs2017添加引用时报错未能正确加载“ReferenceManagerPackage”包. 最近新装了2017,开始前几天还好, 可是最近在添加引用时,报错 -------------------- ...

  7. HDU 1031.Design T-Shirt【结构体二次排序】【8月21】

    Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board ...

  8. 报错:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView

    近期学习RecyclerView,使用eclipse引用RecyclerView.编写完demo后编译没有问题,一执行就挂掉,错误例如以下: 07-22 23:05:34.553: D/Android ...

  9. iOS与HTML交互问题

    一. 加载后台传过来的HTML标签,文字都能正常显示但是图片显示不了.找问题找了很久没有发现那个地方写错,也问了别人都不知道,后来问了Android才知道,后台传过来的HTML标签,有些是转义过的.移 ...

  10. 51NOD 1821 最优集合 栈

    1821 最优集合   一个集合S的优美值定义为:最大的x,满足对于任意i∈[1,x],都存在一个S的子集S',使得S'中元素之和为i. 给定n个集合,对于每一次询问,指定一个集合S1和一个集合S2, ...