NYOJ+贪心+貌似有点偏序的原理,题目idea想错了,多花点时间再想想额

点击打开链接
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<cstdio>
using namespace std;
///主要考察贪心算法,还有涉及了Dilworth定理(偏序<=)。具体方法,
///第一步,将木块按照长度上升排序,如果,长度相等,那么,按照重量的大小升序排序。
///第二步,主要是贪心算法,Dilworth定理,因为,两个结点之间,当长度和重量都大于前面的结点,那么,符合我们的要求,
///所以,可以将已经排序号的序列,分成多个序列,这些序列是按照重量的大小来排序(如果长度相同,重量再从小到大,贪心才贪得好啊);
///然后尽可能多的找出符合要求的结点,这里主要用到了贪心算法,最后,我们只要统计有多少个序列,就是得到的结果。,


struct Block{
	int length;
	int weight;
};
Block a[5000];
bool cmp(Block m, Block n)
{
	if(m.length < n.length) return true;
	else if(m.length == n.length && m.weight < n.weight) return true;
	else return false;
}
int main()
{
	int T,N,i=0,j=0;
	cin>>T;
	while(T--){
		cin>>N;
		for(i = 0; i < N; i++){
			cin>>a[i].length>>a[i].weight;
		}
		sort(a,a+N,cmp);
		int sum = 0;
		for(i=0;i<N;i++){
            int t;
            if(a[i].weight!=0){
                sum++;
                int t=a[i].weight;
                for(j=i+1;j<N;j++){///找出在重量是增长的,这些都是长度和重量比之前大的,标记后,下次不用花费额。
                    if(a[j].weight>=t){
                        t=a[j].weight;
                        a[j].weight=0;
                    }
                }
            }
		}
		printf("%d\n",sum);
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值