P2681 众数
题目背景
Alice和Bob玩游戏
题目描述
Alice现在有一个序列a1、a2...an
现在她需要Bob支持询问一个区间内的众数,还要支持修改一个位置的ai
输入输出格式
输入格式:
第一行两个整数n,m
第二行n个整数,表示a1..an
接下来m行,每行3个整数,flag,x,y
如果flag=0,表示询问[x,y]区间内的众数,如果有多个输出较小的
如果flag=1,表示将a[x]改为y
输出格式:
对于每个flag=0的询问,每行输出一个整数表示答案
输入输出样例
输入样例#1:
5 31 1 2 2 10 1 41 2 30 1 4
输出样例#1:
12
说明
对于100%的数据n,m<=1000
对于查询操作满足x<=y
任意时刻0<ai<=1e9
#include#include #include #include #define N 1010using namespace std;int n,m,p,x,y,s,sum,ans,a[N],b[N],maxn;int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int work(int x,int y){ for(int i=x;i<=y;i++) b[++s]=a[i]; sort(b+1,b+1+s); for(int i=2;i<=s;i++) if(b[i]==b[i-1]) sum++; else { if(sum>maxn) maxn=sum,ans=b[i-1]; sum=1; } if(sum>maxn) maxn=sum,ans=b[s]; printf("%d\n",ans);}int main(){ n=read(),m=read(); for(int i=1;i<=n;i++) a[i]=read(); while(m--) { s=0,maxn=0,sum=1; p=read(),x=read(),y=read(); if(p==0) work(x,y); else a[x]=y; } return 0;}