Criss-Cross Cables (贪心+优先队列)

As a participant in the BAPC (Bizarrely Awful Parties Competition) you are preparing for your next show. Now, you do not know anything about music, so you rip off someone else’s playlist and decide not to worry about that any more. What you do worry about, though, is the aesthetics of your setup: if it looks too simple, people will be unimpressed and they might figure out that you are actually a worthless DJ.

It doesn’t take you long to come up with a correct and fast solution to this problem. You add a long strip with a couple of useless ports, and add some useless cables between these ports. Each of these cables connects two ports, and these special ports can be used more than once. Everyone looking at the massive tangle of wires will surely be in awe of your awesome DJ skills.

However, you do not want to connect the same two ports twice directly. If someone notices this, then they will immediately see that you are a fraud!

You’ve made a large strip, with the ports in certain fixed places, and you’ve found a set of cables with certain lengths that you find aesthetically pleasing. When you start trying to connect the cables, you run into another problem. If the cables are too short, you cannot use them to connect the ports! So you ask yourself the question whether you’re able to fit all of the cords onto the strip or not. If not, the aesthetics are ruined, and you’ll have to start all over again.

Input

The first line has two integers 2≤n≤5⋅1052≤n≤5⋅105 and 1≤m≤5⋅1051≤m≤5⋅105, the number of ports on the strip and the number of wires.

The second line has nn integers 0≤x1<⋯<xn≤1090≤x1<⋯<xn≤109, the positions of the sockets.

The third line has mm integers l1,…,lml1,…,lm, the lengths of the wires, with 1≤li≤1091≤li≤109.

Output

Print yes if it is possible to plug in all the wires, or no if this is not possible.

Sample Input 1
4 4
0 2 3 7
1 3 3 7

Sample Output 1
yes

Sample Input 2
3 4
0 1 2
10 10 10 10

Sample Output 2
no

题意:第一行给定n个点和m条绳子,第二行是点的位置(从小到大排列),第三行是绳子的长度。问能不能把所有的绳子都连进去(绳子的长度>=点之间的距离才可以连,且相同的两个点之间不能连两条绳子)。

思路贪心,n个点共可以连n*(n-1)/2条边,越短的绳子肯定要对应距离越小的边,那么我们从小到大遍历绳子,每次找出里面最短的边,与当前遍历到的的绳子长度进行比较,如果当前遍历到的绳子比目前能连的最短的边的长度还小,那么肯定不能满足题目要求,直接跳出,否则的话进行下一次循环,直到遍历完所有绳子。如果能遍历完,那么满足了要求。

循环的过程可以用优先队列实现,每次找出目前最短的边,然后把它向右扩展一个长度放入队列。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N =5e5+10;
ll b[N],a[N],x,pre;
int m,n,cnt=1;
struct node
{
	int l,r;
	ll val;
	bool operator < (const node x) const
	{
		return val>x.val;
	}

};
priority_queue<node> q;
bool solve()
{
	if(m>n*(n-1)/2)
		return false;
	for(int i=2;i<=n;i++)
		q.push({i-1,i,a[i]-a[i-1]});
	while(cnt<=m)
	{
		node tem=q.top();
		q.pop();
		int l=tem.l,r=tem.r,x=tem.val;
		if(b[cnt]<x)
			return false;
		cnt++;
		if(r<n)
			q.push({l,r+1,a[r+1]-a[l]});
		
	}
	return true;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1;i<=m;i++)
		scanf("%lld",&b[i]);
	sort(b+1,b+m+1);
	if(!solve())
		printf("no\n");
	else
		printf("yes\n");
	return 0;
 } 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值