답안 #637604

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
637604 2022-09-02T13:07:10 Z stoyan_malinin Long Distance Coach (JOI17_coach) C++14
71 / 100
2000 ms 16292 KB
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#include <set>

using namespace std;

const int MAXN = 2e5;
const int64_t inf = 1e18;
const int64_t MAXX = 1e12;

struct Line
{
    int64_t a, b;

    Line(){}
    Line(int64_t a, int64_t b) : a(a), b(b) {}

    int64_t get(int64_t x) const
    {
        return a*x + b;
    }
};

struct LiChaoTree
{
    Line line;

    int64_t l, r;
    LiChaoTree *L, *R;

    LiChaoTree(int64_t l, int64_t r) : line(0, +inf), l(l), r(r), L(nullptr), R(nullptr) {}
    LiChaoTree(int64_t l, int64_t r, const Line& line) : line(line), l(l), r(r), L(nullptr), R(nullptr) {}

    void update(const Line &newLine)
    {
        int64_t mid = (l+r)/2;

        bool stateL = newLine.get(l) < line.get(l);
        bool stateMid = newLine.get(mid) < line.get(mid);

        Line lower, upper;
        if(stateMid==true) {lower = newLine; upper = line;}
        else {lower = line;upper = newLine;}

        line = lower;
        if(stateL!=stateMid)
        {
            if(L==nullptr) L = new LiChaoTree(l, mid, upper);
            else L->update(upper);
        }
        else
        {
            if(R==nullptr) R = new LiChaoTree(mid+1, r, upper);
            else R->update(upper);
        }
    }

    int64_t query(int64_t x) const
    {
        if(x<=(l+r)/2)
        {
            if(L==nullptr) return line.get(x);
            else return min(line.get(x), L->query(x));
        }
        else
        {
            if(R==nullptr) return line.get(x);
            else return min(line.get(x), R->query(x));
        }
    }

    ~LiChaoTree()
    {
        if(L!=nullptr) delete L;
        if(R!=nullptr) delete R;
    }
};

struct Passanger
{
    int64_t d, c;

    Passanger(){}
    Passanger(int64_t d, int64_t c) : d(d), c(c) {}
};

int64_t X, n, m, W, T;
int64_t sources[MAXN+5];
Passanger passangers[MAXN+5];
Passanger passangersPref[MAXN+5];

int64_t depart[MAXN+5];
int64_t endWaterPoints[MAXN+5];
int64_t endWaterPointsPrefix[MAXN+5];

int findLastPassangerBefore(int64_t rem)
{
    int l = 0, r = m - 1, mid;
    while(l+1<r)
    {
        mid = (l+r)/2;

        if(passangers[mid].d<rem) l = mid;
        else r = mid - 1;
    }

    if(passangers[r].d<rem) return r;
    if(passangers[l].d<rem) return l;
    return -1;
}

void init()
{
    sort(sources, sources+n);
    sort(passangers, passangers+m,
    [](const Passanger& A, const Passanger& B)
    {
        if(A.d!=B.d) return A.d<B.d;
        return A.c<B.c;
    });

    for(int i = 0;i<m;i++) depart[i] = -1;

    for(int i = 0;i<n;i++)
    {
        int ind = findLastPassangerBefore(sources[i]%T);
        if(ind!=-1 && depart[ind]==-1) depart[ind] = sources[i];
    }
    {
        int ind = findLastPassangerBefore(X%T);
        if(ind!=-1 && depart[ind]==-1) depart[ind] = X;
    }
}

