2017武汉大学校赛网络预选赛g题

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值