Submission #861822

# Submission time Handle Problem Language Result Execution time Memory
861822 2023-10-17T03:29:52 Z sleepntsheep Salesman (IOI09_salesman) C++17
100 / 100
367 ms 37836 KB
#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll =long long;
using ld=long double;

const ll N = 555555, inf = 1e9;
int n, d, u, s, X = 500005;

struct binarytree
{
    int t[N*2];
    binarytree() {memset(t,0xbf,sizeof t);}
    inline int qry(int l, int r)
    {
        int z=-1e9;
        for (l+=X,r+=X+1;l<r;l/=2,r/=2){
            if(l&1)z=max(z,t[l++]);
            if(r&1)z=max(t[--r],z);
        }
        return z;
    }
    inline void upd(int p, int k) { for(t[p+=X]=k;p/=2;)t[p]=max(t[p*2],t[p*2+1]); }
} thi, tlo;

struct fair
{
    int pos, money;
    bool operator<(const fair &o) const { return pos<o.pos; }
};
vector<fair> a[N];

inline void upd(ll pos, ll val)
{
    thi.upd(pos, val - u * pos);
    tlo.upd(pos, val - (X - pos) * d);
}

inline int cost(int pos)
{
    return max(thi.qry(pos, X-1) + u * pos, tlo.qry(0, pos) + (X - pos) * d);
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin >> n >> u >> d >> s;
    for (int d, p, m, i = 0; i < n; ++i) cin >> d >> p >> m, a[d].push_back(fair{p, m});
    upd(s, 0);
    for (auto i = 0; i <= 500000; ++i)
    {
        auto &x = a[i];
        if (x.empty()) continue;
        sort(x.begin(), x.end());
        if (x.size() == 1)
            upd(x[0].pos, x[0].money + cost(x[0].pos));
        else
        {
            vector<int> temp(x.size(), -inf), rtemp(x.size(), -inf);
            for (int i = 0; i < x.size(); ++i) temp[i] = rtemp[i] = x[i].money + cost(x[i].pos);
            for (int i = 1; i < x.size(); ++i) temp[i] = max(temp[i], temp[i-1] - (x[i].pos - x[i-1].pos) * d + x[i].money);
            for (int i = (int)x.size() - 2; i >= 0; --i) rtemp[i] = max(rtemp[i], rtemp[i+1] - (x[i+1].pos - x[i].pos) * u + x[i].money);
            for (int i = 0; i < x.size(); ++i) upd(x[i].pos, max(temp[i], rtemp[i]));
        }
    }
    cout << cost(s);

    return 0;
}


Compilation message

salesman.cpp: In function 'int main()':
salesman.cpp:69:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<fair>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |             for (int i = 0; i < x.size(); ++i) temp[i] = rtemp[i] = x[i].money + cost(x[i].pos);
      |                             ~~^~~~~~~~~~
salesman.cpp:70:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<fair>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |             for (int i = 1; i < x.size(); ++i) temp[i] = max(temp[i], temp[i-1] - (x[i].pos - x[i-1].pos) * d + x[i].money);
      |                             ~~^~~~~~~~~~
salesman.cpp:72:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<fair>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |             for (int i = 0; i < x.size(); ++i) upd(x[i].pos, max(temp[i], rtemp[i]));
      |                             ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 22108 KB Output is correct
2 Correct 4 ms 22108 KB Output is correct
3 Correct 5 ms 22160 KB Output is correct
4 Correct 5 ms 22108 KB Output is correct
5 Correct 8 ms 22364 KB Output is correct
6 Correct 18 ms 22620 KB Output is correct
7 Correct 38 ms 23768 KB Output is correct
8 Correct 84 ms 25500 KB Output is correct
9 Correct 99 ms 26708 KB Output is correct
10 Correct 199 ms 31572 KB Output is correct
11 Correct 227 ms 31572 KB Output is correct
12 Correct 262 ms 34680 KB Output is correct
13 Correct 295 ms 34896 KB Output is correct
14 Correct 337 ms 37716 KB Output is correct
15 Correct 320 ms 37836 KB Output is correct
16 Correct 367 ms 37712 KB Output is correct
17 Correct 4 ms 22104 KB Output is correct
18 Correct 4 ms 22108 KB Output is correct
19 Correct 5 ms 22108 KB Output is correct
20 Correct 5 ms 22108 KB Output is correct
21 Correct 5 ms 22108 KB Output is correct
22 Correct 6 ms 22108 KB Output is correct
23 Correct 9 ms 22120 KB Output is correct
24 Correct 6 ms 22108 KB Output is correct
25 Correct 56 ms 23264 KB Output is correct
26 Correct 102 ms 24316 KB Output is correct
27 Correct 170 ms 25916 KB Output is correct
28 Correct 190 ms 26388 KB Output is correct
29 Correct 242 ms 26964 KB Output is correct
30 Correct 247 ms 27656 KB Output is correct