E. Jeff and Permutation----思维题
来源:互联网 发布:mac修容 编辑:程序博客网 时间:2024/05/18 13:25
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 inequality ai > 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.
22 1
0
9-2 0 -1 0 -1 2 1 0 -1
6
题目链接:http://codeforces.com/contest/351/problem/E
题目的意思是说给你一串序列,里面的每一个数都可以任意变换正负号,求最小逆序对的个数。
思路挺简单,为什么我没有想到。。
既然正负号可以任意变换,我们在存的时候可以存绝对值,对于每一个数来说,小于它的数,不管在左边还是右边,符号变换对它没有影响,所以我们直接加上数目较少的一边就可以。
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int a[20000];int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&a[i]); a[i]=abs(a[i]); } int ans=0; for(int i=0;i<n;i++){ int l=0,r=0; for(int j=0;j<i;j++){ if(a[j]<a[i]) l++; } for(int j=i+1;j<n;j++){ if(a[i]>a[j]){ r++; } } ans+=min(l,r); } printf("%d\n",ans); return 0;}
大牛链接:http://www.lai18.com/content/913044.html
- E. Jeff and Permutation----思维题
- Codeforces Round #204 (Div. 1) E. Jeff and Permutation
- A. Jeff and Rounding----思维题
- B. Permutation----思维题
- 【CodeForces 351A】Jeff and Rounding(思维)
- codeforces 785E. Anton and Permutation
- codeforces 785 E. Anton and Permutation
- Codefores 785E Anton and Permutation(分块)
- CodeForces 785E Anton and Permutation 分块
- C. Lucky Permutation----思维题
- CodeForces-492E Vanya and Field(思维题/最小公倍数)
- Codeforces 584E Anton and Ira【思维+贪心】好题~
- 761E Dasha and Puzzle[dfs][思维]
- A. Jeff and Digits
- B. Jeff and Periods
- Jeff and Digits
- Jeff and Periods
- Jeff and Rounding
- Sublime3上搭载python3.5,以及为Python pip创建镜像,实测可行!
- mysql出现:There is no 'root '@ '% ' registered 解决方法
- 关于在Quartus II和ModelSim中进行FPGA仿真报错的问题
- Android -- Service基础简介
- python的一些细节(3)
- E. Jeff and Permutation----思维题
- 一步一步学Spring Boot(一)
- STM32单片机总结
- mutable的用法
- 模型评价(一) AUC大法
- 二叉树的高度/销毁一颗二叉树
- quartz定时任务调度器开发Java Product自动运行项目
- Mysql系列二 mysql语法总结
- session 共享问题