zoukankan      html  css  js  c++  java
  • hud 1051 Wooden Sticks

    #include<stdio.h>
    #include<iostream>
    #include<cstdlib>
    using namespace std;
    struct st
    {
        int l;
        int r;
    } a[5003];
    int visit[5002];
    int cmp(const void *a,const void *b)
    {
        struct st *p=(st*)a;
        struct st *q=(st*)b;
        if(p->l!=q->l)
            return p->l-q->l;
        else 
            return p->r-q->r;
    }
    int main()
    {
        int i,j,n,t,temp,num;
        while(scanf("%d",&t)>0)
        {
            while(t--)
            {
                scanf("%d",&n);
                for(i=1;i<=n;i++)
                    scanf("%d%d",&a[i].l,&a[i].r);
                qsort(a,n+1,sizeof(a[1]),cmp);
                for(i=1;i<=n;i++)
                    visit[i]=0;
                for(i=1,num=0;i<=n;i++)
                    if(visit[i]==0)
                    {
                        num++;
                        temp=a[i].r;
                        for(j=i+1;j<=n;j++)
                            if(visit[j]==0&&temp<=a[j].r)
                            {
                                visit[j]=1;
                                temp=a[j].r;
                            }
                    }
                printf("%d\n",num);
    
            }
        }
        return 0;
    }

    http://acm.hdu.edu.cn/showproblem.php?pid=1051

    特殊的例子

    5          
    1 3
    1 5
    1 2
    1 1
    1 9 

    1

    看到了这个题想起了hdu的这个题

    http://acm.nyist.net/JudgeOnline/problem.php?pid=236

    用上面的代码超时,~_~。把while(scanf("%d",&t)>0) 改成scanf("%d",&t);过了。汗。。。

    需要二级排序,看了以前写的代码,用了一级排序,过了,这组数据不对。后来用才知道用二级。又汗,以前居然过了。~

    有个问题值得思考一下,

    当经过二级排序后

    a[i].r的值分别是 1 9 2 3 4

    是1 9 和2 3 4 呢,还是1 2 3 4和9 ?

    如果按照上面的方法用贪心,那就是 1 9 和2 3 4两组,但是如果用最长递增那就是 1 2 3 4 和9 了,结果对么?

    嘿嘿。

  • 相关阅读:
    NOIP 转圈游戏
    NOIP 2012 同余方程
    BZOJ3864 Hero meet devil
    HDU3045 Picnic Cows
    「PKUWC2018」随机算法
    CF543E Listening to Music
    CF833E Caramel Clouds
    「PKUWC2018」Slay the Spire
    Luogu2183【国家集训队】礼物
    CF932E Team Work
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3010356.html
Copyright © 2011-2022 走看看