总结
做的很坎坷。。。
一些细节上的玄学bug
(上次也一样。。。)
还是要加强基础!!!
T1 x的实根
不贴了,就是水题。。。
T2 二分查找
就是lowerbound
T3逆序对
1.树状数组
2.归并排序
注意:
int q[N]={};
这个{}隐含了一个On的初始化!!!
代码
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int N=1e5+100;
const double epc=0.00000000001;
int m,n;
int a[N];
ll num=0;
void guibing(int st,int ed){if(st==ed) return;int mid=(st+ed)>>1;guibing(st,mid);guibing(mid+1,ed);
// printf("st=%d,ed=%d\n",st,ed);int p1=st,p2=mid+1;int nm=0;int q[N];while(p1<=mid&&p2<=ed){if(a[p1]<=a[p2]){q[++nm]=a[p1++];}else{q[++nm]=a[p2++];num+=mid-p1+1;
// printf("dksfh\n");}
// printf("p1=%d p2=%d num=%lld mid=%d\n",p1,p2,num,mid);}while(p1<=mid){q[++nm]=a[p1++];}while(p2<=ed){q[++nm]=a[p2++];}for(int i=1,pl=st;i<=nm;i++,pl++){a[pl]=q[i];}
// printf("\n");
}
int main(){scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]);guibing(1,n);printf("%lld",num);return 0;
}
/*
4
3 2 3 2
*/
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int N=1e5+100;
const double epc=0.00000000001;
int m,n;
int a[N],b[N];
int tree[N];
ll ans;
bool cmp(int x,int y){return x>y;
}
void add(int x){for(int i=x;i<=N;i+=i&(-i)){tree[i]++;
// printf("pl=%d tree=%d\n",i,tree[i]);}return;
}
int query(int x){int res=0;
// printf("query------%d:\n",x);for(;x;x-=x&(-x)){res+=tree[x];
// printf("pl=%d res=%d\n",x,res);}return res;
}
int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}sort(b+1,b+1+n);int num=unique(b+1,b+1+n)-b-1;for(int i=n;i>=1;i--){a[i]=lower_bound(b+1,b+1+num,a[i])-b;
// printf("ooooo%d:\n",a[i]);ans+=query(a[i]-1);add(a[i]);}printf("%lld",ans);return 0;
}
/*
5
3 2 3 2 1
*/