码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5273 Dylans loves sequence

时间:2015-06-21 09:26:10      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:hdu 5273 dylans love

Dylans loves sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 117    Accepted Submission(s): 61


Problem Description
Dylans is given N numbers a[1]....a[N]

And there are Q questions.

Each question is like this (L,R)

his goal is to find the “inversions” from number L to number R.

more formally,his needs to find the numbers of pair(x,y),
that Lx,yR and x<y and a[x]>a[y]
 

Input
In the first line there is two numbers N and Q.

Then in the second line there are N numbers:a[1]..a[N]

In the next Q lines,there are two numbers L,R in each line.

N1000,Q100000,LR,1a[i]231?1
 

Output
For each query,print the numbers of "inversions”
 

Sample Input
3 2 3 2 1 1 2 1 3
 

Sample Output
1 3
Hint
You shouldn‘t print any space in each end of the line in the hack data.
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5275 5274 5271 5270 5269 
 

题意

Dylans得到了N个数a[1]...a[N]。
有Q个问题,每个问题形如(L,R)
他需要求出L?R这些数中的逆序对个数。
更加正式地,他需要求出二元组(x,y)的个数,使得Lx,yRx<ya[x]>a[y]
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<cmath>
#define N 1010

using namespace std;

int n,q;
int a[N],sum[N][N],num[N][N];

int main() {
    //freopen("test.in","r",stdin);
    while(~scanf("%d%d",&n,&q)) {
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        memset(num,0,sizeof num);
        //枚举开头  num[i][j]:以i开头,以j结尾的区间逆序对个数
        for(int i=1; i<=n; i++) {
            for(int j=i+1; j<=n; j++) {
                if(a[j]<a[i]) {
                    num[i][j]=num[i][j-1]+1;
                } else {
                    num[i][j]=num[i][j-1];
                }
            }
        }
        memset(sum,0,sizeof sum);
        ///预处理:sum[j][i]:以i结尾,以j开头的区间内所有逆序对数
        for(int i=1; i<=n; i++) {
            for(int j=i-1; j>=1; j--) {
                sum[j][i]=sum[j+1][i]+num[j][i];
            }
        }
        int l,r;
        while(q--) {
            scanf("%d%d",&l,&r);
            if(l==r) {
                printf("0\n");
                continue;
            }
            printf("%d\n",sum[l][r]);
        }
    }
    return 0;
}




hdu 5273 Dylans loves sequence

标签:hdu 5273 dylans love

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/46575787

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!