F题(水题)

给出一个有N个数的序列,编号0 - N - 1。进行Q次查询,查询编号i至j的所有数中,最大的数是多少。

 
例如: 1 7 6 3 1。i = 1, j = 3,对应的数为7 6 3,最大的数为7。(该问题也被称为RMQ问题)

Input第1行:1个数N,表示序列的长度。(2 <= N <= 10000) 
第2 - N + 1行:每行1个数,对应序列中的元素。(0 <= Sii <= 10^9) 
第N + 2行:1个数Q,表示查询的数量。(2 <= Q <= 10000) 
第N + 3 - N + Q + 2行:每行2个数,对应查询的起始编号i和结束编号j。(0 <= i <= j <= N - 1)
Output共Q行,对应每一个查询区间的最大值。Sample Input

5
1
7
6
3
1
3
0 1
1 3
3 4

Sample Output

7
7
3

直接按照题意求解就好了

#include<stdio.h>
#include<string.h>
#include<algorithm>

using namespace std;

int main()
{
    int n, a[10005];
    int i, beginn, endd;

    scanf("%d", &n);

    memset(a, 0, sizeof(a));

    for( i = 0; i < n; i++ )
    {
        scanf("%d", &a[i]);
    }

    int t, maxx;

    scanf("%d", &t);

    while(t--)
    {
        maxx = 0;

        scanf("%d %d", &beginn, &endd);

        for( i = beginn; i <= endd; i++ )
        {
            maxx = max(maxx, a[i]);
        }

        printf("%d
", maxx);

    }

    return 0;
}
View Code
永远渴望,大智若愚(stay hungry, stay foolish)
原文地址:https://www.cnblogs.com/h-hkai/p/8177307.html