NYOJ236 贪心算法

题目大意:

传送门

我的理解:

首先进行对木棒数组进行排序,从小到大,长度更小的排在前面,长度相同的质量更小的排在前面。采用贪心算法。

具体在代码注释;

我的代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX_N 5005

using namespace std;

typedef pair<int,int> P;
P wood[MAX_N];
int tag[MAX_N];

bool cmp(P a,P b){  
    if(a.first<b.first) return 1;  
    else if(a.first==b.first&&a.second<b.second) return 1;  
    return 0;  
} 

int main()
{
	freopen("D:/OJ/挑战程序设计竞赛/NYOJ236.txt","r",stdin);
	int T;
	cin>>T;
	int n;
	while(T-- != 0){
		int result = 0;
		memset(tag,0,sizeof(tag));
		cin>>n;
		for(int i = 0;i<n;i++){
			cin>>wood[i].first>>wood[i].second;
		}
		sort(wood,wood+n,cmp);
		for(int i = 0;i<n;i++){
			if(tag[i] == 0){
				result++;		//相当于重启一次机器 
				int weight = wood[i].second;
				for(int j = i+1;j<n;j++){		//重启机器后,遍历以后的数组,找到还未被访问的以及不用重启机器就能投入作业的木棒 
					if(tag[j] == 0 && wood[j].second>=weight){
						tag[j] = 1;
						weight = wood[j].second;
					}
				}
			}
			
		}
		cout<<result<<endl;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值