QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#117299#6627. Line Townsomethingnew#0 2ms6112kbC++238.6kb2023-06-30 21:18:062024-05-31 18:44:21

Judging History

你现在查看的是最新测评结果

  • [2024-05-31 18:44:21]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:6112kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-30 21:18:06]
  • 提交

answer

//  ↘ ⬇ ⬇ ⬇ ⬇ ⬇ ↙
//  ➡ @roadfromroi ⬅
//  ↗ ⬆ ⬆ ⬆ ⬆ ⬆ ↖
#include <iostream>
#include "vector"
#include "algorithm"
#include "numeric"
#include "climits"
#include "iomanip"
#include "bitset"
#include "cmath"
#include "map"
#include "deque"
#include "array"
#include "set"
#define all(x) x.begin(), x.end()
using namespace std;
struct segtree {
    int sz;
    int nka;
    vector<int> tree;
    vector<int> ex;
    void make(int n) {
        nka=n;
        sz = 1;
        ex.assign(n, 1);
        while (sz < n)
            sz *= 2;
        tree.assign(sz * 2, {});
        for (int i = 0; i < n; ++i) {
            tree[i + sz] = i;
        }
    }
    int get(int v) {
        v += sz;
        int res = 0;
        while (v) {
            res += tree[v];
            v /= 2;
        }
        return res;
    }
    void ch(int l) {
        if (ex[l] == 0)return;
        nka--;
        ex[l] = 0;
        int r = sz - 1;
        //cerr << l << ' ' << r << ' ' << sz << endl;
        l += sz;
        r += sz;
        while (l <= r) {
            if (l % 2 == 1) {
                tree[l++]--;
            }
            if (r % 2 == 0) {
                tree[r--]--;
            }
            l /= 2;r /= 2;
        }
        return;
    }
};
pair<int, int> calccalc(segtree &realind, deque<int> posit, deque<int> neg, int frsttype, int n) {
    int begtp = -frsttype, backtp;
    if (n%2)
        backtp = -begtp;
    else
        backtp = begtp;
    int res = 0;
    //cerr << begtp << ' ' << backtp << '\n';
    //cout << posit.size() << ' ' << neg.size() << endl;
    pair<int, int> pr = {-1, -1};
    while (!posit.empty() and !neg.empty()) {
        if (posit.size() == 1 and neg.size() == 1 and backtp + begtp == 0) {
            int rka = 0;
            if (backtp == 1) {
                rka = n-1-realind.get(posit.back()) + realind.get(neg.back()) - (neg.back() > posit.front());
            } else {
                rka = n-1-realind.get(posit.back()) + realind.get(neg.back()) - (neg.back() < posit.front());
            }
            if (frsttype == -1) {
                pr.first = res + rka;
            } else {
                pr.second = res + rka;
            }
        }
        int gofr, gobk;
        if (begtp == 1) {
            gofr = realind.get(posit.front()) + realind.get(neg.front()) - (posit.front() < neg.front());
        } else {
            gofr = realind.get(posit.front()) + realind.get(neg.front()) - (posit.front() > neg.front());
        }
        if (backtp == 1) {
            gobk = (n-1-realind.get(posit.back())) + (n-1-realind.get(neg.back())) - (posit.back() > neg.back());
        } else {
            gobk = (n-1-realind.get(posit.back())) + (n-1-realind.get(neg.back())) - (posit.back() < neg.back());
        }
       //cerr << gobk << ' ' << gofr <<'\n';
        bool gofrgd = gofr < gobk;
        if (gofr == gobk) {
            int ps = 0;
            for (auto i : posit) {
                int k = realind.get(i) % 2;
                if (frsttype == -1)
                    k++;
                k %= 2;
                if (k)
                    ps--;
                else
                    ps++;
            }
            for (auto i : neg) {
                int k = realind.get(i) % 2;
                if (frsttype == -1)
                    k++;
                k %= 2;
                if (k)
                    ps++;
                else
                    ps--;
            }
            if (ps < 0)
                gofrgd = 1;
        }
        //cerr << gofrgd << '\n';
        if (gofrgd) {
            res += gofr;
            realind.ch(posit.front());
            realind.ch(neg.front());
            posit.pop_front();
            neg.pop_front();
            n -= 2;
        } else {
            res += gobk;
            realind.ch(posit.back());
            realind.ch(neg.back());
            posit.pop_back();
            neg.pop_back();
            n -= 2;
        }
    }
    if (!posit.empty()) {
        if (posit.size() == 1 and begtp == 1 and backtp == 1) {
            int rind = realind.get(posit.front());
            pair<int, int> pr;
            pr.second = res + (n-1-rind);
            pr.first = res + rind;
            realind.ch(posit.front());
            return pr;
        } else {
            if (begtp == 1) {
                res += realind.get(posit.front());
                realind.ch(posit.front());
                posit.pop_front();
                frsttype = -frsttype;
                n--;
            }
            if (backtp == 1) {
                res += realind.get(posit.back());
                realind.ch(posit.back());
                posit.pop_back();
                n--;
            }
            if (!posit.empty()) {
                return {-1, -1};
            }
            pair<int, int> pr = {-1, -1};
            if (frsttype == 1)
                pr.first = res;
            else
                pr.second = res;
            return pr;
        }
    } else if (!neg.empty()) {
        if (neg.size() == 1 and begtp == -1 and backtp == -1) {
            int rind = realind.get(neg.front());
            pair<int, int> pr;
            pr.first = res + (n-1-rind);
            pr.second = res + rind;
            realind.ch(neg.front());
            return pr;
        } else {
            //cout << "DA\n";
            if (begtp == -1) {
                res += realind.get(neg.front());
                realind.ch(neg.front());
                neg.pop_front();
                frsttype = -frsttype;
                n--;
            }
            if (backtp == -1) {
                res += realind.get(neg.back());
                realind.ch(neg.back());
                neg.pop_back();
                n--;
            }
            if (!neg.empty()) {
                return {-1, -1};
            }
            pair<int, int> pr = {-1, -1};
            if (frsttype == 1)
                pr.first = res;
            else
                pr.second = res;
            return pr;
        }
    } else {
        if (frsttype == 1)
            pr.first = res;
        else
            pr.second = res;
        return pr;
    }
}
void upd(int &v, int x, int y) {
    if (x == -1)
        return;
    if (v == -1)
        v = x + y;
    v = min(v, x + y);
}
void solve() {
    int n;
    cin >> n;
    vector<int> mlt(n);
    vector<int> a(n);
    mlt[0] = 1;
    map<int, int> mp;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        mp[abs(a[i])] = 1;
        if (i != 0)
            mlt[i] = -mlt[i-1];
        a[i] *= mlt[i];
    }
    int cc = 1;
    for (auto &[key, val] : mp) {
        if (key == 0)
            val = 0;
        else
            val = cc++;
    }
    for (int i = 0; i < n; ++i) {
        if (a[i] > 0)
            a[i] = mp[a[i]];
        else
            a[i] = -mp[-a[i]];
    }
    vector<pair<deque<int>, deque<int>>> dcv(cc);
    for (int i = 0; i < n; ++i) {
        //cerr << a[i] << ' ';
        if (a[i] >= 0)
            dcv[a[i]].first.push_back(i);
        else
            dcv[-a[i]].second.push_back(i);
    }
   // cerr << '\n';
    segtree sg1, sg2;
    vector<pair<int, int>> realdp(cc, {-1,-1});
    realdp.back() = {0, -1};
    sg1.make(n);
    sg2.make(n);
    for (int i = cc-1; i > 0; --i) {
        if (realdp[i].first != -1) {
            pair<int, int> gt = calccalc(sg1, dcv[i].first, dcv[i].second, 1, sg1.nka);
            //cerr << i << ' ' << "fr" << ' ';
            //cerr << gt.first << ' ' << gt.second << '\n';
            upd(realdp[i-1].first, gt.first, realdp[i].first);
            upd(realdp[i-1].second, gt.second, realdp[i].first);
        }
        if (realdp[i].second != -1) {
            pair<int, int> gt = calccalc(sg2, dcv[i].first, dcv[i].second, -1, sg2.nka);
            //cerr << i << ' ' << "sc" << ' ';
            //cerr << gt.first << ' ' << gt.second << '\n';
            upd(realdp[i-1].first, gt.first, realdp[i].second);
            upd(realdp[i-1].second, gt.second, realdp[i].second);
        }
        for (auto qq : dcv[i].first) {
            sg1.ch(qq);
            sg2.ch(qq);
        }
        for (auto qq : dcv[i].second) {
            sg1.ch(qq);
            sg2.ch(qq);
        }
    }
    upd(realdp[0].first, realdp[0].second, 0);
    cout << realdp[0].first << '\n';
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    int t = 1;
    while (t--) {
        solve();
    }
}
/*
6
-2 7 -1 -8 2 8

 */

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 0ms
memory: 3568kb

