SGU-180-Feel Good
2014-04-26 16:34
197 查看
这个题其实就是求逆序数个数
用树状数组求逆序数个数,求的时候一定要注意离散化的时候需要去重~这点WA了N次
代码:
用树状数组求逆序数个数,求的时候一定要注意离散化的时候需要去重~这点WA了N次
代码:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=1e5+1000; const int inf=1<<30; int n,g[maxn]; struct Node { int val; int id; int index; bool operator < (const Node &a)const{ return val<a.val; } }a[maxn]; int pos[maxn]; int lowbit(int x) { return x&(-x); } void Update(int x,int val) { for(;x<=n;x+=lowbit(x)) g[x]+=val; } int Sum(int x) { int ans=0; for(;x>0;x-=lowbit(x)) ans+=g[x]; return ans; } int main() { while(scanf("%d",&n)!=EOF) { long long ans=0; memset(g,0,sizeof(g)); for(int i=1;i<=n;i++) { a[i].index=i; scanf("%d",&a[i].val); } sort(a+1,a+n+1); int cnt=0; for(int i=1;i<=n;i++) { if(i>1&&a[i].val==a[i-1].val) pos[a[i].index]=cnt; else pos[a[i].index]=++cnt; } for(int i=1;i<=n;i++) { Update(pos[i],1); ans+=i-Sum(pos[i]); } printf("%lld\n",ans); } return 0; }
相关文章推荐
- UVALive 3517:Feel Good(单调栈 Grade C)
- SGU180——Inversions
- poj 2796 Feel Good
- POJ 2796:Feel Good 单调栈经典题
- TOJ2333 Feel Good
- POJ 2796 : Feel Good - 区间运算最值
- Poj-2796 Feel Good
- SGU_180_Inversions
- POJ 2796:Feel Good 单调栈经典题
- Active Reading: Unit 1 Chapter2: Food that makes you feel good
- goodfeel.info
- POJ2796 - Feel Good
- SGU - 180 (树状数组+离散化)
- POJ 2796 Feel Good
- poj2796 Feel Good
- poj 2796Feel Good
- POJ 2796:Feel Good (单调栈)
- Feel Good 单调递增栈+前缀和
- poj2796Feel Good 单调栈
- POJ 2796 Feel Good