NC25045. [USACO 2007 Jan S]Balanced Lineup
描述
输入描述
Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
输出描述
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
示例1
输入:
6 3 1 7 3 4 2 5 1 5 4 6 2 2
输出:
6 3 0
C++ 解法, 执行用时: 312ms, 内存消耗: 908K, 提交时间: 2022-01-14 00:11:58
#include <bits/stdc++.h> using namespace std; int a[100005]; int main() { int n,q; cin>>n>>q; for(int i=1;i<=n;i++) { cin>>a[i]; } int x1,x2; while(q--) { cin>>x1>>x2; int max1=a[x1],min1=a[x1]; for(int i=x1;i<=x2;i++) { if(a[i]>max1) max1=a[i]; if(a[i]<min1) min1=a[i]; } cout<<max1-min1<<endl; } return 0; }