专栏文章

P5661 题解

P5661题解参与者 6已保存评论 7

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
7 条
当前快照
1 份
快照标识符
@miqnu6s5
此快照首次捕获于
2025/12/04 07:50
3 个月前
此快照最后确认于
2025/12/04 07:50
3 个月前
查看原文

思路

不难想到暴力思路:每次循环判断每一张优惠券是否在规定时间内。然而这样做的时间复杂度为 O(n2)\mathcal{O}(n^2),不可行。
考虑队列优化,每次判断时把前面已经超过 4545 分钟的优惠券弹掉。这样做,时间复杂度降到了 O(n)\mathcal{O}(n),可以通过此题。
AC CODE
CPP
#include<bits/stdc++.h>
using namespace std;
#define front _front
#define back _back//防止重名
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=1e5+10;
struct node{
	bool type;//类型
	int price,t;//命名如题意
}q[N];
int front,back=1;
int main(){
	int n=read(),ans=0;
	for(int i=1;i<=n;++i){
		node temp={(bool)read(),read(),read()};
		if(temp.type){//公交车
			bool flag=false;
			for(int j=back;j<=front;++j)
				if(temp.t-q[j].t<=45){//队列优化判断条件
					if(temp.price<=q[j].price){
						q[j].price=0;
						flag=true;
						break;
					}
				}
				else back=j;//弹掉不符合条件的优惠券
			if(!flag)
				ans+=temp.price;
		}
		else{//地铁
			ans+=temp.price;
			q[++front]=temp;
		}
	}
	printf("%d\n",ans);
	return 0;
}

评论

7 条评论,欢迎与作者交流。

正在加载评论...