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. (转)C#中的那些全局异常捕获

    C#中的那些全局异常捕获(原文链接:http://www.cnblogs.com/taomylife/p/4528179.html)   1.WPF全局捕获异常       public partia ...

  2. android linux 传文件

    EStrongs File Explorer 即: Es文件浏览器 网络 -> 远程管理器 设置 ->设置根目录 linux 使用浏览器访问即可.

  3. Pytest+Allure定制报告

    前言: 最近在研究接口自动化的框架,好的测试报告在整个测试框架起到至关重要的部分.终于被我发现一个超好用的报告框架,不仅报告美观,而且方便CI集成. 就是它,就是它:Allure Test Repor ...

  4. 机器学习第一篇——最近邻kNN

    机器学习监督学习中,根据解决问题的连续性和离散型,分为分类问题和回归问题.最邻近算法kNN是一种最为直接和简便的分类方法. kNN本质上,是计算目标到模型的欧式距离,从而判定目标所属的类别. 首先,在 ...

  5. java web石家庄铁道大学课程管理系统

    package kecheng Kc.java package kecheng; public class Kc { private int id; private String classname; ...

  6. mysql-SQL Error: 1205, SQLState: 41000

    mysql-SQL Error: 1205, SQLState: 41000——CSDN问答频道https://ask.csdn.net/questions/176492 mysql-SQL Erro ...

  7. CentOS查看和修改PATH环境变量的方法 profile

    https://blog.csdn.net/dongheli/article/details/83987092

  8. Pyspider上手

    pyspider安装: pip3 install Pyspider 启动服务操作 1.打开cmd:输入        pyspider  --help 回车,可以查看帮助信息,pyspider all ...

  9. 使用PSR-4配合composer autoload 自动加载文件夹

    require 文件很麻烦,使用PSR-4搭配composer一次加载,终生受用. 感觉类似java中的import了,自己先记录一下最近理解的. 用composer管理自己的包吧 安装compose ...

  10. Oracle 用户管理与权限分配

    用户管理是系统管理员最基本的任务之一,用户想要连接数据库并且使用相应的系统资源就必须是系统的合法用户且具有对应的权限. 1 创建用户 default tablespace default_tables ...