标签:pac nbsp test 题解 long 数组 define amp ++
https://codeforces.com/gym/102082
题意:
给一个数组大小不超过1e5,每个数的值也是1e5以内,可以交换相邻两个数,求保证它呈现一个非递减再非递增的趋势的最小交换次数。
#define bug(x,y) cout<<"i="<<x<<": "<<y<<endl #define IO std::ios::sync_with_stdio(0); #include <bits/stdc++.h> #define itor ::iterator using namespace std; typedef long long ll; typedef pair<ll,ll>P; #define pb push_back #define se second #define fi first #define rs o*2+1 #define ls o*2 const int N=1e5+5; int n; int c[N]; int low(int x){ return x&(-x); } void add(int x,int y){ while(x<N){ c[x]+=y; x+=low(x); } } int cal(int x){ int res=0; while(x){ res+=c[x]; x-=low(x); } return res; } vector<int>v[N]; int main(){ scanf("%d",&n); ll ans=0; for(int i=1;i<=n;i++){ int x; scanf("%d",&x); v[x].pb(i); add(i,1); } for(int i=1;i<N;i++){ if(v[i].empty())continue; for(int j=0;j<v[i].size();j++){ int x=v[i][j]; add(x,-1); n--; } for(int j=0;j<v[i].size();j++){ int x=v[i][j]; ll res=min(cal(x-1),n-cal(x)); //bug(i,res); ans+=res; } } printf("%lld\n",ans); } /* 7 3 1 4 1 5 9 2 */
Asia Yokohama Regional Contest 2018 G题 What Goes Up Must Come Down(树状数组求逆序对)
标签:pac nbsp test 题解 long 数组 define amp ++
原文地址:https://www.cnblogs.com/ccsu-kid/p/10599346.html