Dmaxiya

[蓝桥杯 2026 省 B] 理想温度 参考代码

Apr 12th, 2026
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5.  
  6. typedef long long LL;
  7. const int maxn = 200000 + 100;
  8.  
  9. int main() {
  10.     ios::sync_with_stdio(false);
  11.  
  12.     int n;
  13.     cin >> n;
  14.     vector<int> a(n + 1), b(n + 1);
  15.     for (int i = 1; i <= n; ++i) {
  16.         cin >> a[i];
  17.     }
  18.     for (int i = 1; i <= n; ++i) {
  19.         cin >> b[i];
  20.         a[i] -= b[i];
  21.     }
  22.  
  23.     map<int, vector<int>> idxs;
  24.     vector<int> leq(n + 2, 0), req(n + 2, 0);
  25.     for (int i = 1; i <= n; ++i) {
  26.         idxs[a[i]].push_back(i);
  27.         leq[i] = leq[i - 1];
  28.         if (a[i] == 0) {
  29.             ++leq[i];
  30.         }
  31.     }
  32.     for (int i = n; i >= 1; --i) {
  33.         req[i] = req[i + 1];
  34.         if (a[i] == 0) {
  35.             ++req[i];
  36.         }
  37.     }
  38.  
  39.     int ans = leq[n];
  40.     vector<int> suffixMax;
  41.     for (const auto& [d, idxList] : idxs) {
  42.         suffixMax.assign(idxList.size() + 1, 0);
  43.         for (int i = idxList.size() - 1; i >= 0; --i) {
  44.             int idx = idxList[i];
  45.             suffixMax[i] = max(suffixMax[i + 1], i + 1 + req[idx + 1]);
  46.         }
  47.         for (int i = 0; i < (int)idxList.size(); ++i) {
  48.             int idx = idxList[i];
  49.             ans = max(ans, leq[idx - 1] + suffixMax[i] - i);
  50.         }
  51.     }
  52.     cout << ans << endl;
  53.  
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment