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;}
0 0
原创粉丝点击