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] 表示第 ...
随机推荐
- NetFPGA-SUME下reference_nic测试
Reference_nic Reference_nic是NetFPGA-SUME中提供的一个参考Demo,本文主要介绍如何构建并在SUME上运行reference_nic. GIT源 git clon ...
- The North American Invitational Programming Contest 2017 题目
NAIPC 2017 Yin and Yang Stones 75.39% 1000ms 262144K A mysterious circular arrangement of black st ...
- 树莓派与Arduino Leonardo使用NRF24L01无线模块通信之基于RF24库 (五) 树莓派单子节点发送数据
本项目中各个节点和树莓派的通信不区分信道,因此如果由树莓派发送给特定节点的数据会被所有节点接收到,因此子节点可以判别该数据是否发给自己的,需要在数据的第二个字节中加入目标节点的编号(第一个字节为源节点 ...
- Sprint2-2.0
1.开始一个新的冲刺: 起止:2016.6.1~2016.6.14 按照以下过程进行 ProductBacklog:继续向下细化 Sprint 计划会议:确定此次冲刺要完成的目标 Sprint Bac ...
- D3.js 入门学习(二) V4的改动
//d3.scan /* 新的d3.scan方法对数组进行线性扫描,并根据指定的比较函数返回至少一个元素的索引. 这个方法有点类似于d3.min和d3.max. 而d3.scan可以得到极值的索引而不 ...
- [转帖]go 的goroutine 以及 channel 的简介.
进程,线程的概念在操作系统的书上已经有详细的介绍.进程是内存资源管理和cpu调度的执行单元.为了有效利用多核处理器的优势,将进程进一步细分,允许一个进程里存在多个线程,这多个线程还是共享同一片内存空间 ...
- Android中线程间通信原理分析:Looper,MessageQueue,Handler
自问自答的两个问题 在我们去讨论Handler,Looper,MessageQueue的关系之前,我们需要先问两个问题: 1.这一套东西搞出来是为了解决什么问题呢? 2.如果让我们来解决这个问题该怎么 ...
- Opera官网打不开 下载Opera最新版本的实际地址
目前Opera官网可以打开,但是点下载时就会出错,国内无法访问Opera的下载地址,无法通过官网直接下载Opera浏览器.下面提供下载的方式. 一.通过官方的ftp站点下载 FTP地址为 http:/ ...
- Redis 基础:Redis 数据类型
Redis 数据类型 Redis支持五种数据类型:string(字符串).hash(哈希).list(列表).set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...