Count the Colors
Time Limit: 2 Seconds
Memory Limit: 65536 KB
Painting some colored segments on a line, some previusly painted segments may be covered by some of 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 shoshould contain a color index that can be seen from the top, following the count of the segments of this color, they shocould 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
Code:
The meaning of the question is wrong... I thought it was the number of the same colors for each small segment...
It turns out that if two segments of the same color are adjacent, a segment will be merged...
Dizzy, I thought the sample output gave an error.
Attach the idiot code for future reference!
If you suspect that the question is wrong, you have to pay the price!
# Include "stdio. h "int main () {int tree [8010]; int finalcolor [8010]; int n, I, j; int ls, rs, c; // temporarily indicates the left and right ranges of the current input data and the color int rmax; // indicates the total rightmost boundary while (scanf ("% d", & n )! = EOF) {rmax = 0; for (I = 0; I <n; I ++) // enter n colors, and Its Dyeing Range {scanf ("% d", & ls, & rs, & c); if (rs> rmax) rmax = rs; // The value in the subscript starting from 1 indicates the color of the previous unit segment for (j = ls + 1; j <= rs; j ++) // The color entered later will directly overwrite the previous color tree [j] = c ;}// for (I = 1; I <= rmax; I ++) // printf ("% d", tree [I]); // printf ("\ n ----- \ n"); for (I = 0; I <= rmax; I ++) // initialization. Assign the number of segments where the color is I to 0 finalcolor [I] = 0; // finalcolor [I]. sign = finalcolor [I]. num = 0; int k; j = 0; for (I = 1; I <= rmax; I ++) {finalcolor [tree [I] ++ ;} // forfor (I = 0; I <= rmax; I ++) {if (finalcolor [I]) printf ("% d \ n", I, finalcolor [I]) ;}/// whilereturn 0 ;}
The following is the correct AC code,
This time, I understand the merging attributes of the line tree ~~.....
# Include "stdio. h "# include" string. h "int main () {int tree [8010]; int finalcolor [8010]; int n, I, j; int ls, rs, c; // temporarily indicates the left and right ranges of the current input data and the color int rmax; // indicates the total rightmost boundary while (scanf ("% d", & n )! = EOF) {rmax = 0; memset (tree,-1, sizeof (tree); for (I = 0; I <n; I ++) // enter n colors and Their Dyeing ranges {scanf ("% d", & ls, & rs, & c); if (rs> rmax) rmax = rs; // The value stored in the subscript starting from 1 indicates the color of the previous unit segment (j = ls + 1; j <= rs; j ++) // The color entered later will directly overwrite the previous color tree [j] = c ;}// for (I = 1; I <= rmax; I ++) // printf ("% d", tree [I]); // printf ("\ n ----- \ n"); for (I = 0; I <= 8001; I ++) // initialization. Assign the number of segments where the color is I to 0 finalcolor [I] = 0; // finalcolor [I]. sign = finalcolor [I]. num = 0; finalcolor [tree [1] = 1; ; For (I = 2; I <= rmax; I ++) {// If the I color is different from the previous one, the number of segments can be + + if (tree [I]! = Tree [I-1]) finalcolor [tree [I] ++;} // forfor (I = 0; I <= 8001; I ++) {if (finalcolor [I]) printf ("% d \ n", I, finalcolor [I]);} printf ("\ n ");} // whilereturn 0 ;} /* 50 4 40 3 13 4 20 2 20 2 340 1 13 4 11 3 21 3 160 1 01 2 12 3 11 2 02 3 01 2 131 2 31 2 21 2 1 */