Cow Sorting(http://poj.org/problem?id=3270)
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7932 Accepted: 3119
Description

Farmer John’s N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a unique “grumpiness” level in the range 1…100,000. Since grumpy cows are more likely to damage FJ’s milking equipment, FJ would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (not necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes FJ a total of X+Y units of time to exchange two cows whose grumpiness levels are X and Y.

Please help FJ calculate the minimal time required to reorder the cows.

Input

Line 1: A single integer: N.
Lines 2..N+1: Each line contains a single integer: line i+1 describes the grumpiness of cow i.
Output

Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.
Sample Input

3
2
3
1
Sample Output

7
题目:有一串数字,要将它排列成升序,每次可以交换两个数,交换一次的代价为两数之和。要求代价最小。

从小到大排列花费的权值==逆序数权值和

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int N=101000;
int c[N],vis[N];
int lowbit(int x)
{return x&(-x);
}
void up(int x,int v)
{for(int i=x;i<=N-1;i+=lowbit(i)) c[i]+=v;
}
int sum(int x)
{int res=0;for(int i=x;i>0;i-=lowbit(i)) res+=c[i];return res;
}
int main()
{int n;long long ans=0;cin>>n;memset(c,0,sizeof(c));for(int i=1;i<=n;i++){cin>>vis[i];}for(int i=n;i>0;i--){ans+=(ll)sum(vis[i]-1)*vis[i];up(vis[i],1);}memset(c,0,sizeof(c));for(int i=1;i<=n;i++){ans+=(ll)(sum(N)-sum(vis[i]))*vis[i];up(vis[i],1);}cout<<ans<<endl;return 0;
}

树状数组之POJ3270Cow Sorting相关推荐

  1. LG5200 「USACO2019JAN」Sleepy Cow Sorting 树状数组

    \(\mathrm{Sleepy Cow Sorting}\) 问题描述 LG5200 题解 树状数组. 设\(c[i]\)代表\([1,i]\)中归位数. 显然最终的目的是将整个序列排序为一个上升序 ...

  2. HDU 2838 Cow Sorting(双树状数组+求逆序数)

    Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Ea ...

  3. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  4. 【OpenJ_Bailian - 2299 】Ultra-QuickSort (归并排序 或 离散化 + 树状数组)

    题干: In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a s ...

  5. 洛谷 P5057 [CQOI2006]简单题(树状数组)

    嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...

  6. Color the ball(HDU1556)树状数组

    每次对区间内气球进行一次染色,求n次操作后后所有气球染色次数. 树状数组,上下区间更新都可以,差别不大. 1.对于[x,y]区间,对第x-1位减1,第y位加1,之后向上统计 #include<b ...

  7. 【BZOJ2434】[NOI2011]阿狸的打字机 AC自动机+DFS序+树状数组

    [BZOJ2434][NOI2011]阿狸的打字机 Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P ...

  8. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  9. poj_3067 树状数组

    题目大意 左右两个竖排,左边竖排有N个点,从上到下依次标记为1,2,...N; 右边竖排有M个点,从上到下依次标记为1,2....M.现在从K条直线分别连接左边一个点和右边一个点,求这K条直线的交点个 ...

最新文章

  1. 事业单位计算机专业知识试卷,计算机专业知识试题(事业单位计算机专业知识考试试卷).doc...
  2. Activity销毁重建导致LiveData数据倒灌
  3. 45种Javascript技巧大全
  4. 认识一下Global.asa 文件
  5. PAT甲级1005 Spell It Right :[C++题解]字符串处理
  6. 《机器学习实战》笔记(04):基于概率论的分类方法 - 朴素贝叶斯分类
  7. (一)边缘AI:口罩检测
  8. 虚拟机安装centos7后无法联网
  9. java 表格添加监听_关于Oracle用java实时监听oracle对表的DML操作【技术贴】
  10. Spring quartz插件的使用
  11. 有什么简单办法从格式复杂的 Excel中提取数据
  12. 计算机u盘病毒清除方式,终极:如何消除计算机上U盘的“文件夹.EXE病毒”?
  13. Spring整合Mybatis遇到的错误一
  14. From Word Embeddings To Document Distances论文总结
  15. Laya 2.0 微信排行榜数据
  16. 遗传算法(GA)求解TSP问题
  17. 第七章 实验传统的兴起
  18. Excel表格垂直居中后还是不在单元格中间(探究原因贴)
  19. 【JavaSE】----- Java语言的介绍
  20. C语言: 定义一个函数int isprime(int n),用来判别一个正整数n是否为素数。在主函数中输入两个正整数m和n(m>=1,n>m),统计并输出m和n之间的素数的个数以及这些素数的和。

热门文章

  1. Graphviz在python的用法(一):Graphviz安装和命令
  2. 【单片机仿真项目】报警灯(proteus原理图+keil代码)
  3. 货币政策和财政政策对总需求的影响 - 异想天开
  4. Java递归算法计算5的阶乘
  5. c语言黑白棋ai游戏源码
  6. [C++]BMI指数
  7. 上海域格CAT1模块CLM920_KV3贴片SIM卡3卡切换
  8. 【Ctfer训练计划】——(八)
  9. int 包装类型 Integer
  10. 红米设备怎么样无需ROOT激活Xposed框架的教程