社区讨论
P1908逆序对 35分求助!!!
P1908逆序对参与者 4已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @lo9fqyx3
- 此快照首次捕获于
- 2023/10/28 10:41 2 年前
- 此快照最后确认于
- 2023/10/28 10:41 2 年前
CPP
#include <bits/stdc++.h>
using namespace std;
const int MAXN=500010;
typedef long long ll;
ll n,m;
ll c[MAXN],a[MAXN],b[MAXN];
inline void add(register int x){
while(x<=n){
c[x]++;
x+=(x&-x);
}
}
inline int sum(register int x){
register int s=0;
while(x>0){
s+=c[x];
x-=x&(-x);
}
return s;
}
bool cmp(const int &x,const int &y){
return b[x]>b[y];
}
int main(){
std::ios::sync_with_stdio(false);
ll ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>b[i];
a[i]=i;
}
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
add(a[i]);
ans+=sum(a[i]-1);
}
cout<<ans;
}
回复
共 4 条回复,欢迎继续交流。
正在加载回复...