首页 > 试题广场 >

The Dominant Color (20)

[编程题]The Dominant Color (20)
  • 热度指数:3853 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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.

输入描述:
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.


输出描述:
For each test case, simply print the dominant color in a line.
示例1

输入

5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24

输出

24
#include<iostream>//十行
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int N,M;
    vector<int>v;
    cin>>N>>M;
    int Index=0;
    for(int i=0;i<M;i++)
        for(int j=0;j<N;j++)
    {
        int x;
        scanf("%d",&x);
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    cout<<v[(v.size()+1)/2]<<endl;
    return 0;
}
发表于 2016-12-03 20:23:48 回复(6)
import java.util.HashMap;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int m=in.nextInt();
		int n=in.nextInt();
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		n*=m;
		for(int i=0;i<n;i++){
			String s=in.next();
			if(map.containsKey(s)){
				int value=map.get(s);
				map.remove(s);
				map.put(s, value+1);
			}
			else{
				map.put(s,1);
			}
		}
		for(String s:map.keySet()){
			if(map.get(s)>n/2.0){
				System.out.println(s);
			}
		}
	}
	
}

发表于 2016-10-23 14:49:53 回复(0)
#include <cstdio>
int main(){
int m, n, pixel, ans = 0, cnt = 0;
scanf("%d %d", &m, &n);
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j){
scanf("%d", &pixel);
if(cnt == 0){ans = pixel;cnt++;}
else if(pixel != ans)cnt--;
else cnt++;
}
printf("%d\n", ans);
}
编辑于 2018-07-26 14:12:48 回复(1)

求主元素O(N),最后剩下的dom是主元素

#include

using namespace std;

int main() {
    int m, n;
    while (cin >> m >> n) {
        long dom = -1, cnt = 0;
        for (int i = 0; i<m*n; i++) {
            long pixel;
            cin >> pixel;
            if (pixel == dom)
                cnt++;//如果和dom相同,计数加一
            else {    
                cnt--;//如果不同计数减一
                if (cnt < 0) {//计数小于0,产生新的dom
                    dom = pixel;
                    cnt = 0;//重新计数
                }
            }
        }
        cout << dom << endl;
    }
}
发表于 2018-06-03 22:10:07 回复(0)
#include <iostream> 
#include <map>
using namespace std;
map<int,int> mp;   //数字与出现次数映射
int main(){
    int n,m,temp;
    cin>>m>>n;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%d",&temp);
            if(mp.find(temp)==mp.end()) mp[temp]=0; //若不存在,初始化
            mp[temp]++;                             //若已存在,数量加1
        }
    }
    int k=0,max=0;                                  //出现最多次的数字即其次数
    for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
        if(it->second>max){
            k=it->first;
            max=it->second;
        }
    }
    cout<<k;
    return 0;
}



//vector版
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int N,M;
    vector<int>v;
    cin>>N>>M;
    int Index=0;
    for(int i=0;i<M;i++)
        for(int j=0;j<N;j++)
    {
        int x;
        scanf("%d",&x);
        v.push_back(x);
    }
    sort(v.begin(),v.end());
    cout<<v[(v.size()+1)/2]<<endl;
    return 0;
}

编辑于 2019-01-03 16:48:24 回复(0)
因为如果用数组保存重复次数的话数组会很大而无法运行,因此使用了map,保存每个数据的重复次数。
//The Dominant Color (20)
#include<iostream>
//#include<algorithm>
#include<set>
#include<vector>
#include<map>

using namespace std;

int main(){
	set<int> SET;
	//int record[10000000]={0};
	map<int,int> mapRecord;
	int m,n;cin>>m>>n;
	int temp;int cnt=0;
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>temp;cnt++;
			map<int,int>::iterator iter=mapRecord.find(temp);
			if(iter!=mapRecord.end())iter->second++;//-------------------!!!
			else mapRecord.insert(pair<int,int>(temp,1));
			SET.insert(temp);
		}
	}
	for(set<int>::iterator it=SET.begin();it!=SET.end();it++){
		if(mapRecord.find(*it)->second>cnt/2)cout<<*it;
	}
	
	return 0;
}

