题面:

传送门

题目描述:

题意:确定最大的x,使去除掉前x天的其中一天后,所有不同数字的数量相等。
 

题目分析:

可能是我太久没打cf了,水题都做不出来。
这道题的关键在于:要记录相同数量,的不同数字,有多少个。为什么要这样记录呢?我们分析一下符合题意的有多少种情况:
1.每个不同的数字只出现1次,例:1,2,3,4,5
2.只有一种数字,例:1,1,1,1,1
3.只有一种数字出现1次,其他数字出现的次数相同,例:1,1,1,2,2,2,3
4.有一种数字出现的次数比其他数字都多出1次,例:1,1,2,2,3,3,3
从这些情况可以看出:用“普通”的方法(直接遍历循环检查次数)是行不通的(会超时,这个是O(n^2)的做法)。所以问题就在于如何优化检查“次数”。这时我们只要:多开一个记录 “相同数量有多少个不同数字” 的数组,举个例子:
我们开一个叫same_cnt的数组,那么对于这组数据:1,1,2,2,3,3,4,4,4
其中same_cnt[2] = 3(因为数字1,2,3的数量是2,共3个)
same_cnt[3] = 1(因为数字4的数量是3,共1个)
接下来,我们从小到大枚举x的值就行了,判断可以用这个辅助数组O(1)判断出来。
 
 
AC代码:
 1 #include <bits/stdc++.h>
2 using namespace std;
3 const int maxn = 1e5+5;
4 int u[maxn];
5 int cnt[maxn];
6 int same_cnt[maxn];
7
8 int main(){
9 int n;
10 scanf("%d", &n);
11 for(int i = 1; i <= n; i++){
12 scanf("%d", &u[i]);
13 }
14
15 int res = 0;
16 int mx = 0;
17 for(int i = 1; i <= n; i++){
18 cnt[u[i]]++;
19 same_cnt[cnt[u[i]]-1]--;
20 same_cnt[cnt[u[i]]]++;
21
22 mx = max(mx, cnt[u[i]]); //小技巧,自己领会一下
23 int ok = 0;
24
25 if(same_cnt[1] == i) ok = 1; //第一种情况:数量为1的有i个
26 else if(same_cnt[i] == 1) ok = 1; //第二种情况:数量为i的有1个
27 //第三种情况
28 else if(same_cnt[1] == 1 && same_cnt[mx]*mx == i-1) ok = 1;
29 //第四种情况
30 else if(same_cnt[mx] == 1 && same_cnt[mx-1]*(mx-1) == i-mx) ok = 1;
31
32 if(ok) res = i;
33 }
34
35 printf("%d\n", res);
36 return 0;
37 }
 
 
 
 

Codeforces Round #558 B2. Cat Party (Hard Edition)的更多相关文章

  1. Codeforces Round #558 (Div. 2)

    目录 Codeforces Round #558 (Div. 2) 题解 A Eating Soup B Cat Party C Power Transmission D Mysterious Cod ...

  2. Codeforces Round #558 (Div. 2)-Cat Party (Hard Edition)-(前缀和 + 模拟)

    http://codeforces.com/problemset/problem/1163/B2 题意:有n天,每天有一个颜色,截取前x天,随便抽掉一天,使剩下的各个颜色出现的次数相等. 解题,也可以 ...

  3. Codeforces Round 558(Div 2)题解

    这场比赛没有打,后来和同学们一起开了场镜像打…… B是SB题结果WA了5发…… C是SB题结果差5min调出……虽然中间有个老师讲题吃掉了1h D是比较神仙的题(2200),但是做出来了?算是比较超常 ...

  4. Codeforces Round #558 (Div. 2)B(SET,模拟)

    #include<bits/stdc++.h>using namespace std;int a[100007];int cnt[100007];int main(){    int n; ...

  5. Codeforces Round #558 (Div. 2)C(计算几何,排列组合,模拟)

    #include<bits/stdc++.h>using namespace std;typedef struct{ double k,b;}node;node k[1000007];bo ...

  6. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B. Problems for Round 水题

    B. Problems for Round 题目连接: http://www.codeforces.com/contest/673/problem/B Description There are n ...

  7. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B

    B. Problems for Round time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition)只有A题和B题

    连接在这里,->点击<- A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabyte ...

  9. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

随机推荐

  1. VS2010的单元测试(二)

    四.附加测试属性 附加测试属性,在默认生成的测试代码是使被注释掉的,取消注释就可以使用. 例如,要在执行测试前,输出测试开始时间,在执行测试后,输出测试结束时间.代码如下: [ClassInitial ...

  2. C# wpf window

    使用vs2017 新建wpf 项目 MainWindow 被定义为partial,是因为他要和xaml的一些属性组合在一起,然后再运行起来,这正是 InitailizeCompoent 这个函数要干的 ...

  3. AbstractQueuedSynchronizer解析

    AbstractQueuedSynchronizer简称为AQS,是juc里很基本的一个包,juc里很多工具类是基于AQS实现的,理解了AQS,其它很多juc工具类也会比较清楚了. 1.方法简述 ge ...

  4. TypeScript TSConfig All In One

    TypeScript TSConfig All In One tsconfig.json https://www.typescriptlang.org/tsconfig https://www.typ ...

  5. React Suspense All In One

    React Suspense All In One 挂起让组件在渲染之前"等待"某些东西. 如今,Suspense仅支持一种用例:使用React.lazy动态加载组件. 将来,它将 ...

  6. js 大数计算

    js 大数计算 原理 JavaScript 安全整数 是 -253-1 ~ 253-1 ,即: -9007199254740991 ~ 9007199254740991; 换句话说,整数超过这个范围就 ...

  7. free online business card generator

    free online business card generator 免费在线名片生成器 https://www.logaster.cn/business-card/ https://www.chu ...

  8. ts 修改readonly参数

    readonly name = "xxx"; updateValueAndValidity(): void { // this.name = 'a'; (this as { nam ...

  9. C# 类中操作主窗体控件

    主窗体程序: using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...

  10. Golang 实现 Redis(9): 使用GeoHash 搜索附近的人

    本文是使用 golang 实现 redis 系列的第九篇,主要介绍如何使用 GeoHash 实现搜索附近的人. 搜索附近的POI是一个非常常见的功能,它的技术难点在于地理位置是二维的(经纬度)而我们常 ...