ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF351E Jeff and Permutation

2022-04-05 13:02:03  阅读:164  来源: 互联网

标签:ch return Jeff int CF351E 取反 long Permutation define


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

CF351E Jeff and Permutation

题目描述:

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

题目分析:

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

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

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

  • \(a_i < a_j\) 那么此时产生逆序对的话, (1) 让 \(a_j\) 取反,\(a_i\) 不变,(2) 全都取反。
  • \(a_i > a_j\) 那么(1) 两者都不取反的话必然是成立的.(2)\(a_i\) 不动, \(a_j\) 取反

那么从上面两种情况来看的话,如果说 \(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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有