0分求助,帮忙改错/优化(WA + TLE)
查看原帖
0分求助,帮忙改错/优化(WA + TLE)
529247
Br00k5xx楼主2021/7/2 20:45
#include <iostream>
using namespace std;
bool flag[100005] = {0};
int main() {
    int a[100005], s = 0, ans2 = 1, ans1 = 0, pivot, now; 
    while (cin >> a[s]) {
        s++;
        if (s == 1) continue;
        if (a[s - 1] >= a[s - 2]) ans2 ++; /*每当有一个后面的元素
                                           大于等于前面的元素,
                                           就要增加一个导弹拦截系统*/
        else {
            flag[s - 1] = 1; /*纪录该元素是否处于高度下降趋势之内,
                             如果是,那么就不用枚举从该元素开始的
                             情况了*/
        }
    }
    for (int i = 0; i < s; i++) {
        pivot = i; //目前基准值(最低值)下标为i
        now = 1; //这种情况所能拦截导弹个数
        if (flag[i]) continue;
        for (int j = i + 1; j < s; j++) {
            if (a[j] < a[pivot]) pivot = j, now ++;
        }
        if (now > ans1) ans1 = now; //打擂台,选出最大值
    }
    cout << ans1 << endl << ans2; //输出结果
    return 0;
}

请大神们加以指正。

2021/7/2 20:45
加载中...