• 心急的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

    首先排序,然后在考虑长度的基础上遍历处理掉当前可以处理的并标记,然后继续遍历,遇见没被标记的就cnt++,再次在该点遍历一遍,注意更改当前处理的最大重量
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN  5003
    #define INF 1000000009
    /*
        长度重量都大于当前的 那么无花费
        否则花费为1
    */
    struct node
    {
        int w, l;
    };
    bool cmp(node a, node b)
    {
        if (a.l == b.l) return a.w < b.w;
        return a.l < b.l;
    }
    node a[MAXN];
    bool been[MAXN];
    int T, n;
    int main()
    {
        scanf("%d", &T);
        while (T--)
        {
            memset(been, false, sizeof(been));
            scanf("%d", &n);
            for (int i = 0; i < n; i++)
                scanf("%d%d", &a[i].l, &a[i].w);
            sort(a, a + n, cmp);
            int cnt = 0;
            for (int i = 0; i < n; i++)
            {
                if (!been[i])
                {
                    cnt++;
                    int tmp = a[i].w;
                    for (int j = i + 1; j < n; j++)
                    {
                        if (!been[j] && a[j].w >= tmp)
                        {
                            been[j] = true;
                            tmp = a[j].w;
                        }
                    }
                }
            }
            printf("%d
    ", cnt);
        }
        return 0;
    }
  • 相关阅读:
    Javadoc注释的用法
    Java 和 Android系统环境变量设置
    [转载]Android开发新浪微博客户端 完整攻略 [新手必读]
    eclipse中Android程序字符编码不统一的解决方案
    用word2007写blog时表格的显示效果
    解决Vista中的文件关联图标问题。
    用word2007在博客园发布带图片的blog
    在c++中使用gotoxy
    动态多线程任务管理
    几个视频切割工具
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6816129.html
Copyright © 2020-2023  润新知