codeforces 673C(思维)
2017-07-31 10:53
330 查看
题意:找出每种颜色在区间是否满足它在这个区间的数量最多或者与最大相等但颜色最小,满足的话就加一,输出每种颜色操作后的结果。
思路:直接枚举区间,开一个数组然后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;
}
思路:直接枚举区间,开一个数组然后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;
}
相关文章推荐
- CodeForces-719B Anatoly and Cockroaches(思维题)
- Codeforces 653C Bear and Up-Down【暴力+思维】题目还不错~
- Codeforces 344E Read Time [二分答案]【思维】
- CodeForces - 729D Sea Battle(思维题)
- Codeforces 404C Restore Graph【思维】
- Codeforces 892 D. Gluttony (思维)
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- CodeForces - 711C Coloring Trees(DP)(思维)
- codeForces 672D.Robin Hood(思维+二分)
- Codeforces 763B Timofey and rectangles(四色定理)(思维)
- CodeForces - 894C Marco and GCD Sequence 构造+思维
- CodeForces - 427A (警察和罪犯 思维题)
- CodeForces - 229C 【思维】
- Codeforces 362D Fools and Foolproof Roads【并查集+优先队列+思维】
- Codeforces 134A-Average Numbers(思维)
- CodeForces 604C Alternative Thinking(思维题)
- Codeforces 369D Valera and Fools【思维+dp】
- codeforces 673C
- CodeForces 405D Toy Sum【思维】
- Codeforces 389D Fox and Minimal path【构造+二进制思维】好题!