ZOJ 1610 Count the Colors

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610

Count the Colors

Time Limit: 2 Seconds Memory Limit: 65536 KB

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
3 4 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

Author: Standlove
Source: ZOJ Monthly, May 2003

大意——在一条线上画出一些有颜色的线条,先画出来的可能会被后画出来的给覆盖掉。现在给你一些线条的区间和颜色,问:要你计算出最后能看见的线条的颜色以及条数,并且按颜色值从小到大输出,不能看见的不输出,两个样例之间有一个空行。

思路——开一个数组用来存区间颜色,并且每一次输入完毕,都将区间作为索引进行颜色的存储,那么所有的输入结束后,最后能看见的颜色也保存了下来。再开一个数组用来存区间颜色数目,那么利用颜色做索引记录颜色次数,并且连续相同的颜色只能算一次,这样枚举整个数组,最后就能把颜色次数和颜色记录下来了,而且颜色已然排好序。最后再枚举整个颜色次数数组,判断输出即为答案。

复杂度分析——时间复杂度:O(n^2),空间复杂度:O(maxn)

附上AC代码:


#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int maxn = 8005;
int color[maxn], cnt[maxn];
int start, dest, i_color, n;

int main()
{
    while (scanf("%d", &n) != EOF)
    {
        memset(color, -1, sizeof(color));
        memset(cnt, 0, sizeof(cnt));
        for (int i=0; i<n; i++)
        {
            scanf("%d%d%d", &start, &dest, &i_color);
            for (int j=start; j<dest; j++)
                color[j] = i_color;
        }
        if (color[0] != -1)
            cnt[color[0]]++;
        for (int i=1; i<maxn; i++)
            if (color[i]!=-1 && color[i]!=color[i-1])
                cnt[color[i]]++;
        for (int i=0; i<maxn; i++)
            if (cnt[i] != 0)
                printf("%d %d\n", i, cnt[i]);
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(ZOJ,暴力枚举)