2017武汉大学校赛网络预选赛g题
来源:互联网 发布:澳门网络博客游戏 编辑:程序博客网 时间:2024/04/28 15:52
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 mebibytes
Everyone hates the verdict “Time Limit Exceeded” or “TLE”. However, sometimes, we can’t come up with a “elegant” solution which is clear and fast.
Our problem setter also hates “TLE”, but he is dumb and can only work out what a problem is. He can’t really solve it. We called him “Zui Qiang Xuan Shou”
One day, he got a problem, again, he said “I got it! It is **”, he spoke so fast that no one knew what he said. Luckily, he typed his solution—
He said that it should get AC small cases, but he might receive “Time Limit Exceeded”.
As he clicked the “submit” button, the network is down. So he went to the dinning hall and ask you to correct his solution.
Your program should output exact same things as the program listed above.
Input
The format of input should make the program work if we don’t care about the time.
In the input file, all numbers are positive integers and no more than 10^610
6
.
Output
You should output what the program outputs if the time is unlimited.
Examples
Input 1
5
1 2 3 4 5
Output 1
0
Input 2
6
1 2 3 4 5 6
Output 2
0
题意:让你写一个程序,输入和输出跟上面一样。
解题思路:很明显上面就是一个冒泡排序的代码,答案就是这个数组的逆序对数目.那么求逆序对还不容易,树状数组就行,而且这题不需要离散化,很简单,上次听队友说求逆序数还可以用归并排序,方法很多,这里用树状数组的复杂度是n*logn,所有跑起来很快。
#include<stdio.h>#include<iostream>#include<cstring>using namespace std;const int maxn = 1e6 + 10;typedef long long ll;int n;ll a[maxn];ll Tree[2*maxn];ll lowbit(ll x){ return x&(-x);}ll get(ll x){ ll sum = 0; for(ll i = x; i > 0; i -= lowbit(i)) { sum += Tree[i]; } return sum;}void update(ll x,ll value){ for(ll i = x; i <= maxn; i += lowbit(i)) { Tree[i] += value; }}int main(){ scanf("%d",&n); memset(Tree,0,sizeof(Tree)); for(ll i = 1; i <= n; i++) { scanf("%lld",&a[i]); } ll ans = 0; for(int i = 1; i <= n; i++) { ll res = get(a[i]); ans += i - 1 - res; update(a[i],1); } printf("%lld\n",ans); return 0;}
- 2017武汉大学校赛网络预选赛g题
- 2017武汉大学校赛网络预选赛b题
- 2017武汉大学校赛网络预选赛e题
- 2017武汉大学校赛网络预选赛d题
- 2017武汉大学校赛 网络预选赛 简单题题解
- 2017武汉大学校赛网络预选赛c题
- 武汉大学校赛总结 + E题+F题
- 武汉大学校赛(个人总结+补题)
- 2017华中区邀请赛暨武汉大学校赛网络赛小结 + WOJ 642 Lost in WHU
- 2016年武汉科技大学邀请赛网络赛 G题
- 2017华中地区邀请赛暨武汉大学校赛现场赛小结
- 校赛网络预选赛小结
- 2017ccpc网络预选赛1005
- 4月16日东北大学校赛重现赛G题(经典问题)
- 长安大学校赛-2017
- 2017武大校赛网络预选赛C题 Divide by Six
- 安徽大学第九届大学生程序设计竞赛 网络预选赛 G
- 2017浙江理工大学校赛D题
- POJ1061青蛙的约会
- 使用cvs从SourceForge.net获得源码
- 《一天学懂深度学习》PPT翻译一
- 【设计模式】状态模式
- 【HTML】网页错误码详细报错
- 2017武汉大学校赛网络预选赛g题
- cogs 14. [网络流24题] 搭配飞行员
- 组合模式
- 数据结构-队列:循环队列与链队列的C++模板类实现
- java工程师成神之路
- 我讨厌志向远大的人
- HDU5534 Partial Tree 【DP】
- 如何设置网页地址栏前面的标志favicon图标?
- innodb中幻读与mvcc和间隙锁分析