codeforce之 Bear and Colors

原创 2016年05月31日 10:33:33

题目:

Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.

For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.

There are  non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — the number of balls.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ n) where ti is the color of the i-th ball.

Output

Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.

解答:

N^2级别,在循环的同时进行更新从当前地点开始每个颜色出现的次数。然后和前一个进行比较是否更新了最大值


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<iomanip>
#include<string.h>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<cstdio>
#include<string>

using namespace std;
int num[5002][5002] = { 0 };
int ans[5002] = { 0 };
int color[5002];
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> color[i];
	}
	for (int i = 0; i < n; ++i) {
		int max = 0;
		int temp = color[i] - 1;
		for (int j = i; j < n; ++j){
			num[i][color[j] - 1]++;
			if (num[i][color[j] - 1] > max) {
				max = num[i][color[j] - 1];
				temp = color[j] - 1;
			}
			else if (num[i][color[j] - 1] == max)
			{
				if (color[j] - 1< temp) {
					temp = color[j] - 1;
				}
			}
			ans[temp]++;
		}
	}
	for (int i = 0; i < n; ++i){
		cout << ans[i] << " ";
	}
	//system("pause");
}

相关文章推荐

codeforce771B Bear and Different Names 贪心or思维

记住,当你觉得很苦很累的时候,你真的在进步
  • Na_OH
  • Na_OH
  • 2017年05月02日 15:24
  • 82

Codeforces 673C Bear and Colors【暴力枚举】

C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes inpu...

Codeforces673C Bear and Colors(枚举区间)

C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input sta...

Codeforces Round #356 (Div. 2)-A. Bear and Five Cards(模拟)

A. Bear and Five Cards time limit per test 2 seconds memory limit per test 256 megabytes...

Codeforces 573D Bear and Cavalry

Would you want to fight against bears riding horses? Me neither. Limak is a grizzly bear. He is gen...

Codeforces 680D Bear and Tower of Cubesp【贪心+Dfs】有点难度啊T T

D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes...

【32.89%】【codeforces 574D】Bear and Blocks

time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard...

Codeforces 574C Bear and Poker【思维】

C. Bear and Poker time limit per test 2 seconds memory limit per test 256 megabytes input...

Codeforces Round #356 (Div. 2) C. Bear and Prime 100 (有趣的题目)

C. Bear and Prime 100time limit per test 1 second memory limit per test 256 megabytes input sta...

Educational Codeforces Round 8 C. Bear and String Distance(贪心)

题意: 给定N≤105的字符串,定义字符距离dist(a,b)=abs(a−b)给定N\le10^5的字符串, 定义字符距离dist(a, b)=abs(a-b) 求一个同样长度的但是距离总...
  • lwt36
  • lwt36
  • 2016年02月22日 21:40
  • 236
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforce之 Bear and Colors
举报原因:
原因补充:

(最多只允许输入30个字)