nyoj 236 心急的c小加【贪心】



心急的C小加

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
描述

C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间,如果第i+1个木棒的重量和长度都大于等于第i个处理的木棒,那么将不会耗费时间,否则需要消耗一个单位的时间。因为急着去约会,C小加想在最短的时间内把木棒处理完,你能告诉他应该怎样做吗?

输入
第一行是一个整数T(1<T<1500),表示输入数据一共有T组。
每组测试数据的第一行是一个整数N(1<=N<=5000),表示有N个木棒。接下来的一行分别输入N个木棒的L,W(0 < L ,W <= 10000),用一个空格隔开,分别表示木棒的长度和质量。
输出
处理这些木棒的最短时间。
样例输入
3 
5 
4 9 5 2 2 1 3 5 1 4 
3 
2 2 1 1 2 2 
3 
1 3 2 2 3 1 
样例输出
2
1
3
分析:
先将长度从小到大排序,如果长度相等就按照重量从小到大排序,然后比较重量的大小,如果后者重量比前者大,则时间++。
前面俩个代码都是自己不细心,造成的,最后一个是对的。
代码:
/*
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int N=5050;
struct node{
    int l;
    int w;
}pp[N];
bool cmp(node a,node b)
{
    if(a.l>b.l)
        return true;
    if(a.l==b.l&&a.w>b.w)
        return true;
        return false;
}
int main()
{
    int t,n,cnt;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&pp[i].l,&pp[i].w);
        sort(pp,pp+n,cmp);
        cnt=0;
        for(int i=0;i<n;i++)
        {
            if(pp[i].w==0)
                continue;
                ++cnt;
            for(int j=i+1;j<n;j++)
            {
                if(pp[j].w>=pp[i].w)
                    pp[i].w=pp[j].w;
                pp[j].w=0;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;

struct Node{
	int len, weight;
}a[5002];

bool cmp(Node x, Node y){
	if(x.len < y.len) return true;
	if(x.len == y.len && x.weight < y.weight) return true;
	return false;
}

int main(){
	int t, n, count;
	Node temp;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		for(int i = 0; i < n; ++i){
			scanf("%d%d", &a[i].len, &a[i].weight);
		}

		sort(a, a + n, cmp);

		count = 0;
		for(int i = 0; i < n; ++i){
			if(a[i].weight == 0) continue;
			++count;
			for(int j = i + 1; j < n; ++j){
				if(a[j].weight >= a[i].weight){
					a[i].weight = a[j].weight;
					a[j].weight = 0;
				}
			}
		}
		printf("%d\n", count);
	}
	return 0;
}
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
   int l;
   int w;
};
bool cmp(node a,node b)
{
    if(a.l<b.l)
        return true;
    if(a.l==b.l&&a.w<b.w)
        return true;
    return false;
}

int main()
{
    int t;
    node pp[5050];
    scanf("%d",&t);
    while(t--)
    {
        int n,m,cnt;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d%d",&pp[i].l,&pp[i].w);
        sort(pp,pp+n,cmp);
        cnt=0;
        for(int i=0;i<n;++i)
        {
            if(pp[i].w==0)
                continue;
            ++cnt;
            for(int j=i+1;j<n;++j)
                {
                    if(pp[j].w>=pp[i].w){
                        pp[i].w=pp[j].w;
                    pp[j].w=0;}
                }
        }
        printf("%d\n",cnt);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值