Submission #653932

#TimeUsernameProblemLanguageResultExecution timeMemory
653932LoboRail (IOI14_rail)C++17
100 / 100
137 ms99456 KiB
#include "rail.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5050;
const int inf = 1e9+10;
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define all(x) x.begin(),x.end()

int n;
int d[maxn][maxn];
int p[maxn], tp[maxn];
int CNT =0;
int dist(int a, int b) {
    if(a == b) return d[a][b] = 0;
    if(d[a][b] != -1) return d[a][b];
    CNT++;
    assert(CNT <= 3*n-2);
    return d[a][b] = getDistance(a,b);
}

void findLocation(int N, int first, int location[], int stype[]) {
    n = N;
    for(int i = 0; i < n; i++){
        p[i] = -1;
        for(int j = 0; j < n; j++) {
            d[i][j] = -1;
        }
    }
    p[0] = first;
    tp[0] = 0;

    // find the closest to 0
    int b = -1;
    for(int i = 1; i < n; i++) {
        if(b == -1 || dist(0,i) < dist(0,b)) b = i;
    }
    p[b] = p[0]+dist(0,b);
    tp[b] = 1;
    // find the closest to b
    // int a = -1;
    // for(int i = 0; i < n; i++) {
    //     if(i == b) continue;
    //     if(a == -1 || dist(b,i) < dist(b,a)) a = i;
    // }
    // p[a] = p[b]-dist(b,a);
    // tp[a] = 0;
    int a = 0;
    int a1 = -1;
    for(int i = 0; i < n; i++) {
        if(i == b) continue;
        if(a1 == -1 || dist(b,i) < dist(b,a1)) a1 = i;
    }
    p[a1] = p[b]-dist(b,a1);
    tp[a1] = 0;
    // location[a] = p[a]; stype[a] = tp[a]+1;
    // location[b] = p[b]; stype[b] = tp[b]+1;

    // for(int i = 0; i < n; i++) {
    //     if(i == a || i == b) continue;
    //     if(dist(a,i) <= dist(b,i)) {
    //         location[i] = p[a]+dist(a,i);
    //         stype[i] = 2;
    //     }
    //     else {
    //         location[i] = p[b]-dist(b,i);
    //         stype[i] = 1;
    //     }
    // }

    vector<pair<int,int>> lft,rgt;
    for(int i = 0; i < n; i++) {
        if(i == a || i == b) continue;
        if(dist(a,i)-(p[a1]-p[a]) < dist(b,i)) {
            rgt.pb(mp(dist(a,i),i));
        }
        else {
            lft.pb(mp(dist(b,i),i));
        }
    }

    sort(all(rgt));
    sort(all(lft));
    int lastdw = a;
    int lastup = b;
    a = 0;
    assert(CNT <= 2*n-2);
    for(int i = 0; i < rgt.size(); i++) {
        int u = rgt[i].sc;
        int pup = p[a]+dist(a,u);
        int pdw = p[lastup]-dist(u,lastup);
        int v = lastup;
        for(int j = 0; j < i; j++) {
            int v1 = rgt[j].sc;
            if(tp[v1] == 1 && p[v1] > pdw) {
                v = v1;
                break;
            }
        }
        if(dist(a,u) == dist(a,v)+p[v]-pdw) {
            p[u] = pdw;
            tp[u] = 0;
        }
        else {
            p[u] = pup;
            tp[u] = 1;
            lastup = u;
        }
    }

    for(int i = 0; i < n; i++) {
        if(dist(b,i) < dist(a,i)) {
            if(dist(b,i) < dist(b,a) && p[i] == -1) {
                p[i] = p[b]-dist(b,i);
                tp[i] = 0;
            }
        }
    }

    for(int i = 0; i < lft.size(); i++) {
        int u = lft[i].sc;
        if(p[u] != -1) continue;
        int pdw = p[b]-dist(b,u);
        int pup = p[lastdw]+dist(u,lastdw);

        int v = lastdw;
        for(int j = 0; j < i; j++) {
            int v1 = lft[j].sc;
            if(tp[v1] == 0 && p[v1] < pup) {
                v = v1;
                break;
            }
        }

        if(dist(b,u) == dist(b,v) + pup-p[v]) {
            p[u] = pup;
            tp[u] = 1;
        }
        else {
            p[u] = pdw;
            tp[u] = 0;
            lastdw = u;
        }

        // p[u] = inf;
        // for(int j = 0; j < i; j++) {
        //     int v = lft[j].sc;
        //     if(tp[v] == 0 && 2*p[v]+dist(b,u)-p[b] > p[v] && dist(b,u) == dist(b,v)+dist(v,u)) {
        //         p[u] = min(p[u],2*p[v]+dist(b,u)-p[b]);
        //     }
        // }
        // if(p[u] == inf) {
        //     tp[u] = 0;
        //     p[u] = p[b]-dist(b,u);
        // }
        // else {
        //     tp[u] = 1;
        // }
    }

    for(int i = 0; i < n; i++) {
        location[i] = p[i];
        stype[i] = tp[i]+1;
        // cout << i << " - " << p[i] << " " << tp[i]+1 << endl;
    }
}

Compilation message (stderr)

rail.cpp: In function 'void findLocation(int, int, int*, int*)':
rail.cpp:90:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |     for(int i = 0; i < rgt.size(); i++) {
      |                    ~~^~~~~~~~~~~~
rail.cpp:122:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |     for(int i = 0; i < lft.size(); i++) {
      |                    ~~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...