hdu 1051 Wooden Sticks

本文介绍了一种基于棍子长度进行排序的算法实现,并通过C++代码详细展示了算法的具体步骤。该算法首先对棍子按特定规则排序,然后从最小的棍子开始,寻找能够满足条件的后续棍子,直至所有棍子都被正确排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析:排序后,从小找到大,注意保存第一个不满足条件的stick,作为第二次查找的开始

#include<iostream>
#include<algorithm>
using namespace std;
const int NM=5005;
struct Stick{
	int x,y;
}st[NM];


bool comp(struct Stick A,struct Stick B)
{
	if(A.x<B.x) return 1;
	else
	{
		if(A.x==B.x)
		{
			if(A.y<B.y) return 1;
			else return 0;
		}
		else return 0;
	}
}


int main()
{
	int sx,sy,ex,ey,time,n,i,T,ans;
	cin>>T;
	while(T--)
	{
		cin>>n;
		for(i=0;i<n;i++)
			cin>>st[i].x>>st[i].y;
		if(n==1) {cout<<1<<endl;continue;}
		sort(st,st+n,comp);
		
		ex=st[0].x,ey=st[0].y;
		ans=1;
		time=0;
		
		while(ans<n)
		{
			time++;
			sx=ex,sy=ey;
			ex=ey=1000000;
			for(i=1;i<n;i++)
			{
				if(st[i].x>0&&st[i].x>=sx&&st[i].y>=sy)
				{
					sx=st[i].x,sy=st[i].y;
					st[i].x=0,st[i].y=0;
					ans++;
				}
				else 
				{
					if(st[i].x<ex)
						ex=st[i].x,ey=st[i].y;
				}
			}	
		}
		cout<<time<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值