(2018年全国多校算法寒假训练营练习比赛(第五场))

A 逆序数

链接:https://www.nowcoder.com/acm/contest/77/A
来源:牛客网
 

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。比如一个序列为4 5 1 3 2, 那么这个序列的逆序数为7,逆序对分别为(4, 1), (4, 3), (4, 2), (5, 1), (5, 3), (5, 2),(3, 2)。

输入描述:

第一行有一个整数n(1 <= n <= 100000),  然后第二行跟着n个整数,对于第i个数a[i],(0 <= a[i] <= 100000)。

输出描述:

输出这个序列中的逆序数

示例1

输入

复制

5
4 5 1 3 2

输出

复制

7

PS:这里有两个方法就逆序数,一个是暴力,第二个是树状数组,首先是树状数组,后面再给出暴力的代码。如果用两个循环,肯定会超时的。所以可以声明一个数组,记录比当前位置大的数,因为逆序数,正好是前面出现的比后面大的数,所以只要在前面输入一个数,就把比他小的数的数组加一,最后直接加数组里的数,就是前面比当前输入数大的数的个数。

树状数组:

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
#include<vector>
const int maxn=5e5+5;
const int mod=1e9+7;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int bit[maxn],n;
struct node
{
    int x,i;
    bool friend operator<(node a,node b)
    {
        if(a.x==b.x)
            return a.i<b.i;
        return a.x<b.x;
    }
}a[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void updata(int x)
{
    while(x<=n)
    {
        bit[x]+=1;
        x+=lowbit(x);
    }
}
int ss(int x)
{
    int s=0;
    while(x)
    {
        s+=bit[x];
        x-=lowbit(x);
    }
    return s;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        me(bit,0);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].x);
            a[i].i=i;
        }
        sort(a+1,a+1+n);
        ll sum=0;
        for(int i=1;i<=n;i++)
        {
            updata(a[i].i);
            sum+=i-ss(a[i].i);
        }
        cout<<sum<<endl;
    }
    return 0;
}

暴力: 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define maxn 100000+100
#define ll long long
using namespace std;
int main()
{
    int n,k,a[maxn];
    cin >> n;
    ll sum = 0;
    memset(a,0,sizeof(a));
    while (n--)
    {
        cin >> k;
        sum += a[k];
        for (int i = 0; i < k; i++)
            a[i]++;
    }
    cout << sum << endl;
    return 0;
}

H Tree Recovery(线段树)

链接:https://www.nowcoder.com/acm/contest/77/H
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

输入描述:

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

输出描述:

You need to answer all Q commands in order. One answer in a line.

示例1

输入

复制

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

输出

复制

4
55
9
15

PS:看题就非常清楚这是一个裸的线段树区间更新和区间求和问题。这里就不细说线段树,这里有篇介绍线段树非常好的博客,介绍给大家,线段树详解

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=1e5+5;
const int mod=1e9+7;
const int inf=1e9;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
int num[maxn<<2],add[maxn<<2];
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&num[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    num[rt]=num[rt<<1]+num[rt<<1|1];
}
void pushdown(int ln,int rn,int rt)
{
    if(add[rt])
    {
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        num[rt<<1]+=add[rt]*ln;
        num[rt<<1|1]+=add[rt]*rn;
        add[rt]=0;
    }
}
void updata(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        num[rt]+=(r-l+1)*c;
        add[rt]+=c;
        return ;
    }
    int m=(l+r)>>1;
    pushdown(m-l+1,r-m,rt);
    if(L<=m)
        updata(L,R,c,l,m,rt<<1);
    if(R>m)
        updata(L,R,c,m+1,r,rt<<1|1);
    num[rt]=num[rt<<1]+num[rt<<1|1];
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
        return num[rt];
    int s=0;
    int m=(l+r)>>1;
    pushdown(m-l+1,r-m,rt);
    if(L<=m)
        s+=query(L,R,l,m,rt<<1);
    if(R>m)
        s+=query(L,R,m+1,r,rt<<1|1);
    return s;
}
int main()
{
    int n,m;
    cin>>n>>m;
    build(1,n,1);
    while(m--)
    {
        string s;
        int a,b,c;
        cin>>s;
        if(s=="Q")
        {
            scanf("%d%d",&a,&b);
            cout<<query(a,b,1,n,1)<<endl;
        }
        else
        {
            scanf("%d%d%d",&a,&b,&c);
            updata(a,b,c,1,n,1);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41292370/article/details/81264559
今日推荐