3299. 主色调

shwei
#include <bits/stdc++.h>
using namespace std;

int main(void) { 
    // cin data
    int n;
    while (scanf("%d", &n) != EOF) {
        int tmp = n;
        vector <int> vec;
        while (n--) {
            int c;
            cin >> c;
            vec.push_back(c);
        }
        n = tmp;

    int res[n + 1] = {0};

    /*
    4
    1 2 1 2
    brute count every section
    */
    for (int i = 0; i < n; i++) {
        vector <int> temp(n + 1, 0);
        int nowcolor, oldcolor, oldcnt = 0;
        for (int j = i; j < n; j++) {
            nowcolor = vec[j];
            temp[nowcolor]++;
            if (temp[nowcolor] > oldcnt) {
                oldcnt = temp[nowcolor];
                oldcolor = nowcolor;
            } else if (temp[nowcolor] == oldcnt && oldcolor > nowcolor) {
                oldcolor = nowcolor;
            }
            res[oldcolor]++;
        }
    }

    // cout result
    for (int i = 1; i < n; i++) {
        cout << res[i] << " ";
    }
    cout << res[n] << endl;
    }

    return 0;
}
Xiba

include

include

using namespace std;
int c[5001];
int r[5001];
int temp[5001];
int n;
void cl()
{
for(int i=1;i<=n;i++)
{
c[i]=0;
r[i]=0;
}
}
void cltemp()
{
for(int i=1;i<=n;i++)
temp[i]=0;
}
void cal()
{
int k;
int maxnumber;
for(int i=1;i<=n;i++)
{
cltemp();
temp[c[i]]++;
k=c[i];
maxnumber=1;
r[k]++;
for(int j=i+1;j<=n;j++)
{
temp[c[j]]++;
if(temp[c[j]]c[j])?c[j]:k;
r[k]++;
continue;
}
if(temp[c[j]]>maxnumber)
{
maxnumber=temp[c[j]];
k=c[j];
r[k]++;
continue;
}
}
}
}
int main()
{
while(cin>>n)
{
cl();
for(int i=1;i<=n;i++)
cin>>c[i];
cal();
for(int i=1;i<=n;i++)
{
cout<<r[i];
if(i!=n)
cout<<” “;
}
cout<<endl;
}
return 0;
}

你当前正在回复 博客/题目
存在问题!