题目描述
Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800x600), you are supposed to point out the strictly dominant color.
翻译:在每个背景的后面都是电脑的存储,颜色总是用一段24位信息存储每个像素。在一张图片中,占据最大比例的位置的颜色就叫做主色。一个严格的主色占据总面积的一半。现在给你一个M*N分辨率的图片(例如800*600),你需要输出其严格的主色。
INPUT FORMAT
Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (<=800) and N (<=600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0, 224). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括两个正整数:M(<=800) 和 N (<=600) 代表图片的大小。接着N行,每行包括M个在 [0, 2^24)范围内的数字颜色。数据保证每张输入的图片都存在严格的主色。一行内所有数字之间用空格隔开。
OUTPUT FORMAT
For each test case, simply print the dominant color in a line.
翻译:对于每组输入数据,一行内输出主色。
Sample Input:
5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24
Sample Output:
24
解题思路
将数据进行排序,然后计数,如果过半就输出。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int temp,M,N;
int d[500000],ccount=0;
int main(){
scanf("%d%d",&M,&N);
int a;
int Max=M*N;
for(int i=0;i<Max;i++)scanf("%d",&d[i]);
sort(d,d+Max);
int ccount=0;
for(int i=0;i<Max;i++){
if(i==0||d[i-1]!=d[i])ccount=1;
else ccount++;
if(ccount>Max/2){
printf("%d\n",d[i]);
break;
}
}
return 0;
}