• 排序和RMQ


    目录

    排序 

    1.糖果传递

    2.七夕祭 

    3.动态中位数

     4.超快速排序

    RMQ

    1.天才的记忆


    排序 

    1.糖果传递

    122. 糖果传递 - AcWing题库

     xi是每一个到下一个位置的操作,也即给多少个,可以是负数(意思是反过来给),要使每个位置都相等,则说明每个数都等于平均数,然后把x1当成自由变量,用x1表示其他的xi,然后令S(i-1)-(i-1)*a为ci然后最后相等于变成abs(x1-c1)的和最小值,而这个绝对值的和可以用绝对值不等式|a|+|b|>=|a+b|换,最后证明得x1等于c[n/2]使得这个绝对值的和最小,也即最少的操作

    1. #include
    2. using namespace std;
    3. typedef long long ll;
    4. const int N=1e6+10;
    5. int n;
    6. int a[N];
    7. ll s[N],c[N];
    8. ll trans(int n,int a[])
    9. {
    10. for(int i=1;i<=n;i++) s[i]=s[i-1]+(ll)a[i];//求前缀和后面用
    11. if(s[n]%n) return -1;//假如不能均分
    12. ll avg=s[n]/n;//平均数
    13. c[1]=0;//第一个是0
    14. for(int i=2;i<=n;i++) c[i]=s[i-1]-(ll)(i-1)*avg;//图片上有说
    15. sort(c+1,c+n+1);//排个序
    16. ll res=0;
    17. ll cavg;//中位数
    18. if(n&1) cavg=c[n/2+1];//奇数直接就是中间哪个
    19. else cavg=(c[n/2]+c[n/2+1])/2;//反之就是中间那两个的中位数
    20. for(int i=1;i<=n;i++) res+=abs(c[i]-cavg);//答案就是每个数减去中位数
    21. return res;
    22. }
    23. int main()
    24. {
    25. scanf("%d",&n);
    26. for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    27. ll t=trans(n,a);//将a进行转换
    28. printf("%lld\n",t);
    29. return 0;
    30. }

    2.七夕祭 

    105. 七夕祭 - AcWing题库

    这题相比上面那题就是多了个列就判断两个方向是否满足即可

    1. #include
    2. using namespace std;
    3. typedef long long ll;
    4. const int N=1e5+10;
    5. int n,m;
    6. int col[N],row[N],s[N];
    7. ll c[N];
    8. ll trans(int n,int a[])
    9. {
    10. for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];//求前缀和后面用
    11. if(s[n]%n) return -1;//假如不能均分
    12. int avg=s[n]/n;//平均数
    13. c[1]=0;//第一个是0
    14. for(int i=2;i<=n;i++) c[i]=s[i-1]-(ll)(i-1)*avg;//图片上有说
    15. sort(c+1,c+n+1);//排个序
    16. ll res=0;
    17. ll cavg;//中位数
    18. if(n&1) cavg=c[n/2+1];//奇数直接就是中间哪个
    19. else cavg=(c[n/2]+c[n/2+1])/2;//反之就是中间那两个的中位数
    20. for(int i=1;i<=n;i++) res+=abs(c[i]-cavg);//答案就是每个数减去中位数
    21. return res;
    22. }
    23. int main()
    24. {
    25. int T;
    26. scanf("%d%d%d",&n,&m,&T);
    27. int x,y;
    28. while(T--)
    29. {
    30. scanf("%d%d",&x,&y);
    31. row[x]++,col[y]++;//行跟列这个位置+1个
    32. }
    33. ll t1=trans(n,row),t2=trans(m,col);//算一下最小步数
    34. if(t1!=-1&&t2!=-1) printf("both %lld\n",t1+t2);
    35. else if(t1!=-1) printf("row %lld\n",t1);
    36. else if(t2!=-1) printf("column %lld\n",t2);
    37. else puts("impossible");
    38. return 0;
    39. }

    3.动态中位数

    106. 动态中位数 - AcWing题库

    这题用堆顶堆来做,答案就是下面那个堆的堆顶

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int T;
    6. scanf("%d",&T);
    7. while(T--)
    8. {
    9. int p,n,x;
    10. scanf("%d%d",&p,&n);
    11. printf("%d %d\n",p,(n+1)/2);
    12. priority_queue<int> down;//定义一个大根堆
    13. priority_queue<int,vector<int>,greater<int>> up;//定义一个小根堆
    14. int cnt=0;
    15. for(int i=1;i<=n;i++)
    16. {
    17. scanf("%d",&x);
    18. if(down.empty()||x<=down.top()) down.push(x);//假如空或者这个数小于down堆的堆顶元素
    19. else up.push(x);
    20. if(down.size()>up.size()+1) up.push(down.top()),down.pop();//假如下面多的上面的数大于1个
    21. else if(up.size()>=down.size()) down.push(up.top()),up.pop();//假如上面多
    22. if(i&1)//奇数输出
    23. {
    24. printf("%d ",down.top());
    25. if(++cnt%10==0) puts("");
    26. }
    27. }
    28. if(cnt%10) puts("");
    29. }
    30. return 0;
    31. }

     4.超快速排序

    107. 超快速排序 - AcWing题库

    这题就是问逆序对的个数,因为假如有个数ai>ai+1,交换后逆序对会减一,则最少的交换次数就等于逆序对的个数,这题用归并排序求逆序对的个数

    1. #include
    2. using namespace std;
    3. typedef long long ll;
    4. const int N=5e5+10;
    5. int a[N],temp[N];
    6. ll cnt=0;
    7. void msort(int l,int r)
    8. {
    9. if(l>=r) return;
    10. int mid=l+r>>1;
    11. msort(l,mid),msort(mid+1,r);//递归出来左右
    12. int i=l,j=mid+1,k=0;
    13. while(i<=mid&&j<=r)//把左右两边小的存进temp中
    14. if(a[i]<=a[j]) temp[k++]=a[i++];
    15. else
    16. {
    17. cnt+=mid-i+1;//i~mid这段都比a[j]大,则有mid-i+1个逆序对
    18. temp[k++]=a[j++];
    19. }
    20. while(i<=mid) temp[k++]=a[i++];
    21. while(j<=r) temp[k++]=a[j++];
    22. for(int i=l,k=0;i<=r;i++,k++) a[i]=temp[k];//把排序好的数给回a
    23. }
    24. int main()
    25. {
    26. int n;
    27. while(scanf("%d",&n),n)
    28. {
    29. cnt=0;//置0
    30. for(int i=0;iscanf("%d",&a[i]);
    31. msort(0,n-1);//跑一遍归并排序
    32. printf("%lld\n",cnt);//输出答案
    33. }
    34. return 0;
    35. }

    RMQ

    RMQ也叫ST表和跳表,用来求一个区间的某个性质来用,有数的更改就不能用了

    1.天才的记忆

    天才的记忆 (nowcoder.com) 

    题意就是随便问一段l,r之间的最大值,可以用线段树来做,这题因为没有修改可以用RMQ来做

     询问就是先算出2^k<=r-l+1,求出k,然后答案就是f[l,k]和f[r-2^k-1,k]的最大值,中间虽然有覆盖的区间,但是不影响求最大值,然后求log可以用数学的换地公式来做,因为cmath库中有log函数但是以10为底,我们还可以预处理出来log以2为底的k也行,用的时候直接查表 

    1. #include
    2. using namespace std;
    3. typedef long long ll;
    4. const int N=2e5+10,M=18;
    5. int a[N];
    6. int f[N][M];
    7. int n,m;
    8. void init()
    9. {
    10. for(int j=0;j//枚举长度
    11. for(int i=1;i+(1<-1<=n;i++)//枚举左边界
    12. if(!j) f[i][j]=a[i];
    13. else f[i][j]=max(f[i][j-1],f[i+(1<-1)][j-1]);//状态转移
    14. }
    15. int query(int l,int r)
    16. {
    17. int len=r-l+1;
    18. int k=log(len)/log(2);//获取k
    19. return max(f[l][k],f[r-(1<1][k]);
    20. }
    21. int main()
    22. {
    23. scanf("%d",&n);
    24. for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    25. init();//预处理RMQ的f函数
    26. scanf("%d",&m);
    27. int l,r;
    28. while(m--)
    29. {
    30. scanf("%d%d",&l,&r);
    31. printf("%d\n",query(l,r));//输出询问
    32. }
    33. return 0;
    34. }

  • 相关阅读:
    Leetcode刷题Day8-------------字符串
    SpringBoot/Spring AOP默认动态代理方式
    堆--数据流中第K大元素
    【Tensorboard】工具使用细节记录,实现训练数据保存及可视化
    Java枚举类(Enum)和注解(Annotation)讲解——Java第十二讲
    Django基础二静态文件和ORM
    springboot整合redis
    .NET周刊【7月第1期 2024-07-07】
    YGG 购买了 DigiDaigaku 公司的 NFT,入局 Limit Break 的“免费拥有”游戏
    正点原子嵌入式linux驱动开发——TF-A移植
  • 原文地址:https://blog.csdn.net/m0_63729880/article/details/127677415