Codeforces555 B. Case of Fugitive
出处: 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的更多相关文章
- 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 ...
- 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 ...
- Codeforces 556D - Case of Fugitive
556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/s ...
- Codeforces 555 B. Case of Fugitive
\(>Codeforces \space 555 B. Case of Fugitive<\) 题目大意 : 有 \(n\) 个岛屿有序排列在一条线上,第 \(i\) 个岛屿的左端点为 \ ...
- CodeForces - 556D Case of Fugitive (贪心+排序)
Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet ...
- codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)
题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...
- codeforces 555B Case of Fugitive
题目连接: http://codeforces.com/problemset/problem/555/B 题目大意: 有n个岛屿(岛屿在一列上,可以看做是线性的,用来描述岛屿位置的是起点与终点),m个 ...
- CF555B Case of Fugitive
题目大意 有一些不相交线段和一些桥,桥可以架在两个相邻的线段上.求现有的桥是否可以使所有线段连通. 题解 在两个线段上架桥,桥的长度在一个范围内,相当于一个长度的区间,一个桥只有一个长度,相当于一个长 ...
- CF555B 贪心
http://codeforces.com/problemset/problem/555/B B. Case of Fugitive time limit per test 3 seconds mem ...
随机推荐
- CISCO交换机-SNMP配置
1.1 SNMP基础配置 router> enable 进入路由器是用户模式 router# conf terminal 进入路由器的全局配置模式 #snmp-server commun ...
- Linux 命令(二)
man help:线上查询及帮助命令 命令 --help:简单帮助 help cd:查看一些Linux命令行的一些内置命令 文件和目操作命令(19个) ls cd cp find mkdi ...
- AtCoder Beginner Contest 122 D - We Like AGC (DP)
D - We Like AGC Time Limit: 2 sec / Memory Limit: 1024 MB Score : 400400 points Problem Statement Yo ...
- Largest Rectangle in a Histogram HDU - 1506 (单调栈)
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...
- PS调出唯美冷色情侣婚纱写真照
一.打开PS原片,原片是一张JPG格式的片子 色温较高整个画面较红离对着上面的我们标准的韩式色调我们来进行调节吧 ,我就不打太多文字解释一些基本常规了 二.韩式婚纱内景喜欢加点烟雾.其实我本人是不太喜 ...
- 环同态p64推论
1.为什么属于f(x)∈f(I),那么 2.为什么x属于ker,那么f(x)属于f(I)?
- MySQL数据库性能优化思路与解决方法(二转)
原文:http://bbs.landingbj.com/t-0-242512-1.html 1.锁定表 尽管事务是维护数据库完整性的一个非常好的方法,但却因为它的独占性,有时会影响数据库的性能,尤其是 ...
- 关于PHP批量图片格式转换的问题--本文转成webp, 其他过程格式一样
最近要把项目中的图片全部生成webp格式, 过程整理一下, (直接存在本地,或者图片链接存在数据库都可以看看) 首先,肯定是批量处理, 一个php处理不了这么多, 会爆内存的, 个人建议用aja ...
- react组件选项卡demo
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Day 4-2 time & datetime模块
time模块. import time time.time() #输出: 1523195163.140625 time.localtime() # 获取的是操作系统的时间,可以添加一个时间戳参数 # ...