2 solutions

  • 0
    @ 2026-10-12 13:55:51

    exit

    每个位置的人的贡献在 x±min⁡{x,L−x}x\pm \min\{x,L-x\} 和 xx 这三个点会开始变。直接维护拐点的斜率变化,极值点一定在顶点能取到,把拐点位置排序一下扫一遍就行。

    • 0
      @ 2026-10-12 13:53:09
      #include <bits/stdc++.h>
      using namespace std;
      #define FIO(s)                    \
          freopen(s ".in", "r", stdin); \
          freopen(s ".out", "w", stdout);
      using ll = long long;
      template <typename T>
      void chkmn(T &x, T y) { x = min(x, y); }
      int n, L;
      ll ans;
      vector<tuple<int, ll>> vec;
      int main()
      {
          FIO("exit");
          ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
          cin >> n >> L;
          for (int i = 1; i <= n; i++)
          {
              int x;
              ll w;
              cin >> x >> w;
              ll c = min(x, L - x);
              vec.emplace_back(x - c, -w);
              vec.emplace_back(x, 2 * w);
              vec.emplace_back(x + c, -w);
              ans += c * w;
          }
          vec.emplace_back(L, 0);
          sort(vec.begin(), vec.end());
          ll sum = ans;
          ll K = 0;
          ll lst = 0;
          for (int i = 0; i < vec.size();)
          {
              auto [x, w] = vec[i];
              sum += K * (x - lst);
              chkmn(ans, sum);
              while (i < vec.size() && get<0>(vec[i]) == x)
                  K += get<1>(vec[i++]);
              lst = x;
          }
          cout << ans << '\n';
          return 0;
      }
      
      • 1

      Information

      ID
      3148
      Time
      2000ms
      Memory
      1024MiB
      Difficulty
      9
      Tags
      (None)
      # Submissions
      10
      Accepted
      4
      Uploaded By