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");
}

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

C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes inpu...
  • mengxiang000000
  • mengxiang000000
  • 2017年02月12日 17:17
  • 592

Codeforces 907A Masha and Bears

A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb ...
  • finalcsdn
  • finalcsdn
  • 2017年12月24日 17:17
  • 162

codeforce之Bear and Two Paths

题目: Bearland has n cities, numbered 1 through n. Cities are connected via bidirectional roa...
  • luckyu1
  • luckyu1
  • 2016年05月31日 11:46
  • 153

codeforce(找规律的简单题)

D. Prizes, Prizes, more Prizes time limit per test 2 seconds memory limit per test 256 megabytes...
  • ACMer_hades
  • ACMer_hades
  • 2015年04月22日 21:00
  • 952

CodeForces 673C Bear and Colors

思路:暴力出奇迹#include using namespace std; const int maxn =5005; int a[maxn]; int vis[maxn]; int ans[maxn...
  • qq_21057881
  • qq_21057881
  • 2016年05月10日 19:40
  • 1464

Codeforce - 573B - Bear and Blocks

B. Bear and Blocks time limit per test1 second memory limit per test256 megabytes inputstandard i...
  • qq_36551426
  • qq_36551426
  • 2017年02月26日 10:05
  • 79

【codeforces454div2】A. Masha and Bears

A family consisting of father bear, mother bear and son bear owns three cars. Father bear can climb ...
  • c_circle
  • c_circle
  • 2017年12月24日 17:33
  • 86

Codeforces673C Bear and Colors(枚举区间)

C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input sta...
  • KeyboardMagician
  • KeyboardMagician
  • 2016年07月27日 20:50
  • 409

markdown 最新语法

欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和...
  • ws1352864983
  • ws1352864983
  • 2016年03月05日 01:06
  • 444

CodeForces 673C - Bear and Colors(模拟)

C. Bear and Colors time limit per test2 seconds memory limit per test256 megabytes inputstandard ...
  • Snakeflute
  • Snakeflute
  • 2016年12月07日 17:59
  • 249
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:codeforce之 Bear and Colors
举报原因:
原因补充:

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