样例过了,一交全WA,求调
查看原帖
样例过了,一交全WA,求调
1201743
QinYulang楼主2024/7/24 22:53
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define int long long 

std::vector <int> tree;
int n;
int x;
int minn = 1e9, maxx = -1e9;
int ans;
signed main(){
    std::cin >> n;
    std::cin >> x;
    tree.push_back(x);
    ans += x;
    for(int i = 2; i <= n; ++i){
        std::cin >> x;
        minn = *--std::lower_bound(tree.begin(), tree.end(), x);
        maxx = *std::upper_bound(tree.begin(), tree.end(), x);
        ans += std::min(abs(x - minn), abs(maxx - x));
        tree.insert(std::lower_bound(tree.begin(), tree.end(), x), x);
    }
    std::cout << ans << '\n';
    return 0;
}
2024/7/24 22:53
加载中...