CodeForces 643 A.Bear and Colors(水~)
来源:互联网 发布:鸟哥linux私房菜基础篇 编辑:程序博客网 时间:2024/06/02 06:15
Description
n个球,每个球有一个颜色c[i],对于一个区间里的球,定义该区间的统治颜色为出现次数最多的颜色,如果有多种颜色出现次数都是最多的则选择颜色编号小的那个座位统治颜色,现在对于每种颜色,输出其作为统治颜色的区间数
Input
第一行一整数n表示气球数量,之后n个整数c[i]表示每个气球的颜色(1<=n<=5000,1<=c[i]<=n)
Output
输出n个整数表示每种颜色作为统治颜色的区间数
Sample Input
4
1 2 1 2
Sample Output
7 3 0 0
Solution
暴力枚举区间统计每种颜色出现的数量,注意到加入了一种颜色后最大值只会从之前的最大值和这种颜色中取到,故每次更新最大值是O(1)的,总时间复杂度O(n^2)
Code
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue>#include<map>#include<set>#include<ctime>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define maxn 5555int n,c[maxn],num[maxn],ans[maxn];int main(){ while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d",&c[i]); memset(num,0,sizeof(num)); memset(ans,0,sizeof(ans)); for(int i=1;i<=n;i++) { memset(num,0,sizeof(num)); num[c[i]]++,ans[c[i]]++; int Max=c[i]; for(int j=i+1;j<=n;j++) { num[c[j]]++; if(num[c[j]]>num[Max]||num[c[j]]==num[Max]&&c[j]<Max)Max=c[j]; ans[Max]++; } } for(int i=1;i<=n;i++) printf("%d%c",ans[i],i==n?'\n':' '); } return 0;}
0 0
- CodeForces 643 A.Bear and Colors(水~)
- CodeForces 673C Bear and Colors
- CodeForces 673C - Bear and Colors(模拟)
- CodeForces - 658A Bear and Reverse Radewoosh (模拟)水
- 【Codeforces Round】351A - Bear and Game(水)
- CodeForces - 673A Bear and Game (模拟)水
- Codeforces 653A: Bear and Three Balls(技巧,水)
- CodeForces 639 A.Bear and Displayed Friends(水~)
- 【Codeforces Round #351 Div. 2】 673C Bear and Colors
- Codeforces 673C Bear and Colors【暴力枚举】
- Codeforces 385A Bear and Raspberry(水题)
- Codeforces--658A--Bear and Reverse Radewoosh(模拟)
- 【CodeForces】653A - Bear and Three Balls(计数)
- CodeForces 658A Bear and Reverse Radewoosh(模拟)
- CodeForces 680A Bear and Five Cards(取数字)
- Codeforces 639A Bear and Displayed Friends (优先队列)
- Codeforces 385A Bear and Raspberry
- CodeForces 574A - Bear and Elections
- jsp笔记
- linux 出现大量TIME_WAIT
- Git命令大全及相关概念
- matlab:常用内置函数
- promise是什么,使用方法
- CodeForces 643 A.Bear and Colors(水~)
- Linux基础学习笔记之终端(terminal)简介
- 腾讯后台开发面经+答案
- c++拼接字符串效率比较(+=、append、stringstream、sprintf)
- 矩阵martix类模板
- Pandas DataFrame操作
- 获取本地音乐图片
- 为何浮点数(float,double)不能直接用等号比较
- java 线程