[编程题] 最高分是多少

老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩. 

输入描述:
输入包括多组测试数据。
每组输入第一行是两个正整数N和M(0 < N <= 30000,0 < M < 5000),分别代表学生的数目和操作的数目。
学生ID编号从1编到N。
第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩
接下来又M行,每一行有一个字符C(只取‘Q’或‘U’),和两个正整数A,B,当C为'Q'的时候, 表示这是一条询问操作,他询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少
当C为‘U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。



输出描述:
对于每一次询问操作,在一行里面输出最高成绩.
输入例子:
5 7
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 4 5
U 2 9
Q 1 5
输出例子:
5
6
5
9
 1 // ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "stdafx.h"
 6 
 7 #include <iostream>
 8 #include <vector>
 9 
10 using namespace std;
11 
12 int getmax(int *p, int A, int B)
13 {
14     if (A > B)
15     {
16         A = A^B;
17         B = A^B;
18         A = A^B;
19     }
20     int max=p[A];
21     for (int i = A; i <= B; ++i)
22     {
23         if (p[i] > max)
24         {
25             max = p[i];
26         }
27     }
28     return max;
29 }
30 int _tmain(int argc, _TCHAR* argv[])
31 {
32     int N, M;
33     char ch;
34     int A, B;
35     vector<int> max;
36     while (cin >> N >> M)
37     {
38         int *p = new int[N];
39 
40         for (int i = 0; i < N; i++)
41         {
42             cin >> p[i];
43         }
44         for (int i = 0; i < M; i++)
45         {
46             cin >> ch >> A >> B;
47             if (ch == 'Q')
48             {
49                 max.push_back(getmax(p, A - 1, B - 1));
50             }
51             else if (ch == 'U')
52             {
53                 p[A - 1] = B;
54             }
55         }
56         for (vector<int>::iterator it = max.begin(); it < max.end(); it++)
57         {
58             cout << *it << endl;
59         }
60         max.clear();
61         delete [] p;
62     }
63 
64     return 0;
65 }
原文地址:https://www.cnblogs.com/hhboboy/p/5641799.html