其他分享
首页 > 其他分享> > CF351E Jeff and Permutation

CF351E Jeff and Permutation

作者:互联网

感觉还是记点东西吧。还是总得留下点东西的。

CF351E Jeff and Permutation

题目描述:

给出数组a ,你可以改变每个数的正负,求逆序对数最少是多少

题目分析:

感觉自己好蠢。。猜结论猜错了就离谱。。

首先对于 \(a\) 数组全都取绝对值,毕竟之后还能再变回来。

那么我们考虑会出现逆序对所产生的情况

那么从上面两种情况来看的话,如果说 \(a_i\) 不选择取反的话,那么就是后面比他小的数的个数,否则的话,就是前面比他小的数都会产生贡献,因此统计两边的比 \(a_i\) 小的个数然后取最小值即可。

Code:

//editor : DRYAYST
//Wo shi ge da SHA BI
#include<bits/stdc++.h>
#define g() getchar()
#define il inline
#define ull unsigned long long
#define eps 1e-10
#define ll long long
#define pa pair<int, int>
#define for_1(i, n) for(int i = 1; i <= (n); ++i)
#define for_0(i, n) for(int i = 0; i < (n); ++i)
#define for_xy(i, x, y) for(int i = (x); i <= (y); ++i)
#define for_yx(i, y, x) for(int i = (y); i >= (x); --i)
#define for_edge(i, x) for(int i = head[x]; i; i = nxt[i])
#define int long long
#define DB double
#define ls (p<<1)
#define rs (p<<1|1)
#define m_p make_pair
#define fi first
#define se second
using namespace std;
const int N = 1e6 + 10, INF = 1e18, mod = 1e9 + 7;
il int qpow(int x, int k) {int ans = 1; while(k) {if(k & 1) ans = ans * x % mod; x = x * x % mod; k >>= 1; } return ans; }
il int Add(int x, int y) {return (x += y) %= mod;}
il int Del(int x, int y) {return (x = x - y + mod) % mod;}
il int Mul(int x, int y) {return x * y % mod;}
il int inv(int x) {return qpow(x, mod - 2); }
inline int re() {
    int x = 0, p = 1;
    char ch = getchar();
    while(ch > '9' || ch < '0') {if(ch == '-') p = -1; ch = getchar();}
    while(ch <= '9' and ch >= '0') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
    return x * p;
}

int n, ans; 
int a[N]; 

signed main() {
    // freopen("listen.in","r",stdin);
    // freopen("listen.out","w",stdout); 
    n = re(); for_1(i, n) a[i] = re(), a[i] = abs(a[i]); 
    for_1(i, n) { 
        int L = 0, R = 0; 
        for_xy(j, 1, i-1) if(a[j] < a[i]) L++; 
        for_xy(j, i+1, n) if(a[j] < a[i]) R++; 
        ans += min(L, R); 
    }
    cout<<ans<<endl;
}

标签:ch,return,Jeff,int,CF351E,取反,long,Permutation,define
来源: https://www.cnblogs.com/dryayst/p/16101947.html