NYOJ1036 非洲小孩(又一道贪心水题,区间选点问题)

50 阅读2分钟

题目:

非洲小孩

时间限制: 1000 ms  |  内存限制: 65535 KB

难度: 2

    • 描述

    • 家住非洲的小孩,都很黑。为什么呢?
      第一,他们地处热带,太阳辐射严重。
      第二,他们不经常洗澡。(常年缺水,怎么洗澡。)
      现在,在一个非洲部落里,他们只有一个地方洗澡,并且,洗澡时间很短,瞬间有木有!!(这也是没有的办法,缺水啊!!)
      每个小孩有一个时间段能够洗澡。并且,他们是可以一起洗的(不管你是男孩是女孩)。
      那么,什么时间洗澡,谁应该来洗,由谁决定的呢?那必然是他们伟大的“澡”神啊。“澡”神有一个时间表,记录着该部落的小孩,什么时候段可以洗澡。现在,“澡”神要问你,一天内,他需要最少开启和关闭多少次洗澡的水龙头呢?因为,开启和关闭一次水龙头是非常的费力气的,即便,这也是瞬间完成的。\

        • 输入
        • 多组数据
          第一行一个0<n<=100。
          接下来n行,每行一个时间段。H1H1:M1M1-H2H2:M2M2,24小时制。
          保证该时间段是在一天之内的。但是,不保证,H1H1:M1M1先于H2H2:M2M2。
        • 输出
        • 题目描述,“澡”神最少需要开启和关闭多少次水龙头呢?
        • 样例输入
        • 1
          00:12-12:12
          2
          00:12-12:12
          14:00-12:00
          
        • 样例输出
        • 1
          1
          

代码:

代码1(错误版本):

#include <stdio.h>
#include <algorithm>
using namespace std;
struct time
{
    int h1;
    int m1;
    int h2;
    int m2;
} a[100];
bool cmp(time x,time y)
{
    if(x.h2!=y.h2)
        return x.h2<y.h2;
    else
        return x.m2<y.m2;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0; i<n; i++)
            scanf("%d:%d-%d:%d",&a[i].h1,&a[i].m1,&a[i].h2,&a[i].m2);
        for(int i=0; i<n; i++)
        {
            //printf("%d:%d-%d:%d\n",a[i].h1,a[i].m1,a[i].h2,a[i].m2);
            if(a[i].h1>a[i].h2)
            {
                int q;
                q=a[i].h1;
                a[i].h1=a[i].h2;
                a[i].h2=q;
                q=a[i].m1;
                a[i].m1=a[i].m2;
                a[i].m2=q;
            }
            if(a[i].h1==a[i].h2&&a[i].m1>a[i].m2)
            {
                int q;
                q=a[i].h1;
                a[i].h1=a[i].h2;
                a[i].h2=q;
                q=a[i].m1;
                a[i].m1=a[i].m2;
                a[i].m2=q;
            }
        }
        sort(a,a+n,cmp);
        printf("\n");
//        for(int i=0;i<n;i++)
//            printf("%d:%d-%d:%d\n\n",a[i].h1,a[i].m1,a[i].h2,a[i].m2);
        int sum=1;
        for(int i=1; i<n; i++)
        {
            //printf("%d:%d-%d:%d\n",a[i].h1,a[i].m1,a[i].h2,a[i].m2);
            if(a[i].h1==a[i-1].h2)
            {
                if(a[i].m1>a[i-1].m2)
                    sum++;

            }
            else
            {
                if(a[i].h1>a[i-1].h2)
                    sum++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

代码致错原因见下面的图分析

代码2(正确):

#include <stdio.h>
#include <algorithm>
using namespace std;
struct time
{
    int b;
    int e;
} a[105];
bool cmp(time x,time y)
{
    return x.e<y.e;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int h1,m1,h2,m2;
        for(int i=0; i<n; i++)
        {
            scanf("%d:%d-%d:%d",&h1,&m1,&h2,&m2);
            a[i].b=h1*100+m1;
            a[i].e=h2*100+m2;
            if(a[i].b>a[i].e)
                swap(a[i].b,a[i].e);
        }
        sort(a,a+n,cmp);

        int end=a[0].e;
          int sum=1;
        for(int i=1; i<n; i++) //多区间选取最少公共点
        {
            if(a[i].b>end)
            {
                end=a[i].e;
                sum++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}


\

代码2较代码一做了改进,优化了不必要的结构体参数,改成用一个整数存,更加方便,后半部分用了变量end改变了比较方式。

\

PS:还是要好好练~~

2018年06月04日14:48:31 java代码

import java.util.Scanner;
import java.util.Comparator;
import java.util.Arrays;

class Main {
    static final int N = 1000 + 20;

    static class node {
        int st, ed;
    }

    static node zz[] = new node[N];

    static class cmp implements Comparator<node> {
        public int compare(node x, node y) {
            return x.ed < y.ed ? -1 : 1;
        }
    }

    public static void main(String[] args) {
        Scanner io = new Scanner(System.in);
        while (io.hasNext()) {
            int n = io.nextInt();
            for (int i = 0; i < n; i++) {
                String s = io.next();
                zz[i] = new node();
                String t1 = s.substring(0, 2);
                String t2 = s.substring(3, 5);
                zz[i].st = Integer.parseInt(t1) * 100 + Integer.parseInt(t2);
                t1 = s.substring(6, 8);
                t2 = s.substring(9, 11);
                zz[i].ed = Integer.parseInt(t1) * 100 + Integer.parseInt(t2);
                if (zz[i].st > zz[i].ed) {
                    int t = zz[i].st;
                    zz[i].st = zz[i].ed;
                    zz[i].ed = t;
                }

            }
            Arrays.sort(zz, 0, n, new cmp());
            int temp = zz[0].ed;
            int ans = 1;
            for (int i = 0; i < n; i++) {
                if (zz[i].st > temp) {
                    ans++;
                    temp = zz[i].ed;
                }
            }
            System.out.println(ans);

        }

    }
}

\