Submission #115215

# Submission time Handle Problem Language Result Execution time Memory
115215 2019-06-06T04:59:58 Z IOrtroiii Building Bridges (CEOI17_building) C++14
100 / 100
127 ms 64632 KB
/*
6
3 8 7 1 6 6
0 -1 9 1 2 0
*/
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const ll inf = 1e18;
const int N = 100100;
const int H = 1000100;

struct Line {
   int a; ll b;
   Line(int a = 0, ll b = inf) : a(a), b(b) {}
   ll f(int x) { return (ll) a * x + b; }
};

Line t[H << 2];

void add(int v, int l, int r, Line val) {
   if (t[v].f(l) >= val.f(l) && t[v].f(r) >= val.f(r)) {
      t[v] = val;  return;
   }
   if (t[v].f(l) <= val.f(l) && t[v].f(r) <= val.f(r)) {
      return;
   }
   int md = (l + r) >> 1;
   add(v << 1, l, md, val);
   add(v << 1 | 1, md + 1, r, val);
}

ll get(int v, int l, int r, int x) {
   if (l == r) { return t[v].f(x); }
   ll ans = t[v].f(x);
   int md = (l + r) >> 1;
   if (x <= md) ans = min(ans, get(v << 1, l, md, x));
   else ans = min(ans, get(v << 1 | 1, md + 1, r, x));
   return ans;
}

int n;
int h[N], w[N];
ll f[N];

int main() {
   scanf("%d", &n);
   for (int i = 1; i <= n; ++i) {
      scanf("%d", h + i);
   }
   for (int i = 1; i <= n; ++i) {
      scanf("%d", w + i);
   }
   for (int i = 1; i <= n; ++i) {
      if (i == 1) {
         f[i] = -w[i];
      } else {
         f[i] = get(1, 0, H - 1, h[i]) + (ll) h[i] * h[i] - w[i];
      }
      add(1, 0, H - 1, Line(-2 * h[i], (ll) h[i] * h[i] + f[i]));
   }
   ll ans = f[n];
   for (int i = 1; i <= n; ++i) {
      ans += w[i];
   }
   printf("%lld\n", ans);
}

Compilation message

building.cpp: In function 'int main()':
building.cpp:49:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d", &n);
    ~~~~~^~~~~~~~~~
building.cpp:51:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", h + i);
       ~~~~~^~~~~~~~~~~~~
building.cpp:54:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", w + i);
       ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 47 ms 62968 KB Output is correct
2 Correct 45 ms 62960 KB Output is correct
3 Correct 45 ms 62976 KB Output is correct
4 Correct 47 ms 62936 KB Output is correct
5 Correct 46 ms 62968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 64632 KB Output is correct
2 Correct 114 ms 64540 KB Output is correct
3 Correct 109 ms 64580 KB Output is correct
4 Correct 106 ms 64452 KB Output is correct
5 Correct 100 ms 64480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 62968 KB Output is correct
2 Correct 45 ms 62960 KB Output is correct
3 Correct 45 ms 62976 KB Output is correct
4 Correct 47 ms 62936 KB Output is correct
5 Correct 46 ms 62968 KB Output is correct
6 Correct 108 ms 64632 KB Output is correct
7 Correct 114 ms 64540 KB Output is correct
8 Correct 109 ms 64580 KB Output is correct
9 Correct 106 ms 64452 KB Output is correct
10 Correct 100 ms 64480 KB Output is correct
11 Correct 122 ms 64492 KB Output is correct
12 Correct 126 ms 64552 KB Output is correct
13 Correct 93 ms 64632 KB Output is correct
14 Correct 127 ms 64452 KB Output is correct
15 Correct 103 ms 64508 KB Output is correct
16 Correct 111 ms 64504 KB Output is correct
17 Correct 80 ms 64464 KB Output is correct
18 Correct 72 ms 64468 KB Output is correct