CodeForces 673C

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 n(n+1)/2 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.

Examples Input

4
1 2 1 2

Output

7 3 0 0

Input

3
1 1 1

Output

6 0 0

Note

In the first sample, color 2 is dominant in three intervals:

An interval [2, 2] contains one ball. This ball’s color is 2 so it’s clearly a dominant color.
An interval [4, 4] contains one ball, with color 2 again.
An interval [2, 4] contains two balls of color 2 and one ball of color 1.
There are 7 more intervals and color 1 is dominant in all of them.

熊Limak有n个彩球,排成一排。球从左到右从1到n编号。有n种可能的颜色,也从1到n编号。第i个球的颜色为ti。

对于固定间隔的球(一组连续元素),我们可以定义一种主导色。这是间隔中出现次数最多的一种颜色。如果某些颜色之间出现平局,则选择数字(索引)最小的一种为主。

总共有n(n + 1)/ 2个非空间隔。对于每种颜色,您的任务是计算该颜色占主导地位的间隔数。

输入
输入的第一行包含一个整数n(1≤n≤5000)-球的数量。

第二行包含n个整数t1,t2,…,tn(1≤ti≤n),其中ti是第i个球的颜色。

输出
打印n个整数。它们中的第i个应该等于其中i是主要颜色的间隔数。


输入
4
1 2 1 2
输出
7 3 0 0
输入
3
1 1 1
输出
6 0 0
注意
在第一个样本中,颜色2在三个间隔中占优势:

间隔[2,2]包含一个球。该球的颜色为2,因此显然是主要颜色。
间隔[4,4]包含一个球,颜色再次为2。
间隔[2,4]包含两个颜色为2的球和一个颜色为1的球。
还有7个间隔,并且颜色1在所有间隔中均占主导。

题目大意:

输入一个n表示有n个气球,接下来输入n个数表示第 i 号气球的颜色(颜色从1-n),在这些气球中有一些间隔,对于每个间隔,哪种颜色出现的最多哪种颜色就占主导,如果两种颜色出现的一样多,则颜色编号小的占主导,最后输出n个数,表示第 i 号颜色占主导的间隔数量。

解题思路:

这道题的范围只有5e3,可以暴力去做。开三个数组,一个记录n个气球的编号状态,一个临时数组记录记录气球颜色出现的次数,另一个数组记录答案,即对于每个颜色占主导的间隔数量。设两重循环,i从0到n-1,j从i到n-1,每次循环枚举 i 为开始的所有可能的间隔,设一个mmax和x,mmax记录出现的次数, x记录颜色编号,随时更新mmax和x,注意如果mmax==book[a[j]]时,表示有大于一种颜色出现的次数相等了,判断x和a[j]的关系并更新x的值。每次循环完以后ans[x]++,最后依次输出ans,AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int _max=5e3+50;
int a[_max],b[_max],ans[_max];
int main()
{
	ios::sync_with_stdio(false);
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	  cin>>a[i];
	memset(ans,0,sizeof ans);
	for(int i=0;i<n;i++)
	{
		int x,mmax=-1;
		memset(b,0,sizeof b);//每次以i为开始的间隔时,book数组要清零。
		for(int j=i;j<n;j++)
		{
			b[a[j]]++;
			if(mmax<b[a[j]])//随时更新mmax和x
			{
				mmax=b[a[j]];
				x=a[j];
			}
			else if(mmax==b[a[j]])//注意一下判断气球编号
			{
				if(x>a[j])
				  x=a[j];
			}
			ans[x]++;
		}
	}
	for(int i=1;i<=n;i++)
	  cout<<ans[i]<<" ";
	cout<<endl;
	//system("pause");
	return 0;  
}
原文地址:https://www.cnblogs.com/Hayasaka/p/14294281.html