输入一组数,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]); memset(dp,0,sizeof(dp)); for (i=1;i<=n;i++) for (j=1;j+i-1<=n;j++) { k=i+j-1; dp[j][k]=dp[j+1][k]+dp[j][k-1]-dp[j+1][k-1]; if (a[j]>a[k]) dp[j][k]++; } while (m--) { scanf("%d%d",&i,&j); printf("%d ",dp[i][j]); } } return 0; }