本地正常, 提交错误, 合着我是写了个UB吗? 50pts求助
查看原帖
本地正常, 提交错误, 合着我是写了个UB吗? 50pts求助
320697
AMIRIOX無暝楼主2020/7/20 22:55

图1,2,3分别为本地/数据/洛谷IDE的输出 U5i5E4.png

U5izUH.png

U5F3rT.png 代码:

#include <algorithm>
#include <iostream>
using namespace std;
int x, n, ans, a[1000005];
int main() {
    cin >> x >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(a, a + n, greater_equal<int>());
    for (int i = 0; i < n; i++) {
        if (a[i] == x + 1) continue;
        for (int j = i + 1; j < n; j++) {
            if (a[i] + a[j] <= x && a[j] != x + 1) {
                //cout << ans + 1 << " " << a[i] << " " << a[j] << endl;
                a[j] = x + 1;
                ans++;
                break;
            }
            if (j == n - 1) {
                ans++;
                //cout << ans << " " << a[i] << endl;
            }
        }
    }
    cout << ans;
    return 0;
}

请问这里哪里有ub啊/lengh

2020/7/20 22:55
加载中...