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.
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.
Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color.
4
1 2 1 2
7 3 0 0
3
1 1 1
6 0 0
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.
题意:找出每个区间的重数,将重数的次数输出;
思路:暴力找复杂度o(n*n)
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<queue> #include<algorithm> #include<stack> #include<cstring> #include<vector> #include<list> #include<set> #include<map> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - '0' ; while( ( ch = getchar() ) >= '0' && ch <= '9' ) res = res * 10 + ( ch - '0' ) ; return res ; } int a[5010]; int flag[5010]; int ans[5010]; int main() { int x,y,z,i,t; scanf("%d",&x); for(i=1;i<=x;i++) scanf("%d",&a[i]); for(i=1;i<=x;i++) { memset(flag,0,sizeof(flag)); int maxx=0,ji; for(t=i;t<=x;t++) { //cout<<maxx<<" "<<ji<<" "<<a[t]<<endl; flag[a[t]]++; if(flag[a[t]]>maxx) { maxx=flag[a[t]]; ji=a[t]; ans[ji]++; } else if(flag[a[t]]==maxx&&ji>a[t]) { ji=a[t]; ans[ji]++; } else ans[ji]++; } } for(i=1;i<=x;i++) printf("%d ",ans[i]); return 0; }