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); }
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.
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.
In a single line print the answer to the problem — the minimum number of inversions Jeff can get.
2
2 1
0
9
-2 0 -1 0 -1 2 1 0 -1
6