HDU-5273

Dylans loves sequence

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

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 L≤x,y≤R 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.

N≤1000,Q≤100000,L≤R,1≤a[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

BestCoder Round #45

/**
          题意:给出一个数列,求某个区间的逆序数对有多少个
          做法:因为N 最大1000 所以 枚举,还以为是笼统的归并排序
**/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#define maxn 1010
using namespace std;
int dp[maxn][maxn];
int mmap[maxn];
int main()
{
//#ifndef ONLINE_JUDGE
//          freopen("in.txt","r",stdin);
//#endif // ONLINE_JUDGE
          int n,m;
          while(~scanf("%d %d",&n,&m))
          {
                    for(int i=1;i<=n;i++)
                    {
                              scanf("%d",&mmap[i]);
                    }
                    memset(dp,0,sizeof(dp));
                    for(int i=1;i<=n;i++)
                    {
                              for(int j=i+1;j<=n;j++)
                              {
                                        if(mmap[i] > mmap[j])
                                        dp[i][j] ++;
                              }
                              for(int j=1;j<=n;j++)
                              {
                                        dp[i][j] += dp[i][j-1];
                              }
                    }
                    for(int i=n-1;i>0;i--)        ///枚举i~j中任意一个区间的逆序数对
                    {
                              for(int j=i+1;j<=n;j++)
                              {
                                        dp[i][j] += dp[i+1][j];
                              }
                    }
                    while(m--)
                    {
                              int u,v;
                              scanf("%d %d",&u,&v);
                              printf("%d\n",dp[u][v]);
                    }
          }
          return 0;
}
时间: 2024-08-10 17:07:49

HDU-5273的相关文章

hdu 5273 Dylans loves sequence 逆序数简单递推

Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Description Dylans得到了N个数a[1]...a[N].有Q个问题,每个问题形如(L,R)他需要求出L−R这些数中的逆序对个数.更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y] Input 第一行有两个数N和Q

hdu 5273 Dylans loves sequence

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5273 bestcoder round#45 1002 题目大意: 给出一个有n个数的任意序列,问[r,l]区间内一共有多少对倒置数? 解题思路: 由于1<=n<=1000,所以想怎么做怎么做,当时不知道怎么了,思路不错,但是就是代码wa. 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h&

HDU 5273 Dylans loves numbers(水题)

题意:给出一个0≤N≤1018,求其二进制中有几处是具有1的,假设相连的1只算1处,比如1101011就是3处. 思路:一个个数,当遇到第一个1时就将flag置为1:当遇到0就将flag置为0.当遇到1时,flag=0就统计,当flag=1时就不统计. 1 #include <bits/stdc++.h> 2 #define LL long long 3 using namespace std; 4 5 int main() 6 { 7 int t; 8 LL n; 9 cin>>

HDU 5273 Dylans loves sequence (逆序对,暴力)

题意:给定一个序列,对于q个询问:(L,R)之间有几个逆序对?序列元素个数上限1000,q上限10万.仅1测试例子. 思路: 先分析: [L,R]的逆序对数量可以这么算,假设L<=K<R,将区间拆成两部分,那么[L,k]中的逆序对要算上, (k,R]中的逆序对也要算上,还有一些逆序对假设为(l,r),l在左部分,r在右部分.则应该是3部分来构成,设3部分为A,B,C,那么ans=A+B+C . 而如果将k移到最右边,比如k=R-1,那么区间拆成[L,k]和(K,R],而(K,R]其实就只有R一

hdu 5273 Dylans loves sequence(区间逆序对数-莫队算法)

n<=1000,q<=100000,求区间内逆序对数,从[l,r]显然可以log(n)的时间内移动到[l-1,r],[l+1,r],[l,r-1],[l,r+1],那么就可以用莫队进行离线 复杂度大概是O(n*sqrt(n)*log2(n)),不过可以暴力枚举起点,然后向后统计,然后O(1)回答,不过n再大就无法解决了,这个即使是n<=1e5也可以很快得到答案,开-o优化,1e6也可以很快得到答案 #include<bits/stdc++.h> using namespace

HDU 5273(递推)

Dylans loves sequence Accepts: 250 Submissions: 806 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 Dylans得到了N个数a[1]...a[N]. 有Q个问题,每个问题形如(L,R) 他需要求出L?R这些数中的逆序对个数. 更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y]

HDU 5273 Dylans loves sequence(区间DP)

Dylans loves sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 114    Accepted Submission(s): 59 Problem Description Dylans is given N numbers a[1]....a[N] And there are Q questions. E

HDU 5273(暴力前缀和)

Dylans loves sequence Accepts: 250 Submissions: 806 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 Dylans得到了N个数a[1]...a[N]. 有Q个问题,每个问题形如(L,R) 他需要求出L?R这些数中的逆序对个数. 更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y]

HDU 5273 Dylans loves sequence【 树状数组 】

题意:给出n个数,再给出q个询问,求L到R的逆序对的个数 先自己写的时候,是每次询问都重新插入来求sum(r)-sum(l) 果断T 后来还是看了别人的代码---- 预处理一下,把所有可能的区间的询问都求出来(1000*1000), 然后询问就是O(1)了 然后想自己这样写超时,是因为询问太多了---- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath>

HDU 5273 区间DP

输入一组数,m次询问 问每一个询问区间的逆序数有多少 区间DP简单题 #include "stdio.h" #include "string.h" int dp[1010][1010],a[1010]; int main() { int n,m,i,j,k; while (scanf("%d%d",&n,&m)!=EOF) { for (i=1;i<=n;i++) scanf("%d",&a[i]