poj 2299
来源:互联网 发布:淘宝国产高达模型店 编辑:程序博客网 时间:2024/05/22 03:44
题目:http://poj.org/problem?id=2299
树状数组 +离散化。
我一开始用的 map+树状数组暴搞。。哪知超时了。 这题如果用树状数组,还是需要离散化才行。。
离散化的步骤就是, 先用一个结构体,用num 保存 原来的数,id保存原来数组所在的位置。。然后按num排序,那么得到一个从小到大有序的序列。 然后通过id找到原数组所在的位置,并用另一个数组存放 通过映射得到的新的类似原来(比原来数组的值缩小了,但是相对应的大小关系不变)序列的数组。
#include <cmath>#include <ctime>#include <iostream>#include <string>#include <vector>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#include <map>#include <set>#include <algorithm>#include <cctype>#include <stack>#include <deque>using namespace std;typedef long long LL;#define EPS 10e-9#define INF 0x3f3f3f3f#define REP(i,n) for(int i=0; i<(n); i++)const int maxn = 500500;int a[maxn],c[maxn],aa[maxn];struct node{ int num,id; bool operator <(const node & b) const { return num<b.num; }}b[maxn];int lowbit(int x){ return x&-x;}int add(int x){ while(x<maxn){ c[x]+=1; x+=lowbit(x); }}LL sum(int x){ LL ret=0; while(x>0){ ret+=c[x]; x-=lowbit(x); } return ret;}int main(){ int n; while(scanf("%d",&n)!=EOF){ memset(c,0,sizeof(c)); if(n==0) break; for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i].num=a[i],b[i].id=i; sort(b+1,b+1+n); aa[ b[1].id ]=1; for(int i=2;i<=n;i++){ if(b[i].num==b[i-1].num){ aa[ b[i].id ]=aa[ b[i-1].id ]; } else aa[ b[i].id ]=i; } LL ans=0; for(int i=n;i>=1;i--){ ans+=sum(aa[i]-1); add(aa[i]); } printf("%lld\n",ans); } return 0;}
- POJ-2299-POJ 2299
- POJ 2299
- Poj 2299
- POJ 2299
- poj 2299
- poj 2299
- poj 2299
- poj.2299
- POJ 2299
- poj 2299
- poj-2299
- poj 2299
- poj-2299
- poj 2299
- POJ 2299
- poj 2299
- Poj 2299
- POJ 2299
- 安装Win8的两种方法
- .net连接MySQL的方法
- java synchronized详解
- asp.net用户登入验证
- 指针与引用的区别
- poj 2299
- aspx与aspx.cs的关系
- 周鸿祎:需格外注意的五点用户体验
- x86架构下的物理内存-通过linux概述
- Uva 10970 大块巧克力(记忆式递归or规律)
- 强劲大小核结构 三星将推八核处理器
- java多线程面试题
- 马化腾:让产品自己召唤人
- JQuery事件对象属性小结