int64_t dp[MAXN+5];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> X >> n >> m >> W >> T;
    for(int i = 0;i<n;i++) cin >> sources[i];
    for(int i = 0;i<m;i++) cin >> passangers[i].d >> passangers[i].c;
    init();

    int64_t driverWater = X/T + 1;

    int64_t endWaterPointsSum = 0;
    for(int i = 0;i<m;i++)
    {
        endWaterPoints[i] = ((X-passangers[i].d+(T-1))/T)*T + passangers[i].d;
        endWaterPointsSum += endWaterPoints[i];

        endWaterPointsPrefix[i] = endWaterPoints[i];
        if(i!=0) endWaterPointsPrefix[i] += endWaterPointsPrefix[i-1];
    }

    int64_t beginWaterPointsSum = 0;
    for(int i = 0;i<m;i++) beginWaterPointsSum += passangers[i].d;

    passangersPref[0] = passangers[0];
    for(int i = 1;i<m;i++)
    {
        passangersPref[i].d = passangersPref[i-1].d + passangers[i].d;
        passangersPref[i].c = passangersPref[i-1].c + passangers[i].c;
    }

    LiChaoTree lct(0, +100);
    //lct.update(Line(0, 0));

    for(int i = 0;i<m;i++)
    {
        dp[i] = inf;
        if(i!=0) dp[i] = dp[i-1];
        else dp[i] = 0;

        {
                int64_t lastDp = ((i==0)?0:dp[i-1]);
                int64_t lastEWPP = ((i==0)?0:endWaterPointsPrefix[i-1]);
                int64_t lastC = ((i==0)?0:passangersPref[i-1].c);
                int64_t lastD = ((i==0)?0:passangersPref[i-1].d);
                //int64_t val2 = lastDp - lastC + basedDiv*W*(-i) + ((-lastD + lastEWPP)/T)*W;

                lct.update(Line(W*(-i), lastDp - lastC + ((-lastD + lastEWPP)/T)*W));
        }

        if(depart[i]!=-1)
        {
            int64_t based = (depart[i]/T)*T;
            int64_t basedDiv = depart[i]/T;
            int64_t val1 = ((passangersPref[i].d - endWaterPointsPrefix[i])/T)*W + basedDiv*(i+1)*W + passangersPref[i].c;
            int64_t val2 = lct.query(basedDiv);

            dp[i] = min(dp[i], val1 + val2);
        }
    }

    int64_t ans = dp[m-1] + ((endWaterPointsSum-beginWaterPointsSum)/T)*W + driverWater*W;
    cout << ans << '\n';
}
/*
19 1 4 8 7
10
1 20
2 10
4 5
6 5
*/

Compilation message

coach.cpp: In function 'int main()':
coach.cpp:192:21: warning: unused variable 'based' [-Wunused-variable]
  192 |             int64_t based = (depart[i]/T)*T;
      |                     ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 9 ms 596 KB Output is correct
39 Correct 12 ms 596 KB Output is correct
40 Correct 18 ms 724 KB Output is correct
41 Correct 15 ms 724 KB Output is correct
42 Correct 15 ms 724 KB Output is correct
43 Correct 15 ms 724 KB Output is correct
44 Correct 16 ms 624 KB Output is correct
45 Correct 17 ms 652 KB Output is correct
46 Correct 19 ms 724 KB Output is correct
47 Correct 19 ms 628 KB Output is correct
48 Correct 16 ms 620 KB Output is correct
49 Correct 15 ms 692 KB Output is correct
50 Correct 8 ms 596 KB Output is correct
51 Correct 9 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 340 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 0 ms 340 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 9 ms 596 KB Output is correct
39 Correct 12 ms 596 KB Output is correct
40 Correct 18 ms 724 KB Output is correct
41 Correct 15 ms 724 KB Output is correct
42 Correct 15 ms 724 KB Output is correct
43 Correct 15 ms 724 KB Output is correct
44 Correct 16 ms 624 KB Output is correct
45 Correct 17 ms 652 KB Output is correct
46 Correct 19 ms 724 KB Output is correct
47 Correct 19 ms 628 KB Output is correct
48 Correct 16 ms 620 KB Output is correct
49 Correct 15 ms 692 KB Output is correct
50 Correct 8 ms 596 KB Output is correct
51 Correct 9 ms 596 KB Output is correct
52 Execution timed out 2074 ms 16292 KB Time limit exceeded
53 Halted 0 ms 0 KB -