hoj 2430 Counting the algorithms
来源:互联网 发布:als矩阵 编辑:程序博客网 时间:2024/05/04 21:09
题解:
这个题的关键在于:如果是两个包含的区间则先删除外边的最优。如果是交叉区间的情况,无论先删哪个,答案都是一样的。所以采取如下策略,从左边向右边,一个一个删除。至于统计动态的两个相邻元素之间的数目,可以使用树状数组。
时间复杂度O(nlogn)
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=200010;int f[maxn],l[maxn],a[maxn];long long tree[maxn];int n;inline int lowbit(int x){ return x&(-x);}void add(int pos,int x){ for(int i=pos;i<=2*n;i+=lowbit(i)) tree[i]+=x;}int getsum(int pos){ int sum=0; for(int i=pos;i>0;i-=lowbit(i)) sum+=tree[i]; return sum;}int main(){ while(scanf("%d",&n)!=EOF) { memset(f,0,sizeof(f)); memset(l,0,sizeof(l)); for(int i=1;i<=2*n;i++) { scanf("%d",&a[i]); f[a[i]]==0?f[a[i]]=i:l[a[i]]=i; add(i,1); } long long ans=0; for(int i=1;i<=2*n;i++) if(getsum(i)-getsum(i-1)>0) { ans=ans+getsum(l[a[i]])-getsum(i-1)-1; add(i,-1); add(l[a[i]],-1); } printf("%lld\n",ans); } return 0;}
- hoj 2430 Counting the algorithms
- hoj 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_2430 Counting the algorithms
- hoj2430 Counting the algorithms
- 【HOJ2430】Counting the Algorithms-区间维护
- HOJ2430 Counting the Algorithms 树状数组
- The Program of Algorithms ------- Sorting in Linear Time---- Counting Sort
- HOJ The Colored Cubes
- HOJ 3074 The Position
- HOJ 2785 The Game
- Hoj 1140 The Game
- Hoj 1760 The jackpot
- The Importance of Algorithms
- 笔试面试常见问题问答
- 寻找数组中的第二大数
- HDU 4405 Aeroplane chess
- 黑马程序员07——杂项、构造方法、索引器
- JavaScript+CSS生成静态水平选项卡效果
- hoj 2430 Counting the algorithms
- 如何将VS2005的默认开发语言设置成C#
- 各种编程风格
- php起航------php学习资源
- Android Animation使用详解(sample在资源里)
- C和指针笔记---数据(2)
- VS2005中如何切换C#和C++的编辑环境
- 10 个帮你学习快速编码的网站
- BS结构与CS结构