30分,有没有大佬帮忙看看程序(请无视freopen))
查看原帖
30分,有没有大佬帮忙看看程序(请无视freopen))
543350
lywusichen楼主2023/9/29 17:31
#include<iostream>
#include<stdio.h>
using namespace std;
int n,cost;
struct sub
{
	sub *back;
	sub *next;
	int t;
	int price;
};
sub *head,*p,*r;
int main()
{
	freopen("P5661_1.in","r",stdin);
	freopen("P5661_1.out","w",stdout);
	cin>>n;
	head=new sub;
	head->back=NULL;
	r=head;
	for(int i=0;i<n;i++)
	{
		int tpe;
		cin>>tpe;
		if(tpe==0)
		{
			p=new sub;
			cin>>p->price>>p->t;
			r->next=p;
			p->back=r;
			p->next=NULL;
			r=p;
			cost+=p->price;
			if(p->t-(head->next->t)>45)//清除无效票 
			{
				head=head->next;
				head->back=NULL;
			}
		}
		else
		{
			int bus_price,bus_t;
			bool hpay=true;
			cin>>bus_price>>bus_t;
			if(head==r)//是否为空队 
			{
				cost+=bus_price;
			}
			else
			{
				p=head->next;
				while(p->next!=NULL)
				{
					if(bus_t-(p->t)<=45)
					{
						if(p->price>=bus_price)//使用票 
						{
							sub *nx=p->next,*bc=p->back;
							p->back->next=nx;
							p->next->back=bc;
							hpay=false;
							break;
						}
					}
					else//清除无效票 
					{
						head=p->back;
						head->back=NULL;
					}
					p=p->next;
				}
			}
			if(hpay)
			{
				if(bus_t-(p->t)<=45)
				{
					if(p->price>=bus_price)//使用票
					{
						r=p->back;
						r->next=NULL;
						hpay=false;
					}
				}
				else//清除无效票 
				{
					head=p->back;
					head->back=NULL;
				}
				if(hpay)//累计花费 
				{
					cost+=bus_price;
				}
			}
		}
	}
	cout<<cost;
	fclose(stdin);
	fclose(stdout);
	return 0;
}
2023/9/29 17:31
加载中...