NOJ [1358] An Easy Problem

来源:互联网 发布:unity3d特效包下载 编辑:程序博客网 时间:2024/06/06 07:01
  • 问题描述
  • Here a simple problem.Give you n numbers.a1,a2...an.Then ask you m times.each time you should answer the the minimum number between ax to ay.
  • 输入
  • There are several test
    First line input two number n,m(1<=n<=100000,1<=m<=100000)
    then next line follow n integer.
    next m line with two numbers x,y;(1<=x<=y<=n)
  • 输出
  • for each x,y you should output the minimum number between ax to ay.

    rmq的裸题啊,不会rmq的话,我简单介绍下,我们用一个数组先把值存好,再开一个DP[i][j]来表示区间[i,i+2^j-1]的最值,
    把区间对分为2块,最值就是左右两块的最值的最值

    #include<stdio.h>#include<math.h>int min (int a,int b){  return a<b?a:b;}int num[100010];int dp[100010][18];int temp;int n;void st(){  int i,j;  int k= log((double)(n+1))/log(2.0);  for(i=1;i<=n;i++)    dp[i][0]=num[i];  for(j=1;j<=k;j++)      for(i=1;i+(1 << j)-1 <= n;i++)         dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);}int rmq(int a,int b){  int k = (int)(log((double)(b-a+1))/log(2.0));  return min(dp[a][k],dp[b-(1<<k)+1][k]);}int main(){  int m;  while(~scanf("%d%d",&n,&m))  {    int i;    for(i=1;i<=n;i++)      scanf("%d",&num[i]);    st();    int start,end;    for(i=1;i<=m;i++)    {      scanf("%d%d",&start,&end);      printf("%d\n", rmq(start,end));    }  }  return 0;}


0 0