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. Astrictly 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.
Input Specification:
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.
Output Specification:
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 24Sample Output:
24
注意点:map<string,int>会超时!只能用排序查找!关键是Dominant Color一定存在且数量超过一半!!这个时间复杂取决于快拍
还有一个更好的算法:即
遍历数组若遇见不相同的就删除,最后剩下的就是Dominant Color 时间复杂度为O(N)级别!!
AC参考代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int N,M;
int main()
{
scanf("%d%d",&N,&M);
int *arr = new int[N*M];
for(int i=0;i<N*M;i++){
scanf("%d",arr+i);
}
sort(arr,arr+M*N);
cout<<arr[M*N/2];
return 0;
}
本文介绍了一种高效的算法来确定图像中的主导颜色。通过遍历数组并删除不同颜色的方式,可以找到出现次数超过一半的颜色,时间复杂度仅为O(N)。

400

被折叠的 条评论
为什么被折叠?



