LeetCode上也有这道题,直接扫一遍就行了,连数组都不用开,感觉像是蕴含了某种动归的思想在里面,要不怎么是个动归题呢
代码:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
int curr_score, prev_score, curr_cnt;
int dir, len;
int res = ; prev_score = -;
curr_cnt = ;
dir = ;
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &curr_score);
if (curr_score > prev_score) {
if (dir < ) {
res += ( - curr_cnt) * (len + (int) (curr_cnt <= ));
curr_cnt = ;
}
curr_cnt += ;
res += curr_cnt;
dir = ;
} else if (curr_score < prev_score) {
if (dir >= )
len = ;
curr_cnt -= ;
res += curr_cnt;
dir = -;
len += ;
} else {
if (dir < )
res += ( - curr_cnt) * (len + (int) (curr_cnt <= ));
curr_cnt = ;
res += curr_cnt;
dir = ;
}
prev_score = curr_score;
}
if (dir < )
res += ( - curr_cnt) * (len + (int) (curr_cnt <= )); cout << res << endl; return ;
}