90TLE最后一个点求助
查看原帖
90TLE最后一个点求助
569180
Justin0779楼主2024/10/1 14:27
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;

int n, m, a[N], b[N];
bool v[N];
set<pair<int, int>> cd;

int main() {
    //freopen("main.in", "r", stdin);
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", a + i);
    for (int i = 1; i <= n; i++) scanf("%d", b + i);
    for (int i = 1; i <= n; i++) cd.insert(make_pair(a[i], i));

    while (m--) {
        auto it1 = cd.begin();
        pair<int, int> pl1 = *it1;
        cd.erase(it1);
        auto it2 = cd.end();
        it2--;
        pair<int, int> pr1 = *it2;
        cd.erase(it2);
        auto it3 = cd.begin();
        pair<int, int> pl2 = *it3;
        auto it4 = cd.end();
        it4--;
        pair<int, int> pr2 = *it4;
        bool flagl = 0, flagr = 0;
        int nowans = pr1.first - pl1.first;
        int ansl = max(pr1.first, b[pl1.second]) - min(pl2.first, b[pl1.second]);
        int ansr = max(pr2.first, b[pr1.second]) - min(pl1.first, b[pr1.second]);
        if (ansl < nowans) flagl = 1;
        if (ansr < nowans) flagr = 1;
        if (flagl && flagr) {
            if (ansl < ansr) {
                v[pl1.second] = 1;
                cd.insert(make_pair(b[pl1.second], pl1.second));
                cd.insert(pr1);
            }
            else {
                v[pr1.second] = 1;
                cd.insert(make_pair(b[pr1.second], pr1.second));
                cd.insert(pl1);
            }
        }
        else if (flagl) {
            v[pl1.second] = 1;
            cd.insert(make_pair(b[pl1.second], pl1.second));
            cd.insert(pr1);
        }
        else if (flagr) {
            v[pr1.second] = 1;
            cd.insert(make_pair(b[pr1.second], pr1.second));
            cd.insert(pl1);
        }
        else {
            cd.insert(pl1);
            cd.insert(pr1);
            break;
        }
    }

    auto it5 = cd.begin();
    pair<int, int> plans = *it5;
    auto it6 = cd.end();
    it6--;
    pair<int, int> prans = *it6;
    printf("%d", prans.first - plans.first);
    return 0;
}

按道理应该是正解的O(nlogn),但最后一个点1.03s

2024/10/1 14:27
加载中...