【小米oj】 最少交换次数

求逆序对,可以树状数组,但是这题n^2也能过。。。

 1 #define mm(a) memset(a,0,sizeof(a));
 2 #define max(x,y) (x)>(y)?(x):(y)
 3 #define min(x,y) (x)<(y)?(x):(y)
 4 #define Fopen freopen("1.in","r",stdin); freopen("m.out","w",stdout);
 5 #define rep(i,a,b) for(int i=(a);i<=(b);i++)
 6 #define per(i,b,a) for(int i=(b);i>=(a);i--)
 7 #include<bits/stdc++.h>
 8 typedef long long ll;
 9 #define PII pair<ll,ll>
10 using namespace std;
11 const int INF=0x3f3f3f3f;
12 const int MAXN=(int)2e5 + 5;
13 const ll mod=1e9+7;
14 
15 
16 string input,temp;
17 vector<int>v;
18 int n;
19 int main() {
20     while (cin >> input) {
21         istringstream iss(input);
22         v.push_back(0);
23         while (getline(iss, temp, ',')) {
24             v.push_back(atoi(temp.c_str()));
25         }
26         n=v.size()-1;
27         int cnt=0;
28         for(int i=1;i<=n;i++){
29             for(int j=i+1;j<=n;j++){
30                 if(v[i]>v[j])cnt++;
31             }
32         }
33         printf("%d
",cnt);
34     }
35     return 0;
36 }
原文地址:https://www.cnblogs.com/dogenya/p/10815597.html