【Codeforces Round #351 Div. 2】 673C Bear and Colors
2016-08-28 21:40
393 查看
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.
【题目分析】
很显然有接近n^2个区间,直接暴力就是n^3的复杂度会爆炸,不妨固定一个左端点l然后r扫描过去,这样就可以做到n^2的复杂度了。
【代码】
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.
【题目分析】
很显然有接近n^2个区间,直接暴力就是n^3的复杂度会爆炸,不妨固定一个左端点l然后r扫描过去,这样就可以做到n^2的复杂度了。
【代码】
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int n,col[5010],time[5010],tong[5010],maxv,maxn; int main() { scanf("%d",&n); for (int i=1;i<=n;++i) scanf("%d",&col[i]); for (int i=1;i<=n;++i){ memset(tong,0,sizeof tong); maxv=0;maxn=99999; for (int j=i;j<=n;++j){ tong[col[j]]++; if (tong[col[j]]>maxv){ maxv=tong[col[j]]; maxn=col[j]; } else if(tong[col[j]]==maxv&&maxn>col[j]) { maxv=tong[col[j]]; maxn=col[j]; } time[maxn]++; } } for (int i=1;i<n;++i) printf("%d ",time[i]); printf("%d\n",time ); return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- Codeforces Educational Codeforces Round 16 部分题解
- 【WHUST 2016 Individual Contest #1】解题报告
- 【WHUST 2016 Individual Contest #2】解题报告
- 【WHUST 2016 Individual Contest #3 】解题报告
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- Codeforces Round #363 (Div. 2)
- Codeforces Round #265 (Div. 2)
- codeforces 1B Spreadsheets
- Codeforces #310 div2 C. Case of Matryoshkas
- Codeforces Round #364 vp
- Codeforces Round #318 vp