QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#506659#9127. Optimal Train Operationhos_lyricWA 2ms4260kbC++144.3kb2024-08-05 20:31:042024-08-05 20:31:05

Judging History

你现在查看的是最新测评结果

  • [2024-08-05 20:31:05]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:4260kb
  • [2024-08-05 20:31:04]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


constexpr Int INF = 1001001001001001001LL;

/*
  min
  slope: decr.
  eval at: decr.
*/
struct Cht {
  vector<pair<Int, Int>> stack;
  Cht() {
// cerr<<"[Cht]"<<endl;
  }
  ~Cht() {
// cerr<<"[~Cht]"<<endl;
  }
  void add(Int p, Int q) {
// cerr<<"  [add] "<<p<<" "<<q<<endl;
    if (stack.size() >= 1) assert(stack.back().first >= p);
// stack.emplace_back(p,q);return;
    if (stack.size() >= 1 && stack.back().first == p) {
      if (stack.back().second <= q) {
        return;
      } else {
        stack.pop_back();
      }
    }
    for (; stack.size() >= 2; ) {
      const Int p2 = stack.end()[-2].first, q2 = stack.end()[-2].second;
      const Int p1 = stack.end()[-1].first, q1 = stack.end()[-1].second;
      // (q1 - q2) / (p2 - p1) < (q - q1) / (p1 - p)
      if ((q1 - q2) * (p1 - p) < (q - q1) * (p2 - p1)) {
        break;
      } else {
        stack.pop_back();
      }
    }
    stack.emplace_back(p, q);
  }
  Int get(Int x) {
// cerr<<"  [get] "<<x<<endl;
// Int mn=INF;for(const auto&l:stack)chmin(mn,l.first*x+l.second);return mn;
    for (; stack.size() >= 2; ) {
      const Int p2 = stack.end()[-2].first, q2 = stack.end()[-2].second;
      const Int p1 = stack.end()[-1].first, q1 = stack.end()[-1].second;
      if (p2 * x + q2 > p1 * x + q1) {
        break;
      } else {
        stack.pop_back();
      }
    }
    return stack.size() ? (stack.back().first * x + stack.back().second) : INF;
  }
};

int N;
vector<Int> C, A;

vector<Int> dp;

void rec(int l, int r) {
  if (l + 1 == r) {
    chmin(dp[r], dp[l] + A[l] + C[l]);
  } else {
    const int m = (l + r) / 2;
    rec(l, m);
    
    // [l, m) -> (m, r]
    // max in left
    {
      Int c = 0;
      vector<vector<pair<Int, Int>>> liness(r - m + 1);
      for (int i = m, j = m; --i >= l; ) {
        chmax(c, C[i]);
        for (; j < r && c >= C[j]; ++j) {}
        // dp[i] + A[i] + c (j - i)
        liness[j - m].emplace_back(c, dp[i] + A[i] - c * i);
      }
      Cht cht;
      for (int j = r; j > m; --j) {
        reverse(liness[j - m].begin(), liness[j - m].end());
        for (const auto &line : liness[j - m]) cht.add(line.first, line.second);
        const Int res = cht.get(j);
        chmin(dp[j], res);
      }
    }
    // max in right
    {
      Int c = 0;
      Cht cht;
      for (int i = m, j = m; ++j <= r; ) {
        chmax(c, C[j - 1]);
        for (; i > l && C[i - 1] <= c; ) {
          --i;
          // dp[i] + A[i] + c (j - i)
          cht.add(i, dp[i] + A[i]);
        }
        const Int res = cht.get(-c);
        chmin(dp[j], res + c * j);
      }
    }
    
    rec(m, r);
  }
}