发表于 2021-08-19 21:54:50 回复(0)
d = {}
for i in range(int(input().split()[1])):
    for j in input().split():
        try:
            d[j] += 1
        except:
            d[j] = 1
            
print(sorted([[d[i], i] for i in d])[-1][1])

发表于 2020-02-25 17:26:59 回复(0)
用空间换时间,利用计数排序的思想,以为会超时,但是没有。
#include <iostream>
 
using namespace std;
#define INT_MAX 16777218
int buf[INT_MAX]={0};
 
int main() {
    int m,n;
    cin >> m >> n;
    for (int i=0; i<m*n; i++) {
        int val;
        cin >> val;
        buf[val]++;
    }
    for (int j=0; j<INT_MAX; j++) {
        if (buf[j] > m*n/2)
            cout << j << endl;
    }
    return 0;
}


发表于 2020-02-19 13:28:03 回复(0)
#include<iostream>
#include<cstdio>
#include<map>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdlib>
using namespace std;
int m, n;
map<int, int> mp;
int main()
{
    int maxx = -(1 << 24);
    int ans = 0,num;
    cin >> m >> n;
    for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
    {
        cin >> num;
        mp[num]++;
        if (mp[num] > maxx)
        {
            maxx = mp[num];
            ans = num;
        }
    }
    cout << ans << endl;
    system("pause");
    return 0;
}
发表于 2018-09-23 20:46:02 回复(0)

#include<map>
using namespace std;
int main(){
 int a,b;
 cin>>a>>b;
 map<string,int> s;
 string s1;
 string c;
 int max=0;
 for(int i=0;i<a;i++){
  for(int j=0;j<b;j++){
   cin>>c;
  // if(s.find(c)==s.end()) s[c]=0;//初始化
   s[c]++;
   if(s[c]>max)
   {
      max=s[c];
      s1=c;
       }
     }
 }
 cout<<s1<<endl;
 return 0;
}
发表于 2018-05-11 14:08:31 回复(0)
/* 3 2 4502761 4502761 4502761 4502761 7340438 3208719  */  //不用map数组,两两抵消 #include <cstdio> int main(int argc, char const *argv[])
{ int n, m;  scanf("%d%d", &n, &m);   int num;  int count = 1, ans = -1;  for(int i  = 0; i < n * m; i++){
        scanf("%d", &num);  if(ans != num){
            count--;  }else{
            count++;  } if(count == 0){
            ans = num;  count++;  }

    }
    printf("%d\n", ans);  return 0; }

发表于 2020-02-27 20:25:10 回复(1)
//喜欢秀STL?
#include <bits/stdc++.h>
using namespace std;
#define maxm 800
#define maxn 600
int screen[maxm][maxn];
int colors[1<<26] = {0};
pair<int, int> dominp(-1, 0);
int main() {
    int m, n, color;
    cin >> m >> n;
    for (int i = 0 ; i < m; i++) {
        for (int j = 0; j < n ; j ++) {
            scanf("%d", &color);
            colors[color]++;
            if (colors[color] > dominp.second) {
                dominp.first = color;
                dominp.second = colors[color];
            }
        }
    }
    cout << dominp.first;
}

发表于 2023-09-04 15:12:39 回复(0)
摩尔排序的思路
#include <bits/stdc++.h>
using namespace std;

int main() {
    int m, n;
    while (cin >> m >> n) {
        ios::sync_with_stdio(false);
        vector<long long int>colors;
        for (int i = 0; i < m * n; ++i) {
            long long int color;
            cin >> color;
            colors.emplace_back(color);
        }
        long long int domain = colors[0];
        int count = 1;
        for (int i = 1; i < colors.size(); ++i) {
            if (domain == colors[i]) {
                count++;
            } else if (--count < 0) {
                domain = colors[i];
                count=1;
            }
        }
        cout << domain << endl;
    }
}



发表于 2023-08-16 20:42:07 回复(0)
占了好大的内存呀😅
#include<bits/stdc++.h>
using namespace std;

