天梯赛 L2-014 列车调度

L2-014 列车调度

解析参考:PTA 7-13 列车调度(二分查找)

最初没有看出列末数字的有序性,做了模拟,超时21分

正确做法是二分


模拟代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5;
int n;
queue<int> q[N];
int cnt;//到第几个队列
int t;//空出来几个队列
map<int,int> tail;//队列末尾数
map<int,int> head;//队头数对应队列
int now;//该出哪个数了 
int res;
int main()
{
	cin>>n;now=n;
	while(n--)
	{
		int x;cin>>x;
		if(now==x)
		{
			cnt++;
			res=max(res,cnt-t);
			t++;
			res=max(res,cnt-t);
			now--;
			while(head[now])
			{
				int pos=head[now];
				q[pos].pop();
				head[now]=0;
				if(q[pos].empty())
				{
					t++;
					tail[pos]=0;
				}
				else
				{
					head[q[pos].front()]=pos;
				}
				now--;
				res=max(res,cnt-t);
			}
			continue;
		}	
		bool f=false;	
		for(int i=1;i<=cnt;i++)
		{
			if(tail[i]&&tail[i]>x)
			{
				tail[i]=x;
				q[i].push(x); 
				f=true;
				break;
			}
		}
		if(!f)
		{
			cnt++;
			q[cnt].push(x);
			head[x]=cnt;
			tail[cnt]=x;
		}
		res=max(res,cnt-t);
	}
	cout<<res; 
//	cout<<cnt<<" "<<t;
}

二分代码:

#include<bits/stdc++.h>
using namespace std;
int n;
int s[100005];
int t=0;
int main()
{
	cin>>n;
	while(n--)
	{
		int x;cin>>x;
		int l=1,r=t;
		while(l<r)
		{
			int mid=l+r>>1;
			if(s[mid]>x) r=mid;
			else l=mid+1;
		}
		if(s[l]>x) s[l]=x;
		else s[++t]=x;
	}
	cout<<t<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vic.GoodLuck

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值