当前位置:网站首页>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).
边栏推荐
- 剑指 Offer 04. 二维数组中的查找
- Hang wait lock vs spin lock (where both are used)
- [turn]: Apache Felix framework configuration properties
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- room数据库的使用
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- Introduction to tools in TF-A
- Using HashMap to realize simple cache
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- [to be continued] [depth first search] 547 Number of provinces
猜你喜欢
YOLOv5-Shufflenetv2
服务熔断 Hystrix
YOLOv5添加注意力机制
sync. Interpretation of mutex source code
[to be continued] [UE4 notes] L2 interface introduction
Sword finger offer 05 Replace spaces
剑指 Offer 05. 替换空格
浅谈JVM(面试常考)
Yolov5 adds attention mechanism
To the distance we have been looking for -- film review of "flying house journey"
随机推荐
TF-A中的工具介绍
剑指 Offer 09. 用两个栈实现队列
剑指 Offer 53 - I. 在排序数组中查找数字 I
游戏商城毕业设计
Talking about JVM (frequent interview)
Control Unit 控制部件
The present is a gift from heaven -- a film review of the journey of the soul
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
Maximum number of "balloons"
【Jailhouse 文章】Look Mum, no VM Exits
C language Essay 1
剑指 Offer 35.复杂链表的复制
Sword finger offer 35 Replication of complex linked list
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Improvement of pointnet++
远程升级怕截胡?详解FOTA安全升级
[to be continued] [UE4 notes] L2 interface introduction
记录QT内存泄漏的一种问题和解决方案
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
Solution to the palindrome string (Luogu p5041 haoi2009)