답안 #434727

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434727 2021-06-21T15:51:10 Z blue Road Construction (JOI21_road_construction) C++17
5 / 100
10000 ms 58400 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;

/*
1. Normalize Y coordinates
Binary search for K'th smallest distance:

WLOG x[1] <= ... <= x[N]
The cost for the point i and the point j is (x[j] - x[i]) + |y[j] - y[i]|.
                    y[i] < y[j]             (x[j] + y[j]) - (x[i] + y[i])    Segtree S
                    y[i] >= y[j]            (x[j] - y[j]) - (x[i] - y[i])    Segtree T

Maintain two segment trees: In one store the x[i] + y[i] values, in the other store the x[i] - y[i]
values.
For each point compute the normalized Y coordinate.
*/

const long long INF = 1'000'000'000'000;
const int maxN = 250000;

struct segtree
{
    int l;
    int r;
    pair<int, long long> v; //range max

    segtree* left = NULL;
    segtree* right = NULL;

    segtree()
    {
        ;
    }

    segtree(int L, int R)
    {
        l = L;
        r = R;
        v = make_pair(l, -INF);
        if(l == r) return;
        int m = (l+r)/2;
        left = new segtree(l, m);
        right = new segtree(m+1, r);
    }

    void update(int I, long long V)
    {
        if(I < l || r < I) return;
        else if(l == r) v = make_pair(l, V);
        else
        {
            left->update(I, V);
            right->update(I, V);

            pair<int, long long> LP = left->v;
            pair<int, long long> RP = right->v;

            if(LP.second > RP.second) v = LP;
            else v = RP;
        }
    }

    pair<int, long long> rangemax(int L, int R)
    {
        if(R < l || r < L) return make_pair(-1, -INF);
        else if(L <= l && r <= R) return v;
        else
        {
            pair<int, long long> LP = left->rangemax(L, R);
            pair<int, long long> RP = right->rangemax(L, R);

            if(LP.second > RP.second) return LP;
            else return RP;
        }
    }
};

int N, K;

struct point
{
    long long X;
    long long Y;
    int Yindex;
};

vector<point> P;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> N >> K;

    P = vector<point>(N);

    for(int i = 0; i < N; i++) cin >> P[i].X >> P[i].Y;

    sort(P.begin(), P.end(), [] (point A, point B)
    {
        return A.Y < B.Y;
    });

    for(int i = 0; i < N; i++) P[i].Yindex = i+1;

    sort(P.begin(), P.end(), [] (point A, point B)
    {
        return A.X < B.X;
    });

    // for(point p:P) cerr << p.X << ' ' << p.Y << ' ' << p.Yindex << '\n';

    segtree S(1, N), T(1, N);

    long long lo = 0, hi = 4'000'000'000LL;
    vector<long long> res;
    while(1)
    {
        long long Kval = (lo+hi)/2;
        res.clear();
        for(int i = 1; i <= N; i++)
        {
            S.update(i, -INF);
            T.update(i, -INF);
        }

        // cerr << "searching " << lo << ' ' << hi << '\n';


        for(point p:P)
        {
            vector< pair<int, long long> > updates;

            while(res.size() < K+1)
            {
                pair<int, long long> Q = S.rangemax(1, p.Yindex);
                if(Q.second <= -INF) break;

                if((p.X + p.Y) - Q.second > Kval) break;

                res.push_back((p.X + p.Y) - Q.second);

                S.update(Q.first, -INF);

                updates.push_back(Q);
            }

            for(pair<int, long long> u: updates)
                S.update(u.first, u.second);

            updates.clear();






            while(res.size() < K+1)
            {
                pair<int, long long> Q = T.rangemax(p.Yindex+1, N);
                if(Q.second <= -INF) break;

                if((p.X - p.Y) - Q.second > Kval) break;

                res.push_back((p.X - p.Y) - Q.second);

                T.update(Q.first, -INF);

                updates.push_back(Q);
            }

            for(pair<int, long long> u: updates)
                T.update(u.first, u.second);

            updates.clear();






            S.update(p.Yindex, p.X + p.Y);
            T.update(p.Yindex, p.X - p.Y);

            if(res.size() >= K+1) break;
        }

        sort(res.begin(), res.end());
        if(lo == hi)
        {
            while(res.size() > K) res.pop_back();
            break;
        }
        else if(res.size() >= K) hi = min(res[K-1], Kval);
        else lo = Kval + 1;
    }
    for(long long r:res) cout << r << '\n';
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:139:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  139 |             while(res.size() < K+1)
      |                   ~~~~~~~~~~~^~~~~
road_construction.cpp:163:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  163 |             while(res.size() < K+1)
      |                   ~~~~~~~~~~~^~~~~
road_construction.cpp:190:27: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  190 |             if(res.size() >= K+1) break;
      |                ~~~~~~~~~~~^~~~~~
road_construction.cpp:196:30: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  196 |             while(res.size() > K) res.pop_back();
      |                   ~~~~~~~~~~~^~~
road_construction.cpp:199:28: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  199 |         else if(res.size() >= K) hi = min(res[K-1], Kval);
      |                 ~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3240 ms 5220 KB Output is correct
2 Correct 3178 ms 5212 KB Output is correct
3 Correct 2993 ms 5172 KB Output is correct
4 Correct 2904 ms 5360 KB Output is correct
5 Correct 2861 ms 4012 KB Output is correct
6 Correct 23 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 10096 ms 55312 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8413 ms 53060 KB Output is correct
2 Correct 8872 ms 58180 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 6313 ms 55968 KB Output is correct
5 Execution timed out 10066 ms 58400 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 8413 ms 53060 KB Output is correct
2 Correct 8872 ms 58180 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 6313 ms 55968 KB Output is correct
5 Execution timed out 10066 ms 58400 KB Time limit exceeded
# 결과 실행 시간 메모리 Grader output
1 Correct 3240 ms 5220 KB Output is correct
2 Correct 3178 ms 5212 KB Output is correct
3 Correct 2993 ms 5172 KB Output is correct
4 Correct 2904 ms 5360 KB Output is correct
5 Correct 2861 ms 4012 KB Output is correct
6 Correct 23 ms 460 KB Output is correct
7 Execution timed out 10029 ms 23700 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3240 ms 5220 KB Output is correct
2 Correct 3178 ms 5212 KB Output is correct
3 Correct 2993 ms 5172 KB Output is correct
4 Correct 2904 ms 5360 KB Output is correct
5 Correct 2861 ms 4012 KB Output is correct
6 Correct 23 ms 460 KB Output is correct
7 Execution timed out 10096 ms 55312 KB Time limit exceeded
8 Halted 0 ms 0 KB -