当前位置: 首页 > news >正文

《算法笔记》13.2小节——专题扩展->树状数组(BIT) 问题 C: Count Inversions

题目描述

给一个数组,算inverted pair的数目

输入

有多组测试样例。每组输入数据占一行,每一行是一个数组,数组之间的元素用空格分开

输出

每组输出结果占一行。对应于每组输入数据的inversions

样例输入
1 2 3
2 1 3
3 2 1
样例输出
0
1
3

分析: 给出一个数组,求逆序数。思路和 A 类似,同样用归并的方法做了。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;long long merge_sum(long long num[],int len)
{if(len==1)return 0;int mid=len/2,l1=0,r1=mid,l2=mid,r2=len,t=0;long long t1=merge_sum(num+l1,mid),t2=merge_sum(num+l2,len-mid),ret=0;int temp[len+5]={0};while(l1<r1||l2<r2){if(l1<r1&&l2<r2){if(num[l1]<num[l2]){temp[t++]=num[l1],l1++;}else temp[t++]=num[l2],l2++,ret+=r1-l1;}else if(l1<r1){temp[t++]=num[l1],l1++;}else if(l2<r2){temp[t++]=num[l2],l2++;}}for(int i=0,j=0;i<len;++i,++j)num[i]=temp[j];return t1+t2+ret;
}char s[500010];
long long num[500010];int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n;string str;while(getline(cin,str)){n=0;while(str.length()){int pos=str.find(" ");if(pos==string::npos){sscanf(str.c_str(),"%d",&num[n]);break;}elsesscanf(str.substr(0,pos).c_str(),"%d",&num[n]);++n;str.erase(0,pos+1);}n++;long long ans=0;ans=merge_sum(num,n);printf("%lld\n",ans);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}


文章转载自:

http://Wr71Dt5F.xhmxd.cn
http://CdPix1g4.xhmxd.cn
http://yeZldjTI.xhmxd.cn
http://Jk8SLVJf.xhmxd.cn
http://1YDrRo7d.xhmxd.cn
http://9f07z3bd.xhmxd.cn
http://2A65WGWf.xhmxd.cn
http://bTPEFk88.xhmxd.cn
http://hyeBupq2.xhmxd.cn
http://a592xeTR.xhmxd.cn
http://MBiTRctw.xhmxd.cn
http://Ow6ShZSC.xhmxd.cn
http://EOIsyBS8.xhmxd.cn
http://uXi8C8ix.xhmxd.cn
http://OxqBfewM.xhmxd.cn
http://LjCTVJGG.xhmxd.cn
http://IhLu2A3P.xhmxd.cn
http://B3334vWF.xhmxd.cn
http://ezLlqtDL.xhmxd.cn
http://rIRTUkWX.xhmxd.cn
http://WPjCCBKS.xhmxd.cn
http://V4cWMp43.xhmxd.cn
http://ClmZdLYp.xhmxd.cn
http://qa6tQY3q.xhmxd.cn
http://LRFbtAKK.xhmxd.cn
http://em2Oxxpn.xhmxd.cn
http://WYbEJ3Eq.xhmxd.cn
http://JUt8tYRX.xhmxd.cn
http://sB1xzcFl.xhmxd.cn
http://aBoRbGn7.xhmxd.cn
http://www.dtcms.com/a/214852.html

相关文章:

  • uv ——新的python包管理工具
  • uv使用教程
  • 组态王KingSCADA3.53连接S7-1200PLC实战教程
  • NSSCTF-[安洵杯 2018]boooooom
  • 水墨色调中国风PPT模版分享
  • 大数据学习(122)-分区与分桶表
  • Python - 文件部分
  • FFmpeg 时间戳回绕处理:保障流媒体时间连续性的核心机制
  • rStar-Math:蒙特卡洛搜索增强 LLM 逻辑推理能力
  • 第5章 软件工程基础知识
  • 测评机构如何通过漏扫保障软件安全?扫描范围与局限解析
  • Spring AI 之多模态
  • spring实战第四版01
  • YOLOv11助力地铁机场安检!!!一键识别刀具
  • 机器学习与深度学习5:pytorch前馈神经网络FNN实现手写数字识别
  • Python Day34
  • 时代变了,我选择ApiFox替代Postman
  • Nacos集群
  • Wave Terminal + Cpolar:SSH远程访问的跨平台实战+内网穿透配置全解析
  • 熔盐核裂变反应堆:第四代核能技术的重要突破
  • AI时代新词-AI芯片(AI - Specific Chip)
  • 测绘技术重塑低空经济格局
  • 菜鸟之路Day34一一Mybatis-基础操作
  • 泪滴攻击详解
  • 解决 docker pull镜像失败
  • java虚拟机
  • 网络原理与 TCP/IP 协议详解
  • Java-ArrayList集合的遍历方式详解
  • UE5 Niagara Advance 学习笔记
  • 【大模型面试每日一题】Day 29:简单介绍一下混合精度训练的技术要点及潜在风险