• zoj 1610 Count the Colors


    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=610

     Count the Colors
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Description

    Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

    Your task is counting the segments of different colors you can see at last.

    Input



    The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

    Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

    x1 x2 c

    x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

    All the numbers are in the range [0, 8000], and they are all integers.

    Input may contain several data set, process to the end of file.

    Output



    Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

    If some color can't be seen, you shouldn't print it.

    Print a blank line after every dataset.

    Sample Input



    5
    0 4 4
    0 3 1
    3 4 2
    0 2 2
    0 2 3
    4
    0 1 1//第0个区间涂颜色1
    3 4 1//第3个区间涂颜色1
    1 3 2
    1 3 1
    6
    0 1 0
    1 2 1
    2 3 1
    1 2 0
    2 3 0
    1 2 1

    Sample Output



    1 1
    2 1
    3 1

    1 1

    0 2
    1 1

    题目大意:在线段上涂色,下一次涂色有可能会覆盖之前涂的,经过n次操作后问每种颜色在线段上有多少个间断的区间

    这道题可用线段树写,

     (0, 1)为区间0,(1, 2)为区间1,,(2, 3)为区间2   ...
     
    该代码未用线段树,用一般方法写的
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define INF 0x3f3f3f3f
    #define N 8010
    
    using namespace std;
    
    int color[N], counts[N];
    int main()
    {
        int n, i, a, b, c, Min, Max;
        while(scanf("%d", &n) != EOF)
        {
            memset(color, -1, sizeof(color));//记录区间被涂的颜色
            memset(counts, 0, sizeof(counts));//记录每种颜色的区间个数
            Min = INF;
            Max = -INF;
            while(n--)
            {
                scanf("%d%d%d", &a, &b, &c);
                Min = min(a, Min);
                Max = max(b, Max);
                for(i = a ; i < b ; i++)
                    color[i] = c;//第i个区间被涂上类型为c的颜色
            }
            for(i = Min + 1 ; i <= Max - 1 ; i++)
                if(color[i] != color[i - 1] && color[i - 1] != -1)
                    counts[color[i - 1]]++;//统计每种颜色的个数
            if(color[i - 1] != -1)
                counts[color[i - 1]]++;
            for(i = 0 ; i < N ; i++)
                if(counts[i] != 0)
                    printf("%d %d
    ", i, counts[i]);//i表示颜色种类,counts[i]涂i种颜色的区间个数
            printf("
    ");
        }
        return 0;
    }


     
  • 相关阅读:
    python之bytes和string(转)
    http post请求传文件报错 invalid character '' in numeric literal
    正则表达式匹配从指定字符开始到指定字符结束的字符串
    为什么提倡会议上要多提问?
    4K 显示器突然变模糊了怎么调回来
    快速将 gif 图转成 jpg 图片
    <一>window安装git
    <二>使用git将本地项目上传到gitee
    <一>window安装rabbitmq
    <一>redis安装
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4698711.html
Copyright © 2020-2023  润新知