• Codeforces Round #204 (Div. 1) E. Jeff and Permutation


    http://codeforces.com/contest/351/problem/E

    题意:

    给出一些数,可以改变任意数的正负,使序列的逆序对数量最少

    因为可以任意加负号,所以可以先把所有数看作正数

    序列中的数无论怎么改,都不会改变与绝对值最大的数的逆序对的数量

    所以从绝对值最大的数开始,决定它的正负

    若这个序列中没有重复的最大值

    若i是正数,与右边比他小的形成逆序对

    若i是负数,与左边比他小的形成逆序对

    所以两边取较小的

    若序列中出现重复的最大值

    那么最优解的最大值一定是 先是若干个负数,然后若干个正数

    仍然符合上面的算法

    #include<cstdio>
    #include<algorithm>
    
    using namespace std;
    
    int a[2001];
    
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;++i) 
        {
            scanf("%d",&a[i]);
            if(a[i]<0) a[i]=-a[i];
        }
        int ans=0;
        int l,r;
        for(int i=1;i<=n;++i)
        {
            l=r=0;
            for(int j=1;j<i;++j)
                if(a[i]>a[j]) l++;
            for(int j=i+1;j<=n;++j)    
                if(a[i]>a[j]) r++;
            ans+=min(l,r);
        }
        printf("%d",ans);
    }
    E. Jeff and Permutation
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Jeff's friends know full well that the boy likes to get sequences and arrays for his birthday. Thus, Jeff got sequence p1, p2, ..., pn for his birthday.

    Jeff hates inversions in sequences. An inversion in sequence a1, a2, ..., an is a pair of indexes i, j (1 ≤ i < j ≤ n), such that an inequalityai > aj holds.

    Jeff can multiply some numbers of the sequence p by -1. At that, he wants the number of inversions in the sequence to be minimum. Help Jeff and find the minimum number of inversions he manages to get.

    Input

    The first line contains integer n (1 ≤ n ≤ 2000). The next line contains n integers — sequence p1, p2, ..., pn (|pi| ≤ 105). The numbers are separated by spaces.

    Output

    In a single line print the answer to the problem — the minimum number of inversions Jeff can get.

    Examples
    input
    2
    2 1
    output
    0
    input
    9
    -2 0 -1 0 -1 2 1 0 -1
    output
    6
  • 相关阅读:
    P3193 [HNOI2008]GT考试(KMP+矩阵乘法加速dp)
    P2606 [ZJOI2010]排列计数
    洛谷P2657 [SCOI2009]windy数
    P2602 [ZJOI2010]数字计数(递推)
    洛谷 P1073 最优贸易
    [一本通学习笔记] 字典树与 0-1 Trie
    [一本通学习笔记] KMP算法
    [一本通学习笔记] 字符串哈希
    [一本通学习笔记] 广度优先搜索和优化
    [一本通学习笔记] 深度优先搜索与剪枝
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8075533.html
Copyright © 2020-2023  润新知