zoukankan      html  css  js  c++  java
  • NYOJ 119 士兵杀敌(三)

    http://blog.csdn.net/niushuai666/article/details/7400587

    看过神牛的RMQ算法之后自己算是默写了遍.....

    交上去的时候,华丽丽的超时了。因为我用的是 cin cout

    才想到当输入和输出的次数多时,那么这两个是很耗时间的。

    View Code
     1  
     2 #include <iostream>
     3 #include <stdio.h>
     4 #include <cmath>
     5 using namespace std;
     6 const int maxn= 100010;
     7 int maxsum[maxn][20], minsum[maxn][20];
     8 int max(int a, int b)
     9 {
    10     return a > b ? a : b;
    11 }
    12 int min(int a, int b)
    13 {
    14     return a > b ? b : a;
    15 }
    16 void RMQ(int num)
    17 {
    18     int i, j;
    19     for(j = 1; j < 20; ++j)
    20       for(i = 1; i <= num; ++i)
    21       if(i + (1 << j) - 1 <= num)
    22       {
    23          maxsum[i][j] = max(maxsum[i][j - 1], maxsum[i + (1 << (j - 1))][j - 1]);  
    24          minsum[i][j] = min(minsum[i][j - 1], minsum[i + (1 << (j - 1))][j - 1]);
    25       }  
    26 }
    27 
    28 int main()
    29 {
    30     int n, q, i, j, a, b, ma, mi;
    31     scanf("%d%d",&n,&q);
    32     for(i = 1; i <= n; i++)
    33     {
    34         scanf("%d",&maxsum[i][0]);
    35         minsum[i][0] = maxsum[i][0];
    36     }
    37     RMQ(n);
    38     while(q--)
    39     {
    40         scanf("%d%d",&a,&b);
    41         int k = ( int )(log(b - a + 1)/log(2.0));
    42         ma = max(maxsum[a][k], maxsum[b - (1 << k) + 1][k]);
    43         mi = min(minsum[a][k], minsum[b - (1 << k) + 1][k]);
    44         printf("%d\n",ma-mi);
    45     }
    46     return 0;
    47 }
    48         
  • 相关阅读:
    centos7 mysql 数据库备份与还原
    Centos 7 搭建wordpress
    Centos 7 安装 mysql5.7
    linux 搭建php网站许愿墙
    GNOME桌面的安装
    cetnos 7 ntp服务的安装与配置
    Tornado初探
    Mysql报错 Cannot load from mysql.proc
    zabbix install
    PYPY_GC
  • 原文地址:https://www.cnblogs.com/yoru/p/2669087.html
Copyright © 2011-2022 走看看