Codeforces题号:#310B

出处: Codeforces

主要算法:贪心+优先队列

难度:4.6

思路分析:

这道题乍一看没有思路……

  考虑贪心的做法。首先预处理出每两座相邻的桥之间边界相差的min和max(即题目要求的),存在b数组中。将桥的长度从小到大排序。将b数组按照min从小到大排序。

  这样做有什么好处呢?我们枚举每一座桥,然后按顺序选出它适合放置的那些区间。由于这些区间都是适合放这座桥的,所以我们自然要选择差距最小的,也就是max最小的。这其实是一个贪心:让当前这座桥利用的区间尽量小,让别的更长的桥有更大的空间——这就是为什么b数组要排序。

  那么具体如何来实现呢?我们可以维护一个优先队列。在这些桥的长度都>=min的情况下,我们需要max最小。因此我们可以用优先队列维护,max最小的作为堆顶。然后每一次都选出第一个区间来安置当前这座桥。如果发现无法安置,那么也就是说在所有适合当前桥的区间都已近用完了,也就无解了。

代码注意点:

  变量名不要打错。

Code

/** This Program is written by QiXingZhi **/
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define Max(a,b) (((a)>(b)) ? (a) : (b))
#define Min(a,b) (((a)<(b)) ? (a) : (b))
using namespace std;
typedef long long ll;
#define int ll
const int N = ;
const int M = ;
const int INF = ;
inline int read(){
int x = ; int w = ; register int c = getchar();
while(c ^ '-' && (c < '' || c > '')) c = getchar();
if(c == '-') w = -, c = getchar();
while(c >= '' && c <= '') x = (x << ) +(x << ) + c - '', c = getchar();
return x * w;
}
struct Island{
int l,r;
}a[N];
struct Dist{
int min,max,idx;
friend bool operator < (Dist a, Dist b){
return a.max > b.max;
}
}b[N];
struct Bridge{
int len,idx;
}bri[M];
int n,m,top,cnt;
int ans[N];
priority_queue <Dist> q;
inline bool comp_dist(Dist& a, Dist& b){
if(a.min != b.min) return a.min < b.min;
return a.max < b.max;
}
inline bool comp_bridge(Bridge& a, Bridge& b){
return a.len < b.len;
}
#undef int
int main(){
#define int ll
// freopen(".in","r",stdin);
n = read(), m = read();
for(int i = ; i <= n; ++i){
a[i].l = read();
a[i].r = read();
}
for(int i = ; i <= m; ++i){
bri[i].len = read();
bri[i].idx = i;
}
for(int i = ; i < n; ++i){
b[i].max = a[i+].r - a[i].l;
b[i].min = a[i+].l - a[i].r;
b[i].idx = i;
}
sort(b+,b+n,comp_dist);
sort(bri+,bri+m+,comp_bridge);
int lst = ;
for(int i = ; i <= m; ++i){
if(cnt >= n-) break;
while(lst < n && b[lst].min <= bri[i].len && bri[i].len <= b[lst].max){
q.push(b[lst]);
++lst;
}
if(!q.size()) continue;
Dist tmp = q.top();
q.pop();
if(bri[i].len <= tmp.max){
ans[tmp.idx] = bri[i].idx;
++cnt;
}
else{
printf("No");
return ;
}
}
if(cnt < n-){
printf("No");
return ;
}
printf("Yes\n");
for(int i = ; i < n; ++i){
printf("%lld ", ans[i]);
}
return ;
}

Codeforces555 B. Case of Fugitive的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Codeforces 556D - Case of Fugitive

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

  4. Codeforces 555 B. Case of Fugitive

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

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

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

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

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

  7. codeforces 555B Case of Fugitive

    题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...

  8. CF555B Case of Fugitive

    题目大意 有一些不相交线段和一些桥,桥可以架在两个相邻的线段上.求现有的桥是否可以使所有线段连通. 题解 在两个线段上架桥,桥的长度在一个范围内,相当于一个长度的区间,一个桥只有一个长度,相当于一个长 ...

  9. CF555B 贪心

    http://codeforces.com/problemset/problem/555/B B. Case of Fugitive time limit per test 3 seconds mem ...

随机推荐

  1. 【JS复习笔记】03 继承(从ES5到ES6)

    前言 很久以前学习<Javascript语言精粹>时,写过一个关于js的系列学习笔记. 最近又跟别人讲什么原型和继承什么的,发现这些记忆有些模糊了,然后回头看自己这篇文章,觉得几年前的学习 ...

  2. 常用yum操作命令

    1.yum repolist 获取当前系统有效的repolist,如下图 2.yum list,列出所有可安装的软件包 获取当前有效repolist中所能安装的所有rpm包列表,(很长,慎重),可以结 ...

  3. Dapper-小型ORM之王(C#.NET)

    ORM:对象关系映射器,它直接将数据库映射到C#对象. 有很多ORM框架可用,Dapper是其中之一,被称为ORM之王. 下面是Dapper主要的一些功能: 速度快,性能好; 更少的代码行 对象映射 ...

  4. pycharm导入自己写的.py文件时,模块下方出现红色波浪线解决

    点击菜单栏的File,选择Setting, 然后,选择需要导入的.py文件“所在的目录",而非项目根目录,右键 之后再导入该.py文件就不会出现红色波浪线了.

  5. git出现: not a git repository

    使用用git add . 出现这样错误: fatal: not a git repository (or any of the parent directories): .git 意思是说:.git没 ...

  6. sql 查询优化小计

    好久没更博了,偷偷的抽时间写一下. 早上开始working的时候,发现一个页面加载很慢,经排查是昨天写的一条联合查询的sql导致的.于是着手优化! 首先想到的是在join的时候,减少表体积之后再进行关 ...

  7. redis 的使用,及如何使用redis维护数亿人的登录状态

    一.redis中几个常用的方法 redis的使用场景移步本文 select db redis 下默认有有16个表,0~15可以通过:select 2 或者 select 11这样的方式切换表 keys ...

  8. 用WSDL4J解析types标签中的内容

    WSDL4J是一种用来解析WSDL文本的常用工具. 但网络上用WSDL4J来解析wsdl文档complexType标签中内容的问题一大堆也没有有效的解决方法.今天在我“遍历”wsdl4j的api文档和 ...

  9. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bw.mapper.BillMapper.getBillList at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225

    这个错误是没有找到映射文件 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.b ...

  10. 将Vue移动端项目打包成手机app---HBuilder

    将移动端页面打包成app 1.使用 HBuilder 直接编译打包 点击左上角 文件>打开目录>选择目录  选择用Webpack打包好的dist文件目录 由于我添加到项目了,所以会显示该项 ...