时间限制:1 秒 空间限制:131072 KB
 

定义一个区间的值为其众数出现的次数
现给出n个数,求将所有区间的值排序后,第K大的值为多少。

众数(统计学/数学名词)_百度百科

Input
第一行两个数n和k(1<=n<=100000,k<=n*(n-1)/2)
第二行n个数,0<=每个数<2^31
Output
一个数表示答案。
Input示例
4 2
1 2 3 2
Output示例
2
思路:二分答案t,统计众数出现次数大于等于t的区间有多少个。
枚举右端点R,计算左端点L最大为多少,使得区间[L,R]的值大于等于t,对于每个R他对答案贡献为L。
通过线性扫一遍找出每一个数的前面第t-1个与他相同的数字,记其位置为b[i],若不存在则为0。
若R增加,则[L,R+1]的值也必定大于等于t,所以新的L=max(L,b[R+1]),这样就可以找出每一个R对应的L,O(n)计算出答案。
总复杂度O(nlogn)
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 int id[100005];
12 int cnt[100005];
13 int cp[100005];
14 int str[100005];
15 LL tt[100005];
16 typedef struct pp
17 {
18 LL x;
19 int id;
20 } ss;
21 ss ans[100005];
22 LL check(int mid);
23 bool cmp(pp n,pp m)
24 {
25 return n.x<m.x;
26 }
27 LL n,m;
28 int main(void)
29 {
30 int i,j,k;
31 while(scanf("%lld %lld",&n,&m)!=EOF)
32 {
33 memset(cnt,0,sizeof(cnt));
34 for(i=1; i<=n; i++)
35 {
36 scanf("%lld",&ans[i].x);
37 ans[i].id=i;
38 }
39 LL nn=ans[1].x;
40 int mm=1;
41 sort(ans+1,ans+n+1,cmp);
42 for(i=1; i<=n; i++)
43 {
44 if(ans[i].x!=nn)
45 {
46 mm++;
47 nn=ans[i].x;
48 }
49 tt[ans[i].id]=mm;
50 }
51 for(i=1;i<=n;i++)
52 {
53 ans[i].x=tt[i];
54 }
55 int maxx=0;
56 for(i=1; i<=n; i++)
57 {
58 cnt[ans[i].x]++;
59 if(maxx<cnt[ans[i].x])
60 {
61 maxx=cnt[ans[i].x];
62 }
63 }
64 int l=1;
65 int ask=1;
66 int r=maxx;
67 while(l<=r)
68 {
69 int mid=(l+r)/2;
70 LL ak=check(mid);
71 LL cnt1=(n)*(n-1)/2;
72 if(ak>=m)
73 { ask=mid;
74 l=mid+1;
75 }
76 else
77 { r=mid-1;
78
79 }
80 }printf("%d\n",ask);
81 }
82 return 0;
83 }
84 LL check(int mid)
85 {
86 int i,j,k;
87 LL sum=0;
88 if(mid==1)
89 return n*(n-1)/2;
90 else
91 {
92 memset(id,0,sizeof(id));
93 memset(cnt,0,sizeof(cnt));
94 memset(str,0,sizeof(str));
95 cnt[ans[1].x]++;str[ans[1].x]=1;id[1]=0;
96 for(i=2; i<=n; i++)
97 {
98 cnt[ans[i].x]++;
99 if(cnt[ans[i].x]==1)
100 {
101 str[ans[i].x]=i;
102 id[i]=0;
103 }
104 else if(cnt[ans[i].x]==mid)
105 {
106 id[i]=str[ans[i].x];
107 }
108 else if(cnt[ans[i].x]<mid)
109 {
110 id[i]=0;
111 }
112 else if(cnt[ans[i].x]>mid)
113 {
114 while(cnt[ans[i].x]>mid)
115 {
116 str[ans[i].x]++;
117 if(ans[str[ans[i].x]].x==ans[i].x)
118 {
119 cnt[ans[i].x]--;
120 break;
121 }
122 }id[i]=str[ans[i].x];
123 }
124 }
125 int L=0;
126 LL sum=0;
127 for(i=1; i<=n; i++)
128 {
129 L=max(L,id[i]);
130 sum+=L;
131
132 }printf("\n");
133 return sum;
134 }
135 }

