一、题目内容
CodeForces:556D Case of Fugitive 原题地址
二、题意解释
有N个岛屿,每个岛屿的左端点和右端点已知,此时有m种桥,其长度已知,问能否用这些桥连接起来所有的岛屿。
三、代码及注释
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std
;
typedef long long ll
;
int n
,m
;
struct N
{
ll x
,y
;
} node
[200005];
struct F
{
ll mi
,ma
;
int idx
;
bool used
;
bool operator < (const F
&A
) const
{
return mi
<A
.mi
;
}
} fanwei
[200005];
struct Bridge
{
ll len
;
int idx
;
bool operator < (const Bridge
&A
) const
{
return len
<A
.len
;
}
} bridge
[200005];
struct cmp
{
bool operator()(const int a
,const int b
)
{
return fanwei
[a
].ma
>fanwei
[b
].ma
;
}
};
int ans
[200005];
int main()
{
scanf("%d%d",&n
,&m
);
for(int i
=0; i
<n
; i
++)
{
scanf("%lld%lld",&node
[i
].x
,&node
[i
].y
);
}
for(int i
=0; i
<m
; i
++)
{
scanf("%lld",&bridge
[i
].len
);
bridge
[i
].idx
=i
+1;
}
for(int i
=0; i
<n
-1; i
++)
{
fanwei
[i
].mi
=node
[i
+1].x
-node
[i
].y
;
fanwei
[i
].ma
=node
[i
+1].y
-node
[i
].x
;
fanwei
[i
].idx
=i
;
fanwei
[i
].used
=false;
}
sort(bridge
,bridge
+m
);
sort(fanwei
,fanwei
+n
-1);
int size
=0;
priority_queue
<int,vector
<int>,cmp
> Q
;
for(int i
=0;i
<m
;i
++){
for(int j
=0;j
<n
-1&&fanwei
[j
].mi
<=bridge
[i
].len
;j
++){
Q
.push(j
);
}
while(!Q
.empty()){
int tmp
=Q
.top();
Q
.pop();
if(fanwei
[tmp
].ma
>=bridge
[i
].len
&&fanwei
[tmp
].used
==false){
ans
[fanwei
[tmp
].idx
]=bridge
[i
].idx
;
fanwei
[tmp
].used
=true;
size
++;
break;
}
}
}
if(size
!=n
-1)
{
printf("No\n");
}
else if(size
==n
-1)
{
printf("Yes\n");
for(int i
=0; i
<size
-1; i
++)
{
printf("%d ",ans
[i
]);
}
printf("%d\n",ans
[size
-1]);
}
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-1541.html