Who Is In Front of Me
TimeLimit: 1 Second MemoryLimit: 32 Megabyte
Totalsubmit: 693 Accepted: 192
Description
There are N(1<=N<=50000)students stand in a queue.We assume that every can only see the students that are in front of him and taller than him
.For example, there are 6 students standing in a queue with height of
4 3 1 2 5 2,begining from the front student. In this example, student 3's height is 1, and he can see 2 persons. Student 6, although many students in front are taller than him, but he can only see the student with height of 5.
Now, given the number of students in a queue, and the height of each, can you find the student that can see most persons.
Input
Input contains multiple test cases. The first line is a integer T, the number of cases.The first line of each case is a integer N, the number of students.The second line of each case contains the heights of students from front to back.
Output
For each test case, print a line with a integer M, representing the maximum number of students can someone can see.
Sample Input
2
6
4 3 1 2 5 2
3
2 2 2
Sample Output
20
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 50010; int m[maxn]; //member int num[maxn]; //record how many member in front of me; //1216 Accepted C++ 68MS 1128K 1095B int work(int n) { int index = 0; int max_num = 0; num[0] = 0; for(int i = 1; i < n; i++) { if(m[i] < m[i-1]) { num[i] = num[i-1] + 1; } else if(m[i] < m[index]) { for(int j = i-1; j >= index; j--) { if(m[j]>m[i]) { num[i] = num[j] + 1; break; } } } else{ ///the max member num[i] = 0; index = i; } max_num = max(num[i], max_num); } return max_num; } int main() { int T, n; scanf("%d", &T); while(T--) { scanf("%d", &n); memset(m, 0, sizeof(m)); for(int i = 0; i < n; i++) { scanf("%d", &m[i]); } int res = work(n); cout << res << endl; } return 0; }