Advertisement
SorahISA

基地台與房屋仲介 (wifi2)

Jul 16th, 2023
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int int64_t
  5.  
  6. #define ALL(x) begin(x), end(x)
  7. #define SZ(x) ((int)(x).size())
  8.  
  9. const int INF = INT_MAX >> 1;
  10.  
  11. struct Line {
  12.     int h, p, c;
  13.     Line(int _h = 0, int _p = 0, int _c = 0) {h = _h, p = _p, c = _c;}
  14.     /// 取位置 x 的值(下取整) ///
  15.     int operator() (int x) {return c * h / (abs(x-p) + c);}
  16.     /// 取位置 x 的值(分數) ///
  17.     pair<int, int> get_val(int x) {return {c * h, abs(x-p) + c};}
  18. };
  19.  
  20. /// 求香蕉位置(分數) ///
  21. pair<int, int> banana(Line a, Line b) {
  22.     // assert(a.h < b.h and a.p > b.p);
  23.     return {b.h * a.p - b.p * a.h - b.h + a.h, b.h - a.h};
  24. }
  25.  
  26. /// 比較分數大小 ///
  27. bool cmp_le(pair<int, int> a, pair<int, int> b) {
  28.     if (a.first / a.second != b.first / b.second) return a.first / a.second < b.first / b.second;
  29.     a.first %= a.second, b.first %= b.second;
  30.     /// banana 回傳的分子是 O(NB)、分母是 O(B),如果沒有上面的處理可能會爆 long long ///
  31.     return a.first * b.second <= b.first * a.second;
  32. }
  33.  
  34. int32_t main() {
  35.     ios_base::sync_with_stdio(0), cin.tie(0);
  36.    
  37.     int N, C; cin >> N >> C;
  38.    
  39.     vector<int> B(N);
  40.     for (int &x : B) cin >> x;
  41.    
  42.     vector<int> A(N, 0);
  43.    
  44.     for (int _ : {0, 1}) {
  45.         deque<Line> deq{Line(0, -1, C)};
  46.        
  47.         for (int i = 0; i < N; ++i) {
  48.             while (SZ(deq) >= 2 and cmp_le(deq[0].get_val(i), deq[1].get_val(i))) deq.pop_front();
  49.             A[i] = max(A[i], deq[0](i));
  50.             if (deq[0](i) >= B[i]) continue;
  51.            
  52.             Line now(B[i], i, C);
  53.             while (SZ(deq) and deq[0].h <= now.h) deq.pop_front();
  54.             while (SZ(deq) >= 2 and cmp_le(banana(deq[0], deq[1]), banana(now, deq[0]))) deq.pop_front();
  55.             deq.emplace_front(now);
  56.         }
  57.        
  58.         reverse(ALL(A)), reverse(ALL(B));
  59.     }
  60.    
  61.     // for (int i = 0; i < N; ++i) cout << A[i] << " \n"[i == N-1];
  62.    
  63.     for (int i = 0; i < N; ++i) {
  64.         if (A[i] > B[i]) return cout << "No" << "\n", 0;
  65.     }
  66.     cout << "Yes" << "\n";
  67.     for (int i = 0; i < N; ++i) {
  68.         cout << (A[i] == B[i] ? 0 : B[i]) << " \n"[i == N-1];
  69.     }
  70.    
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement