题目连接:

  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. HTML的DIV如何实现垂直居中

    外部的DIV必须有如下代码 display:table-cell; vertical-align:middle;   这样可以保证里面的东西,无论是DIV还是文本都可以垂直居中

  2. 是男人就下100层【第四层】——Crazy贪吃蛇(2)

    在上一篇<是男人就下100层[第四层]--Crazy贪吃蛇(1)>中我们让贪吃蛇移动了起来,接下来我们来实现让贪吃蛇能够绕着手机屏幕边线移动而且能够改变方向 一.加入状态并改动代码 首先我 ...

  3. 微信小程序 自定义组件(stepper)

    项目目录: 步骤一:创建组件 声明这一组文件为自定义组件 stepper.json { "component": true, "usingComponents" ...

  4. fedora下安装xdot和objgraph

    前提:安装好了python 1.先下载xdot-0.6.tar.gz和objgraph-1.8.0-py27-none-any.whl,你也可以在官网上下载其他版本. 2.下载完后,解压. 3.打开终 ...

  5. Django项目开发-小技巧

    当你开发完一个Django项目之后肯定要吧他丢到服务器让跑起来,但是你在自己的环境下安装了好多的包,是不是在服务器中也要一个个的安装了, pip freeze > read.txt #这条命令会 ...

  6. 【HDOJ 3652】B-number

    [HDOJ 3652]B-number 给一整数n 找<=n的整数中能被13整除且含有13的 数位dp 记忆化! . 一入记忆化深似海. ..再也不想用递推了...发现真的非常好想 仅仅要保证满 ...

  7. Android修改签名

    #!/bin/shtmp=~/temp.apkcp "$1" "$tmp"zip -d "$tmp" META-INF/\*jarsigne ...

  8. Android ListView分页,动态添加数据

    1.ListView分页的实现,重点在于实现OnScrollListener接口,判断滑动到最后一项时,是否还有数据可以加载, 我们可以利用listView.addFootView(View v)方法 ...

  9. Android ConstraintLayout详解

    1. 概述 在本篇文章中,你会学习到有关ConstraintLayout -- 一种构建于弹性Constraints(约束)系统的新型Android Layout.最终你将会在Android Stud ...

  10. Persisting Data to the Device

    Persisting Data to the Device Edit PagePage History Overview The Android framework offers several op ...