input:

10
1 1 1 1 1 -1 -1 -1 1 -1

output:

-1

result:

ok 1 number(s): "-1"

Test #2:

score: -3
Wrong Answer
time: 0ms
memory: 3628kb

input:

10
1 1 1 1 1 1 -1 1 1 -1

output:

4

result:

wrong answer 1st numbers differ - expected: '3', found: '4'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Wrong Answer

Test #60:

score: 4
Accepted
time: 0ms
memory: 3520kb

input:

10
3 10 5 -9 7 2 -6 1 8 0

output:

-1

result:

ok 1 number(s): "-1"

Test #61:

score: 0
Accepted
time: 0ms
memory: 3872kb

input:

10
-9 0 -1 7 5 10 6 3 2 -8

output:

13

result:

ok 1 number(s): "13"

Test #62:

score: 0
Accepted
time: 2ms
memory: 6056kb

input:

2000
40667 -598150 -1084780 1201651 1570514 -1859539 -2029075 2941581 -2945945 3038404 3447919 5293872 -5335692 -5669647 5973784 6041345 6346915 -7222112 8820986 -9153143 9563103 9749206 -9894732 -11847193 11987150 12161864 13336572 13528051 -13722732 -13836176 -15497141 -15841563 15862227 16618123 ...

output:

-1

result:

ok 1 number(s): "-1"

Test #63:

score: -4
Wrong Answer
time: 0ms
memory: 6112kb

input:

2000
3038404 -798315545 693574695 172661079 516504064 164016456 193562146 -131746730 382134316 -398886978 188767854 -834289064 -965673210 -826409444 -281381674 450991903 -592752625 81651101 -594873306 -352059270 -651772982 540062674 -769881300 68999588 307151563 -129950325 550154987 354801227 840540...

output:

760485

result:

wrong answer 1st numbers differ - expected: '658039', found: '760485'

Subtask #6:

score: 0
Skipped

Dependency #5:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%