Dylans loves sequence(hdu5273)

Dylans loves sequence

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


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]2311
 
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
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 int ans[1010][1010],s[1010],cnt[1010];
 4 int main()
 5 {
 6     int n,m,i,j,sum,l,r;
 7     scanf("%d %d",&n,&m);
 8     for(i=1;i<=n;i++)
 9     {
10         scanf("%d",&s[i]);
11     }
12     for(i=1;i<=n;i++)
13     {
14         sum=0;
15         for(j=1;j<=i;j++)
16         {
17             if(s[i]<s[j])
18             {
19                 sum++;
20             }
21         }
22         ans[1][i]=ans[1][i-1]+sum;
23     }
24     for(i=2;i<=n;i++)
25     {
26         memset(cnt,0,sizeof(cnt));
27         for(j=i;j<=n;j++)
28         {
29             if(s[i-1]>s[j])
30             {
31                 cnt[j]=cnt[j-1]-1;
32             }
33             else
34             {
35                 cnt[j]=cnt[j-1];
36             }
37             ans[i][j]=ans[i-1][j]+cnt[j];
38         }
39     }
40     while(m--)
41     {
42         scanf("%d %d",&l,&r);
43         printf("%d
",ans[l][r]);
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/qioalu/p/4592883.html