当前位置:网站首页>Codeforces Round #716 (Div. 2) D. Cut and Stick
Codeforces Round #716 (Div. 2) D. Cut and Stick
2022-07-05 05:30:00 【solemntee】
obviously :
For an interval, we only need to deal with the element that appears most times
( We call the remaining elements other elements )
The optimal strategy is to take two maximum elements and one other element to form a sequence
So the answer to a certain interval is
m a x ( most many element plain individual Count − Its He element plain , 1 ) max( Maximum number of elements - Other elements ,1) max( most many element plain individual Count − Its He element plain ,1)
So for a single interval , We just need to find out the number of elements that appear most often .
This is easy to make .
Because each time add a number or delete a number , The number of elements with the highest number of occurrences in the interval will only increase or decrease by one , So we only need one c n t [ i ] cnt[i] cnt[i] Record i i i Number of occurrences , r n k [ i ] rnk[i] rnk[i] The number of occurrences recorded is i i i Number of elements of times .
Every maintenance c n t [ i ] cnt[i] cnt[i] and r n k [ i ] rnk[i] rnk[i] that will do .( You can know that the complexity of this is very low , Because of the above " The number of elements with the most occurrences will only increase or decrease by one ")
At the same time, because the inquiry is a continuous interval , So I think of Mo team , The complexity can be controlled within O ( n n ) O(n\sqrt n) O(nn) within .
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=3e5+50;
int n,q;
int a[maxn],ans[maxn],rnk[maxn],cnt[maxn];
int nowl=1,nowr=0,nowans=0;
struct qu
{
int l,r,num;
}query[maxn];
bool cmp(qu a,qu b)
{
int t=sqrt(n);
if(a.r/t!=b.r/t)return a.r/t<b.r/t;
return a.l<b.l;
}
void add(int x)
{
if(cnt[a[x]]>0)rnk[cnt[a[x]]]--;
cnt[a[x]]++;
rnk[cnt[a[x]]]++;
nowans=max(nowans,cnt[a[x]]);
}
void del(int x)
{
rnk[cnt[a[x]]]--;
cnt[a[x]]--;
rnk[cnt[a[x]]]++;
while(rnk[nowans]==0)nowans--;
}
int main()
{
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=q;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
query[i].num=i;
}
sort(query+1,query+1+q,cmp);
for(int i=1;i<=q;i++)
{
while(nowr<query[i].r)
{
nowr++;
add(nowr);
}
while(nowl>query[i].l)
{
nowl--;
add(nowl);
}
while(nowr>query[i].r)
{
del(nowr);
nowr--;
}
while(nowl<query[i].l)
{
del(nowl);
nowl++;
}
// cout<<nowans<<' '<<"aa"<<endl;
if(nowans<=(query[i].r-query[i].l+1+1)/2)ans[query[i].num]=1;
else ans[query[i].num]=max(1,nowans-(query[i].r-query[i].l+1-nowans));
}
for(int i=1;i<=q;i++)printf("%d\n",ans[i]);
return 0;
}
But as far as this topic is concerned, we can do many things , There are two kinds of feelings that are very meaningful .
1、 If a number in the interval exceeds ( n + 1 ) / 2 (n+1)/2 (n+1)/2 Time , Then it must appear in some sub interval more than ( n + 1 ) / 2 (n+1)/2 (n+1)/2 Time ( Otherwise, the contradiction ), So we only need to use the segment tree to deal with each interval exceeding ( n + 1 ) / 2 (n+1)/2 (n+1)/2 The number of times , And then separately c h e c k check check that will do .
2、 If a number in the interval exceeds ( n + 1 ) / 2 (n+1)/2 (n+1)/2 Time , Then let's take a random position , The probability of randomly reaching this number is greater than 1 / 2 1/2 1/2, So we randomly n n n Time , c h e c k check check Value obtained each time , The probability of finding this number is very high .
notes :check Just a simple two-point subscript ~
Also learned by the way , For occurrences greater than ( n + 1 ) / 2 (n+1)/2 (n+1)/2 We have a Moore voting algorithm that can O ( n ) O(n) O(n) Find out …
b y t h e w a y by\,the\,way bytheway We can also review some cold knowledge about the median , For an interval, we can divide it into two numbers , Then count the number of numbers greater than this number and less than this number , Come on c h e c k check check, Complexity is O ( n l o g n ) O(nlogn) O(nlogn).
边栏推荐
- Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- [speed pointer] 142 circular linked list II
- 服务熔断 Hystrix
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
- [轉]: OSGI規範 深入淺出
- [sum of two numbers] 169 sum of two numbers II - enter an ordered array
- Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
- Pointnet++学习
- GBase数据库助力湾区数字金融发展
猜你喜欢
![[turn]: OSGi specification in simple terms](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[turn]: OSGi specification in simple terms

Romance of programmers on Valentine's Day

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

剑指 Offer 06.从头到尾打印链表

剑指 Offer 53 - I. 在排序数组中查找数字 I

Reader writer model

第六章 数据流建模—课后习题

剑指 Offer 53 - II. 0~n-1中缺失的数字
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces

YOLOv5添加注意力机制
随机推荐
[turn to] MySQL operation practice (I): Keywords & functions
[turn to] MySQL operation practice (III): table connection
Pointnet++ learning
[allocation problem] 135 Distribute candy
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
Fragment addition failed error lookup
利用HashMap实现简单缓存
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
读者写者模型
[turn]: OSGi specification in simple terms
On-off and on-off of quality system construction
National teacher qualification examination in the first half of 2022
游戏商城毕业设计
Haut OJ 1221: a tired day
Daily question - longest substring without repeated characters
使用Electron开发桌面应用
Web APIs DOM节点
远程升级怕截胡?详解FOTA安全升级
Solon 框架如何方便获取每个请求的响应时间?