CF&&CC百套计划3 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[]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;++i)
{
scanf("%d",&a[i]);
if(a[i]<) a[i]=-a[i];
}
int ans=;
int l,r;
for(int i=;i<=n;++i)
{
l=r=;
for(int j=;j<i;++j)
if(a[i]>a[j]) l++;
for(int j=i+;j<=n;++j)
if(a[i]>a[j]) r++;
ans+=min(l,r);
}
printf("%d",ans);
}
2 seconds
256 megabytes
standard input
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.
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
CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation的更多相关文章
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik
http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- CF&&CC百套计划1 Codeforces Round #449 C. Willem, Chtholly and Seniorious (Old Driver Tree)
http://codeforces.com/problemset/problem/896/C 题意: 对于一个随机序列,执行以下操作: 区间赋值 区间加 区间求第k小 区间求k次幂的和 对于随机序列, ...
- CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly
http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...
- CF&&CC百套计划1 Codeforces Round #449 A. Nephren gives a riddle
http://codeforces.com/contest/896/problem/A 第i个字符串嵌套第i-1个字符串 求第n个字符串的第k个字母 dfs #include<map> # ...
- CF&&CC百套计划2 CodeChef December Challenge 2017 Chef And Easy Xor Queries
https://www.codechef.com/DEC17/problems/CHEFEXQ 题意: 位置i的数改为k 询问区间[1,i]内有多少个前缀的异或和为k 分块 sum[i][j] 表示第 ...
随机推荐
- C#窗体——四则运算
用户需求:程序能接收用户输入的整数答案,并判断对错程序结束时,统计出答对.答错的题目数量.补充说明:0——10的整数是随机生成的用户可以选择四则运算中的一种用户可以结束程序的运行,并显示统计结果.在此 ...
- HDU 3092 Least common multiple 01背包
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3092 Least common multiple Time Limit: 2000/1000 MS ...
- php 把数字转化为大写中文
PHP 数字转大写中文 PHP入门小菜鸟一枚.下午要求写一个把数字转成大写中文的脚本,百度了一波,几十个博客和网站都是用的那四个代码,第一个运行不了,第二个有问题,不合要求,第三个第四个太长,懒得看, ...
- 《简明Python教程》学习笔记
<简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Pa ...
- windows下python3.6安装pycryto or crypto or pycryptodome与使用
pycrypto,pycrytodome和crypto是一个东西,在很久以前,crypto在python上面的名字是pycrypto它是一个第三方库,但是已经停止更新三年了,所以不建议安装这个库: w ...
- 面试问题总结二(技术能力-PHP)----Ⅰ
1.你都做过什么项目? 答:第一份实习工作接触的项目是CRM 销售管理系统,一款用JSP语言开发的进销存管理系统.第一份正式工作是一款主打高质量图片社交社区网站项目,“美啦周末”(后改型为”聊会儿”) ...
- python 安装pymssql
error: command 'gcc' failed with exit status 1 ---------------------------------------- Command &quo ...
- Mysql 错误相关
有几天没启动本地的 MySQL 了,今天打开小海豚,直接给我弹出了连接失败,这是怎么回事,就直接进入命令行,发现也有问题,输入密码后一闪而过,也没有什么提示信息给我,百度了先,都说是修改 MySQL ...
- Java多线程之ThreadLocal总结
原贴地址:http://www.cnblogs.com/zhengbin/p/5674638.html 阅读目录 官方对ThreadLocal的描述: <Thinking in Java> ...
- matlab gradient 和 prctile
介绍两个matlab小函数: 1.gradient 借用别人的例子:例:>> x=[6,9,3,4,0;5,4,1,2,5;6,7,7,8,0;7,8,9,10,0]x = 6 ...