跳到正文

个人记录

20250714 总结

_little_duck_约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟
获赞
1

版本

  1. v1当前

D.洛谷 P9588

题意

给定你几个修改查询操作,请你维护数据

思路

  • 用数组模拟队列
  • 将插入的数组当作区间
  • 用 multisetmultiset 维护最大值

易错点

  • 范围处理
  • multisetmultiset 非空辨别

代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 10;

int c, q, op, k, cnt;
multiset<int> st;
int b[N], p, a[N];
int hd = 1, tl, nt = 1;

signed main(){
  cin >> c >> q;
  while(q--){
    cin >> op;
    if(op == 1){
      cin >> k;
      tl += k;
      cnt++;
      st.insert(k);
      a[p + 1] = k;
      b[p + 1] = b[p] + k;
      p++;
    }
    if(op == 2){
      cin >> k;
      for(; nt <= p && hd + k - 1 >= b[nt]; nt++){
        st.erase(st.find(a[nt]));
      }
      hd += k;
    }
    if(op == 3){
      cin >> k;
      int l = nt, r = cnt, target = hd + k - 1;
      while(l <= r){
        int mid = l + r >> 1;
        if(b[mid] < target){
          l = mid + 1;
        }
        else{
          r = mid - 1;
        }
      }
      cout << target - b[r] << "\n";
    }
    if(op == 4){
      if(st.empty()){
        cout << "0\n";
      }
      else{
        cout << *st.rbegin() << "\n";
      }
    }
  }
  return 0;
}

E.洛谷 P3069

题意

从一段数字中选出一串连续的数字,使其最多有 k+1k+1 种数字,统计串中出现的数字的最大数量

思路

  • 双指针维护区间
  • 用 mapmap 维护出现次数和种类

易错点

  • 双指针边界

代码

C++
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int n, k, a[N], cnt, mx;
map<int, int> m;

int main(){
  cin >> n >> k;
  for(int i = 1; i <= n; i++){
    cin >> a[i];
  }
  for(int l = 1, r = 1; l <= n && r <= n; l++){
    while(r <= n && m.size() <= k + 1){
      m[a[r]]++;
      mx = max(mx, m[a[r]]);
      r++;
    }
    m[a[l]]--;
    if(!m[a[l]]){
      m.erase(a[l]);
    }
  }
  cout << mx;
  return 0;
}

F.洛谷 P3917

题意

统计异或和

思路

  • 二进制拆解
  • 二进制前缀和
  • 进制转换

易错点

  • 空间
  • 二进制方向

代码

C++
#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
long long n, l[N][100], ans;
long long cnt[N][5], b[N];

void f(long long x, int p){
  string s = "";
  while(x){
    s += (x % 2 + '0');
    x /= 2;
  }
  for(int i = 0; i < s.size(); i++){
    l[p][i] = (s[i] - '0');
  }
}

int main(){
  cin >> n;
  for(int i = 1, x; i <= n; i++){
    cin >> x;
    f(x, i);
  }
  for(int i = 0; i <= 32; i++){
    cnt[0][0] = 1;
    for(int j = 1; j <= n; j++){
      b[j] = b[j - 1] ^ l[j][i];
      cnt[j][0] = cnt[j - 1][0];
      cnt[j][1] = cnt[j - 1][1];
      cnt[j][0] += b[j] == 0;
      cnt[j][1] += b[j] == 1;
      ans += cnt[j][!b[j]] * (1 << i);
    }
  }
  cout << ans;
  return 0;
}

G. AtCoder abc389_e

题意

用 mm 元买 nn 种商品,最多买几样

思路

  • 二分价格
  • 数学推导确定单价、数量

易错点

  • 数据范围要开 __int28
  • 要快读快写
  • 数学推导要严谨

代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int __int128

const int N = 2e5 + 10;
int n, m, a[N], ans;

int read(){
  int k = 0, f = 1;
  char c = getchar();
  while(c < '0' || c > '9'){
    if(c == '-'){
      f = -1;
    }
    c = getchar();
  }
  while(c >= '0' && c <= '9'){
    k = k * 10 + c - '0';
    c = getchar();
  }
  return k*f;
}

void out(int x){
    if(x < 0){
      putchar('-');
      x =- x;
    }
    if(x < 10){
      putchar(x + '0');
    }
    else{
      out(x / 10);
      putchar(x % 10 + '0');
    }
}

bool f(int x){
  int sum = 0, ret = m;
  for(int i = 1; i <= n; i++){
    int k = (x / a[i] + 1) / 2;
    if(k * k * a[i] > ret){
      return 0;
    }
    sum += k;
    ret -= k * k * a[i];
  }
  for(int i = 1; i <= n && ret - x >= 1; i++){
    int k = (x / a[i] + 1) / 2;
    if((k * 2 + 1) * a[i] == x + 1){
      sum++;
      ret -= x + 1;
    }
  }
  ans = max(ans, sum);
  return 1;
}

signed main(){
  n = read();
  m = read();
  for(int i = 1; i <= n; i++){
    a[i] = read();
  }
  int l = 0, r = m;
  while(l <= r){
    int mid = (l + r) >> 1;
    if(f(mid)){
      l = mid + 1;
    }
    else{
      r = mid - 1;
    }
  }
  out(ans);
  return 0;
}
原文