const int Max=1e8;
int Hashtable[Max];

int main() {
    ios::sync_with_stdio(0);
	int n,m,Max=0,ans;
	cin>>n>>m;
	int x;
	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			cin>>x;
			Hashtable[x]++;
			if(Hashtable[x]>Max) {
				Max=max(Max,Hashtable[x]);
				ans=x;
			}
		}
	}
	cout<<ans<<endl;
}

编辑于 2022-11-19 21:51:39 回复(2)
python一开始list会超时,改成dict
m, n = map(int, input().split())
clr = {}
# 字典是O(1),比list要快很多,把存储结构换成字典
th = m * n / 2
th1 = m * n
while len(clr) != th1:
    cur = input().split()
    for i in cur:
        if clr.get(i, -1) != -1:
            clr[i] += 1
        else:
            clr[i] = 1
    l = sorted(clr.items(), key=lambda x: x[1], reverse=True)
    if l[0][1] > th:
        print(l[0][0])
        break


# 原来的,最后两个测试点会超时
# m, n = map(int, input().split())
# clr = []
# th = m * n / 2
# th1 = m * n
# while len(clr) != th1:
#     # cur = list(map(int, input().split()))
#     cur = [int(i) for i in input().split()]
#     clr.extend(cur)
#     tmp_s = set(cur)
#     cnt = [(clr.count(i), i) for i in tmp_s]
#     if max(cnt)[0] > th:
#         print(max(cnt)[1])
#         break

            


发表于 2021-07-11 15:41:48 回复(0)
#include<iostream>
#include<map>
using namespace std;
int N, M;
map<int,int> cnt;
int main()
{
	cin >> M >> N; int color;
	for (int i = 0; i < N; ++i)
		for (int j = 0; j < M; ++j) {
			cin >> color;
			if (++cnt[color] > M * N / 2) break;
		}
	cout << color;
	return 0;
}

发表于 2021-03-16 06:33:21 回复(0)
//The Dominant Color (20分)
#include <iostream>
(720)#include <map>

using namespace std;
int n, m, maxcolor = 0;
map<int, int> arrays;

int main() {
    scanf("%d %d", &m, &n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            int color;
            scanf("%d", &color);
            arrays[color]++;
        }
    }
    for (map<int, int>::iterator it = arrays.begin(); it != arrays.end(); it++) {
        if (it->second > arrays[maxcolor] && it->second > (n * m) / 2)
            maxcolor = it->first;
    }
    printf("%d", maxcolor);
}

发表于 2020-03-26 16:04:52 回复(0)
参考第一位大佬的,牛客不报错我都不知道条件给的主要颜色必须大于一半的区域
代码:
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
	map<int, int> p;
	int len = 0, wid = 0, tem = 0;
	cin >> len >> wid;
	for (int i = 0; i < len*wid; i++) {
		scanf("%d",&tem);
		p[tem]++;
		if (p[tem] > len*wid / 2)break;
	}
	cout << tem<<endl;
	system("pause");
	return;
}


发表于 2019-10-24 22:46:12 回复(0)
我就感觉很奇怪,为什么一直错,竟然有个测试用例是这样的,, 行数和列数搞反了,按原题意思应该是6 4


发表于 2019-08-23 22:52:31 回复(1)
#include <iostream>
#include <algorithm>
using namespace std;

//1054 The Dominant Color
//简单题,cin超时,换成scanf就过了。
    
    
map<int,int> mp;

int main(){
    int m,n,val;
    cin>>m>>n;
    
    
    for(int i=0;i<n;++i){
        for(int j=0;j<m;++j){
            scanf("%d",&val);
            mp[val]+=1;
        }
    }
    
    //vector<pair<int, int> > v(mp.begin(),mp.end());
    
    int maxx = 0,maxid=0;
    for(map<int,int>::iterator i=mp.begin();i!=mp.end();++i){
        if(i->second>maxx){ maxx=i->second; maxid = i->first;}
    }
    
    cout<<maxid;
    return 0;
}
发表于 2019-08-17 12:30:10 回复(0)