POJ 1065 && HDU 1051 Wooden Sticks 贪心

http://blog.csdn.net/v5zsq/article/details/46790025

Description 

有一些木材和一台机器。机器每次加工一根木材需要的时间是1,但是当加工木材的长度和宽度都小于等于前一根木材的时候,不需要时间。求最少需要多少时间加工完所有的木材 
Input 
第一行为数据组数t,每组用例第一行为木材数量n,然后是每根木材的长度和宽度 
Output 
对于每组用例,输出最短加工时间 
Sample Input 


4 9 5 2 2 1 3 5 1 4 

2 2 1 1 2 2 

1 3 2 2 3 1 
Sample Output 



Solution 

 

由于切一根最长最宽的就可以不切比它短和窄的,反过来就是切一根最短最细的就不用切比它长和宽的,所以将木材升序排之后,从最短的开始切,用标记数组表示是否切此根木材,每切一根就把比它长和宽的标记,最后记录没被标记的木材即为需要切的木材数量 

 

 

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
const int maxn=100005;
const int INF=0x3f3f3f3f;
typedef long long LL;
using namespace std;
struct node
{
	int l,w;
}P[maxn];
bool cmp(node a,node b)
{
	return a.l==b.l?a.w<b.w:a.l<b.l;
}
int used[maxn]; //0表示切,1不切 
int main()
{
//	freopen("E:\\ACM\\test.txt","r",stdin);
	int T,N;
	cin>>T;
	while(T--)
	{
		cin>>N;
		memset(used,0,sizeof(used)); //初始化所有木块都要切 
		for(int i=0;i<N;i++) cin>>P[i].l>>P[i].w;
		
		sort(P,P+N,cmp);
		for(int i=0;i<N;i++)
		{
			if(used[i]) continue; //已经可以不用切的木块 
			int t=i;
			for(int j=i+1;j<N;j++)
			{
				if(!used[j]&&P[j].l>=P[t].l&&P[j].w>=P[t].w)  
				{
					t=j;
					used[j]=1; //这块木块可以不用切 
				}
			}
		}
		int ans=0;
		for(int i=0;i<N;i++) if(!used[i]) ++ans; //要切的木块就是答案 
		cout<<ans<<endl;
	}
	
	return 0;
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值