int main() {
  for (; ~scanf("%d", &N); ) {
    C.resize(N);
    for (int i = 0; i < N; ++i) scanf("%lld", &C[i]);
    A.assign(N + 1, 0);
    for (int i = 1; i < N; ++i) scanf("%lld", &A[i]);
// reverse(C.begin(),C.end());
// reverse(A.begin(),A.end());
    
    dp.assign(N + 1, INF);
    dp[0] = 0;
    rec(0, N);
// cerr<<"dp = "<<dp<<endl;
    printf("%lld\n", dp[N]);
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3776kb

input:

4
3 1 4 1
5 9 2

output:

15

result:

ok "15"

Test #2:

score: 0
Accepted
time: 0ms
memory: 4072kb

input:

9
28 35 19 27 84 98 78 79 60
40 35 54 63 72 71 27 94

output:

682

result:

ok "682"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3864kb

input:

7
476190629 262703497 211047202 971407775 628894325 731963982 822804784
877914575 602436426 24979445 861648772 623690081 433933447

output:

5339182432

result:

ok "5339182432"

Test #4:

score: 0
Accepted
time: 0ms
memory: 3796kb

input:

8
910286918 802329211 404539679 303238506 317063340 492686568 773361868 125660016
430302156 982631932 161735902 880895728 923078537 707723857 189330739

output:

6166732840

result:

ok "6166732840"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3792kb

input:

5
191890310 576823355 782177068 404011431 818008580
839296263 462224593 492601449 384836991

output:

4069897043

result:

ok "4069897043"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3768kb

input:

6
138996221 501899080 353195922 545531545 910748511 350034067
160449218 155374934 840594328 164163676 797829355

output:

3921700772

result:

ok "3921700772"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

2
824284691 533206504
470338674

output:

1648569382

result:

ok "1648569382"

Test #8:

score: 0
Accepted
time: 2ms
memory: 4036kb

input:

4524
43 144 216 44 156 252 243 172 78 246 56 103 207 177 102 22 202 236 61 255 128 238 237 187 176 110 156 213 65 11 75 239 142 9 203 124 154 149 119 59 152 91 151 226 131 184 174 236 220 149 16 209 51 32 40 131 119 138 201 171 249 150 108 82 75 70 204 80 131 42 180 125 257 131 5 107 39 145 154 242 ...

output:

892773

result:

ok "892773"

Test #9:

score: 0
Accepted
time: 2ms
memory: 4244kb

input:

3554
15524 576548 853763 358257 912947 607318 377277 594520 590783 922703 139384 266137 693731 799505 247238 995905 514827 263444 611535 122831 466617 516802 5006 249914 605892 791993 636436 679336 42260 219660 532740 966264 550887 364610 954581 894777 429135 168854 890132 790960 133682 762293 35996...

output:

2727153111

result:

ok "2727153111"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3760kb

input:

618
617818 785227 582280 603843 289182 885322 490594 955276 765859 270207 215181 99610 230078 709255 874840 899990 333912 389237 680551 146846 415280 388688 972914 305729 502870 21714 512132 859972 340081 705834 164476 561312 273914 606331 506703 839809 703999 226779 460292 520904 12001 368981 16914...

output:

469678591

result:

ok "469678591"

Test #11:

score: 0
Accepted
time: 0ms
memory: 4008kb

input:

3476
460 467 505 792 564 820 253 906 684 341 681 33 894 135 17 8 963 554 659 655 670 554 354 401 933 360 64 275 484 287 233 924 148 260 893 375 845 781 793 848 497 219 95 242 419 48 278 842 912 585 655 163 810 101 262 764 547 136 54 245 430 498 825 402 243 970 881 219 382 603 41 550 385 38 452 821 4...

output:

3375770

result:

ok "3375770"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

366
307564 238187 24120 317824 293689 612357 307612 200273 261934 550075 549358 179086 655772 147193 973655 531657 48998 969881 414037 843784 467459 54188 161082 804295 839360 187192 827372 446836 348027 871477 700772 294194 471337 887311 190447 870934 752144 441831 582896 578093 968014 168146 30748...

output:

183717682

result:

ok "183717682"

Test #13:

score: 0
Accepted
time: 2ms
memory: 3992kb

input:

3774
747364 498725 806506 764437 879363 395703 431280 16381 355651 877649 215994 979465 937295 42841 164430 174217 363111 612775 295749 663934 47519 955015 887821 389919 497309 205370 735527 784727 724382 534900 51497 994308 968305 587359 690172 337786 47994 513239 982751 821246 955249 208493 264482...

output:

2891998925

result:

ok "2891998925"

Test #14:

score: 0
Accepted
time: 1ms
memory: 3792kb

input:

485
190 367 401 35 21 60 101 251 348 287 10 98 91 316 233 414 133 343 75 139 382 271 285 170 5 217 244 346 336 476 349 231 65 296 212 211 297 86 458 86 49 277 264 256 429 270 184 79 72 326 93 137 101 384 64 353 300 26 226 207 95 377 441 281 208 168 422 400 178 117 423 116 250 154 364 32 353 424 229 ...

output:

233770

result:

ok "233770"

Test #15:

score: 0
Accepted
time: 1ms
memory: 3908kb

input:

813
104165 461736 432346 532684 32243 732999 41991 441755 241314 914813 859856 479562 592827 555964 742136 631640 554269 453290 992854 376052 33976 634595 74838 694020 803923 740597 727800 203295 860615 237475 59216 857107 140610 845731 966596 350560 124293 967008 989457 555002 243942 685560 482247 ...

output:

621448685

result:

ok "621448685"

Test #16:

score: 0
Accepted
time: 2ms
memory: 4064kb

input:

4677
265 78 203 65 79 75 176 78 164 141 2 167 193 239 200 134 237 218 238 39 29 81 13 232 194 33 195 102 261 138 31 178 62 93 100 262 12 187 16 195 11 196 183 200 141 233 83 104 176 37 47 162 100 124 137 42 89 266 207 111 212 106 62 25 225 97 143 59 6 203 66 24 42 66 153 86 41 245 151 165 87 155 149...

output:

1248759

result:

ok "1248759"

Test #17:

score: 0
Accepted
time: 1ms
memory: 4216kb

input:

2984
892149722 832487302 761446835 752750294 109973448 988924894 571856631 832571491 878761234 256657009 211486419 914755434 745727757 866030689 524953892 512094120 562932415 815609767 531046587 483522799 313216354 459745678 775516124 143131556 898264293 261953334 500159168 241077569 244120682 51547...

output:

1502349543535

result:

ok "1502349543535"

Test #18:

score: -100
Wrong Answer
time: 0ms
memory: 4260kb

input:

4125
780998210 140156214 178580275 103156375 918633302 35837731 340956454 617478278 98747470 595646587 89092407 791462530 712604641 889286351 674818038 949482629 375115292 10500387 93431434 381797694 17003742 499105392 693439788 710482576 689837658 663297571 348609450 410750993 53446039 32268955 285...

output:

3113937008080

result:

wrong answer 1st words differ - expected: '3113815996980', found: '3113937008080'