CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
2017-12-20 19:50
507 查看
http://codeforces.com/contest/351/problem/E
题意:
给出一些数,可以改变任意数的正负,使序列的逆序对数量最少
因为可以任意加负号,所以可以先把所有数看作正数
序列中的数无论怎么改,都不会改变与绝对值最大的数的逆序对的数量
所以从绝对值最大的数开始,决定它的正负
若这个序列中没有重复的最大值
若i是正数,与右边比他小的形成逆序对
若i是负数,与左边比他小的形成逆序对
所以两边取较小的
若序列中出现重复的最大值
那么最优解的最大值一定是 先是若干个负数,然后若干个正数
仍然符合上面的算法
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
output
input
output
题意:
给出一些数,可以改变任意数的正负,使序列的逆序对数量最少
因为可以任意加负号,所以可以先把所有数看作正数
序列中的数无论怎么改,都不会改变与绝对值最大的数的逆序对的数量
所以从绝对值最大的数开始,决定它的正负
若这个序列中没有重复的最大值
若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
相关文章推荐
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And his Cake
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
- CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)
- Codeforces Round #204 (Div. 1) E. Jeff and Permutation
- cf 204 div2 D. Jeff and Furik 逆序对
- CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle
- CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly
- CF&&CC百套计划2 CodeChef December Challenge 2017 Penalty Shoot-out
- CF&&CC百套计划2 CodeChef December Challenge 2017 Total Diamonds
- Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 找规律
- 【CF】207 Div.1 B.Xenia and Hamming
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组)
- B. Jeff and Periods(cf)
- CF 150&&152 div 2