hoj 2430 Counting the algorithms (树状数组)
来源:互联网 发布:android 查看端口占用 编辑:程序博客网 时间:2024/05/18 18:54
Counting the algorithms
题目链接:点击打开链接
题目描述:在一个1到2*N的区间内,1到N这N个数都出现了两次,相同的数做差,做完之后删除,其他的元素位置也会被改变,所以用树状数组统计区间内的元素个数。加个位置数组快速找到相同数的位置。
代码如下:
#include <cstring>#include <cstdio>#include <algorithm>using namespace std;#define MAXN 200005int n;int data[MAXN];int tree[MAXN];int vi[MAXN/2];int addre[MAXN/2];void add(int x,int num){ for(int i=x;i<=2*n;i+=i&-i) tree[i]+=num;}int read(int x){ int sum=0; for(int i=x;i>0;i-=i&-i) sum+=tree[i]; return sum;}int main(){ int ans; while(~scanf("%d",&n)) { ans=0; memset(tree,0,sizeof(tree)); memset(vi,0,sizeof(vi)); memset(addre,0,sizeof(addre)); for(int i=1;i<=2*n;i++) { add(i,1); scanf("%d",&data[i]); if(addre[data[i]]==0) addre[data[i]]=i; } for(int i=2*n;i>0;i--) { if(vi[data[i]]==0) { int tem=read(i)-read(addre[data[i]]); // printf("%d %d %d %d \n",i,addre[data[i]],read(i),read(addre[data[i]])); ans+=tem; add(i,-1);add(addre[data[i]],-1); vi[data[i]]==1; } } printf("%d\n",ans); } return 0;}
0 0
- hoj 2430 Counting the algorithms +树状数组
- hoj 2430 Counting the algorithms (树状数组)
- hoj 2430 Counting the algorithms(树状数组,贪心)
- HOJ 2430——Counting the algorithms(树状数组+贪心)
- hoj 2430 Counting the algorithms
- HIT 2430 Counting the algorithms (树状数组)
- hoj Counting the algorithms
- HOJ2430 Counting the Algorithms 树状数组
- HOJ_2430 Counting the algorithms
- hoj2430 Counting the algorithms
- Counting Black(树状数组)
- hoj 2430 树状数组 简单题
- -----hoj 2275 Number sequence(树状数组)
- hoj (2275)树状数组
- HDU 4358 Boring counting(树状数组)
- HDU3450 Counting Sequences(树状数组+dp)
- hoj 1867 水~~~树状数组
- HOJ 2275——Number sequence(树状数组)
- AbstractQueuedSynchronizer简单解析
- veloctiy常用操作
- easyui select combobox 获取下拉选项值
- dpdk kni例子程序分析
- 关于thrift的使用
- hoj 2430 Counting the algorithms (树状数组)
- Toast管理——ToastUtils
- spring与mybatis三种整合方法
- 第二天:Interactive Optimizer 教程
- Windows环境下TensorFlow的安装及如何在Jupyter Notebook中使用TensorFlow
- Hive如何添加第三方JAR
- Java的内存回收——Java引用的种类
- JNDI是什么
- JavaScript-逗号操作符