C. Bear and Colors
2 seconds
256 megabytes
standard input
standard output
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.
模拟每个区间,通过if(cnt[a[j]]>cnt[a[best]]||(cnt[a[j]] == cnt[a[best]]&&a[j]<a[best])) best = j来转移
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = ;
int a[maxn],cnt[maxn],ans[maxn];
void solve(){
int n;
scanf("%d",&n);
for(int i = ; i<=n; i++) scanf("%d",&a[i]);
for(int i = ; i<=n; i++){
// ans[a[i]]++;
int best = i;
memset(cnt,,sizeof(cnt));
// for(int j = i; j<=n; j++) cnt[a[j]] = 0;
for(int j = i; j<=n; j++)
{
cnt[a[j]]++;
if(cnt[a[j]]>cnt[a[best]]||(cnt[a[j]] == cnt[a[best]]&&a[j]<a[best])) best = j;
ans[a[best]]++;
}
}
for(int i = ; i<n; i++)
{
printf("%d ",ans[i]);
}
printf("%d",ans[n]); }
int main()
{
solve();
return ;
}
C. Bear and Colors的更多相关文章
- codeforces 351 div2 C. Bear and Colors 暴力
C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力
C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...
- codeforces 673C C. Bear and Colors(暴力)
题目链接: C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors
题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...
- [Codeforces673C]Bear and Colors(枚举,暴力)
题目链接:http://codeforces.com/contest/673/problem/C 题意:给一串数,不同大小的区间内出现次数最多的那个数在计数的时候会+1,问所有区间都这样计一次数,所有 ...
- C. Bear and Colors 区间枚举的技巧
http://codeforces.com/problemset/problem/673/C 先说一个枚举区间的技巧,枚举前缀,不要枚举后缀. 就是下面这个代码是不好的 ; i <= n; ++ ...
- 【Henu ACM Round#16 B】 Bear and Colors
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] O(n^2)枚举每一个区间. 然后维护这个区间里面的"统治数字"是什么. 对于每个区间cnt[统治数字]++; ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C (用map 超时)
C. Bear and Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- LeetCode OJ 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- pulseaudio的交叉编译
在/etc/profile里导入 export PATH==$PATH:/home/jack/arm-linux-gcc/x-tools/arm-unknown-linux-gnueabi/bin 配 ...
- Inno Setup入门(十)——操作注册表
有些程序需要随系统启动,或者需要建立某些文件关联等问题,这些都是通过在安装程序中对注册表进行操作的结果.Inno Setup中通过[registry]段实现对注册表的操作. 本段说明: 参数列表: 参 ...
- ref与out之间的区别整理 摘自与望楼http://blog.csdn.net/xiaoning8201/article/details/6893154
ref和out都是C#中的关键字,所实现的功能也差不多,都是指定一个参数按照引用传递. 对于编译后的程序而言,它们之间没有任何区别,也就是说它们只有语法区别. 总结起来,他们有如下语法区别: .ref ...
- C++调用C#之C# COM控件
C#做界面真的是比C++方便多了,所以尝试了一下,使用C++做核心功能(例如绘图),然后用C#来做节目(例如对话框),考虑到以后可能不能使用.net,使用DLL做一个隔离层,隔离C++和C#,方便以后 ...
- HTML5编程之旅系列一:HTML5 Geolocation 初探
让我们假设这样一个场景,有一个web应用程序,它可以向用户提供附近不远处某商场的打折优惠信息.使用HTML5 Geolocation API(地理定位 API),可以请求用户共享他们的位置信息. HT ...
- LightOJ 1341 Aladdin and the Flying Carpet(整数拆分定理)
分析:题目并不难理解,就是一些细节上的优化需要我们注意,我在没有优化前跑了2000多MS,优化了一些细节后就是400多MS了,之前还TLE了好几次. 方法:将整数拆分为质因子以后,表达为这样的形式,e ...
- angularJS 判断
判断语句: ng-switch on ng-switch-when ng-switch-when ng-if=”person.sex==1“ <ul> <li ng-repeat=” ...
- SVM支持向量机算法
支持向量机(SVM)是另一类的学习系统,其众多的优点使得他成为最流行的算法之一.其不仅有扎实的理论基础,而且在许多应用领域比大多数其他算法更准确. 1.线性支持向量机:可分情况 根据公式(1)< ...
- HDU 5480 Conturbatio
区间求和不更新,开个数组记录一下前缀和就可以了 #include<cstdio> #include<cstring> #include<cmath> #includ ...