Submission #1046178

#TimeUsernameProblemLanguageResultExecution timeMemory
1046178ZicrusTeleporters (IOI08_teleporters)C++17
65 / 100
1064 ms65536 KiB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll n, m;
vector<ll> a, r;
vector<bool> vst;
ll r1 = 0;

void simulateVst(vector<ll> &a) {
    ll res = 0;
    ll i = 0;
    while (i < a.size()) {
        r[i] = res;
        i = a[i];
        vst[i] = true;
        i++;
        res++;
    }
    r1 = res;
}

ll simulateRange(vector<ll> &a, ll start) {
    ll res = r[start]+1;
    ll i = start+1;
    while (i != a[start]+1) {
        i = a[i];
        if (i == start) {
            i--;
            res++;
        }
        i++;
        res++;
    }
    return res + (i < r.size() ? r1 - r[i] : 0);
}

ll simulate(vector<ll> &a) {
    ll res = 0;
    ll i = 0;
    while (i < a.size()) {
        i = a[i];
        i++;
        res++;
    }
    return res;
}

int main() {
    ios::sync_with_stdio(0);

    cin >> n >> m;
    vector<pair<ll, ll>> tp;
    for (int i = 0; i < n; i++) {
        ll x, y;
        cin >> x >> y;
        tp.push_back({x, y});
        tp.push_back({y, x});
    }
    sort(tp.begin(), tp.end());

    unordered_map<ll, ll> ids;
    for (int i = 0; i < 2*n; i++) {
        ids[tp[i].first] = i;
    }
    a = vector<ll>(2*n);
    for (int i = 0; i < 2*n; i++) {
        a[i] = ids[tp[i].second];
    }

    ll mx = m == 0 ? simulate(a) : 0;
    while (m--) {
        vst = vector<bool>(a.size());
        r = vector<ll>(a.size());
        simulateVst(a);
        ll mxA = -1;
        mx = r1+1;
        for (int i = 0; i < a.size(); i++) {
            if (a[i] < i || vst[i] || !vst[a[i]]) continue;
            ll val = simulateRange(a, i);
            if (val > mx) {
                mx = val;
                mxA = i;
            }
        }
        if (m == 0) continue;
        if (mxA != -1) {
            vector<ll> nw;
            for (int j = 0; j < a.size(); j++) {
                if (j == mxA) {
                    nw.push_back(mxA+2);
                    nw.push_back(a[mxA]+2);
                    nw.push_back(mxA);
                    continue;
                }
                if (a[j] == mxA) nw.push_back(mxA+1);
                else nw.push_back(a[j] > mxA ? a[j]+2 : a[j]);
            }
            a = nw;
        }
        else {
            a.push_back(a.size()+1);
            a.push_back(a.size()-1);
        }
    }

    cout << mx;
}

Compilation message (stderr)

teleporters.cpp: In function 'void simulateVst(std::vector<long long int>&)':
teleporters.cpp:17:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     while (i < a.size()) {
      |            ~~^~~~~~~~~~
teleporters.cpp: In function 'll simulateRange(std::vector<long long int>&, ll)':
teleporters.cpp:39:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |     return res + (i < r.size() ? r1 - r[i] : 0);
      |                   ~~^~~~~~~~~~
teleporters.cpp: In function 'll simulate(std::vector<long long int>&)':
teleporters.cpp:45:14: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     while (i < a.size()) {
      |            ~~^~~~~~~~~~
teleporters.cpp: In function 'int main()':
teleporters.cpp:82:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for (int i = 0; i < a.size(); i++) {
      |                         ~~^~~~~~~~~~
teleporters.cpp:93:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |             for (int j = 0; j < a.size(); j++) {
      |                             ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...