[luogu1908]逆序对(upper_bound)

对于给定的一段正整数序列,逆序对就是序列中ai>aj且i<j的有序对

用upper_bound法求逆序对,Code很棒

据说有用树状数组和线段树写逆序对的,这里用upper_bound水一发。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 #include<algorithm>
 5 using namespace std;
 6 int n;
 7 vector<int> shu;
 8 int main(){
 9     scanf("%d",&n);    
10     long long s=0,a;
11     int kk[60000];
12     for(int i=0;i<n;i++){
13         scanf("%d",&kk[i]);//核心代码:
14         s+=shu.end()-upper_bound(shu.begin(),shu.end(),kk[i]);
15         shu.insert(upper_bound(shu.begin(),shu.end(),kk[i]),kk[i]);
16     }
17     printf("%d",s);
18     return 0;
19 }
原文地址:https://www.cnblogs.com/Fylsea/p/7800874.html