1686 第K大区间的更多相关文章

  1. 51nod 1686 第k大区间

    1686 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. ...

  2. 1686 第K大区间(尺取+二分)

    1686 第K大区间 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. ...

  3. 51nod 1686 第K大区间2

    1685 第K大区间2 定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. 众数(统计学/数学名词)_百度百科 Input 第一行两个数n和k(1<=n ...

  4. 51NOD 1686 第K大区间 二分

    第k大区间   定义一个区间的值为其众数出现的次数.现给出n个数,求将所有区间的值排序后,第K大的值为多少. Input   第一行两个数n和k(1<=n<=100000,k<=n* ...

  5. 51Nod——T 1686 第K大区间

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...

  6. 51nod 1686 第K大区间【离散化+二分】

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 定义一个区间的值为其众数出现的次数. 现给出n ...

  7. 51Nod 1686 第K大区间(离散化+尺取法)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 题意: 思路: 第K大值,所以可以考虑二分法,然后用尺取法去扫描, ...

  8. 【题解】51nod 1686第K大区间

    成功的秘诀,在于克服自己看题解的冲动……[笑哭].自己A掉这题还是灰常开心的~ 以及爱死 two - pointer ! two - pointer 大法是真的好哇……这个题目有上一题的经验:求第\( ...

  9. 51nod 1686 第K大区间 二分瞎搞

    题目: 定义一个区间的值为其众数出现的次数. 现给出n个数,求将所有区间的值排序后,第K大的值为多少. 题解: 答案明显单调,我们考虑二分答案. 转化为判定问题后我们需要观察到一个性质: 如果一个区间 ...

随机推荐

  1. day11 四层负载均衡和http协议

    day11 四层负载均衡和http协议 四层负载均衡和七层负载均衡的区别 四层和七层负载均衡的区别 四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发.由此可以看出,七层负 ...

  2. gitlab之实战部署

    #:准备Java环境,安装jdk root@ubuntu:~# cd /usr/local/src/ root@ubuntu:/usr/local/src# ls jdk-8u191-linux-x6 ...

  3. vue-cli2嵌入html

    1.使用iframe <!-- 相对路径/绝对路径 --> <iframe src="../../../static/zsw.html"></ifra ...

  4. 【编程思想】【设计模式】【创建模式creational】Pool

    Python版 https://github.com/faif/python-patterns/blob/master/creational/pool.py #!/usr/bin/env python ...

  5. Spring Boot对静态资源的映射规则

    规则一:所有 " /webjars/** " 请求都去classpath:/META-INF/resources/webjars/找资源 webjars:以jar包的方式引入静态资 ...

  6. 【Python】matplotlib直方图纵轴显示百分比

    其实很简单,就是算了一下百分比权重,乘以了一个权重值 import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatt ...

  7. 11、Redis的配置文件

    Redis的配置文件 一.Redis配置文件简介 Redis是通过配置文件启动的 Redis对大小写字母不敏感 Redis基本上环境搭建都在配置文件 关于Redis配置文件位置是安装时放的位置,关于R ...

  8. 使用hbuilder打包vue项目容易出现的坑点

    1.打包后手机打开"该app专为旧版本安卓"问题解决(在hbuilder中设置) 打开manifest.json 然后 2.打包后app打开显示白屏. 路径问题:在webpack中 ...

  9. Windows下安装xampp的PHP扩展(redis为例)

    (1)PHP的windowns扩展下载网址:https://windows.php.net/downloads/pecl/releases/ (2)Ctrl+f查询你要下载的扩展名 注意:扩展的版本要 ...

  10. 09 - Vue3 UI Framework - Table 组件

    接下来做个自定义的表格组件,即 table 组件 返回阅读列表点击 这里 需求分析 开始之前我们先做一个简单的需求分析 基于原生 table 标签的强语义 允许用户自定义表头.表体 可选是否具有边框 ...