zoukankan      html  css  js  c++  java
  • HD-ACM算法专攻系列(21)——Wooden Sticks

    题目描述:

    AC源码:

    此题考查贪心算法,解题思路:首先使用快速排序,以w或l按升序排序(注意相等时,应按另一值升序排序),这样就将二维变量比较,变为了一维的,排好序的一边就不需要去管了,只需要对未排序的一边直接进行贪心遍历。时间复杂度O(n^2)

    #include"iostream"
    #include"algorithm"
    using namespace std;
    
    struct Stick
    {
        int l;
        int w;
        bool processed;
    };
    
    bool cmp(Stick a, Stick b)
    {
        
        if(a.l < b.l)
        {
            return true;
        }
        else if(a.l == b.l)
        {
            return a.w < b.w;
        }
        else
        {
            return false;
        }
    }
    
    int main()
    {
        int t, n, total, val;
        Stick s[5000];
        scanf("%d", &t);
        for(int i = 0; i < t; i++)
        {
            scanf("%d", &n);
            total = 0;
            for(int j = 0; j < n; j++)
            {
                scanf("%d %d", &s[j].l, &s[j].w);
                s[j].processed = false;
            }
            sort(s, s+n, cmp);
            for(int j = 0; j < n; j++)
            {
                if(!s[j].processed)
                {
                    total++;
                    val = s[j].w;
                    s[j].processed = true;
                    for(int k = j + 1; k < n; k++)
                    {
                        if(!s[k].processed && s[k].w >= val)
                        {
                            val = s[k].w;
                            s[k].processed = true;
                        }
                    }
                }
            }
            printf("%d
    ", total);
        }
        return 0;
    }
    

      

  • 相关阅读:
    POJ 2209
    POJ 2196
    POJ 2215
    POJ 2192
    POJ 2195
    POJ 2181
    POJ 2182
    POJ 2159
    POJ 2153
    字符设备驱动 —— 字符设备驱动框架
  • 原文地址:https://www.cnblogs.com/forcheng/p/7634905.html
Copyright © 2011-2022 走看看