2023牛客寒假算法基础集训营5(优先队列)

 

 

 

 

思路:首先我们每次都需要输出在每次得到道具的时候能通过多少关,并且一共最多就1e5个区间,那么我们可以把需要合并的区间合并起来,但是如果合并的时候遍历所有的区间那么时间就很可能超时,那么我们使用一个优先队列,左区间按从小到大排序,然后每次取前面的两个区间,看能不能合并,如果能就合并,不能就直接退出循环

那么我们在当前情况下能通过的关卡数就是队列的第一个区间,能通关的关卡数也就可以得到了,实行的操作数也不会很多,每次比较两个人能通过的关卡数然后比较一下就可以得到答案了。

然后具体的实现可以用优先队列+pair,然后优先队列的排序默认的就是第一个元素从小到大排序,所以不需要进行过多的操作。

代码:

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define sc_int(x) scanf("%d", &x)
#define sc_ll(x) scanf("%lld", &x)
#define pr_ll(x) printf("%lld", x)
#define pr_ll_n(x) printf("%lld\n", x)
#define pr_int_n(x) printf("%d\n", x)
#define ll long long 
using namespace std;

const int N=1000000+100;
int n ,m,h;
int s[N];
typedef pair<int,int>PII;

struct lk{
	int l;
	int r;
}cnt[N],bnt[N];

void slove( )
{
	int t;
	sc_int(n);

	for(int i =1;i<=n;i++){
		sc_int(cnt[i].l),sc_int(cnt[i].r);
	}
	for(int i =1;i<=n;i++){
		sc_int(bnt[i].l),sc_int(bnt[i].r);
	}

	priority_queue<PII,vector<PII>,greater<PII>>q,p;
	for(int i=1;i<=n;i++)
	{
		q.push({cnt[i].l,cnt[i].r}),p.push({bnt[i].l,bnt[i].r});
		while(q.size()>=2)
		{
			PII A,B,C;
			A=q.top();
			q.pop();
			B=q.top();
			q.pop();
			if(A.second+1>=B.first)
			{
				C.first=A.first,C.second=max(B.second,A.second);
				q.push(C);
			}
			else {
            q.push(A),q.push(B);
            break;
            }
		}
		while(p.size()>=2)
		{
			PII A,B,C;
			A=p.top();
			p.pop();
			B=p.top();
			p.pop();
			if(A.second+1>=B.first)
			{
				C.first=A.first,C.second=max(B.second,A.second);
				p.push(C);
			}
			else {
            p.push(A),p.push(B);
            break;
            }
		}

		int sum1=0,sum2=0;
		sum1=q.top().second-q.top().first+1,sum2=p.top().second-p.top().first+1;
		if(q.top().first!=1)sum1=0;
		if(p.top().first!=1)sum2=0;
       // cout<<sum1<<" "<<sum2<<endl;
		if(sum1>sum2) cout<<"sa_win!\n"<<sum1-sum2<<endl;	
		else if(sum2>sum1)cout<<"ya_win!\n"<<sum2-sum1<<endl;
		else cout<<"win_win!\n"<<0<<endl;
	}
}

int main()
{
	int t;
// 	sc_int(t);
// 	while(t--)
	slove();


	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值