codeforces 673C(思维)

来源:互联网 发布:北京外企和fdi数据 编辑:程序博客网 时间:2024/05/22 02:17

题意:找出每种颜色在区间是否满足它在这个区间的数量最多或者与最大相等但颜色最小,满足的话就加一,输出每种颜色操作后的结果。

思路:直接枚举区间,开一个数组然后O(1)维护这个区间的最多的颜色的数。


PS:比赛的时候想不到呀?思维还是不够!


#include<bits/stdc++.h>using namespace std;typedef pair<int,int> P;const int maxn = 5000 + 10;typedef long long ll;int a[maxn];int cnt[maxn];int ans[maxn];int main(){    int n,m;    while( ~ scanf("%d",&n))    {        for(int i = 1; i <= n; i ++)            scanf("%d",&a[i]);        memset(ans,0,sizeof(ans));        for(int i = 1; i <= n; i ++)        {            memset(cnt,0,sizeof(cnt));            int t = 0,pos;            for(int j = i; j <= n; j ++)            {                cnt[a[j]] ++;                if(cnt[a[j]] > t || (cnt[a[j]] == t && a[j] < pos))t = cnt[a[j]],pos = a[j];                ans[pos] ++;            }        }        for(int i = 1;i <= n; i ++)            printf("%d%c",ans[i],i < n ? ' ': '\n');    }    return 0;}


原创粉丝点击