跳到正文

个人记录

0714总结

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

文章信息

发布
2025年7月14日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 8 分钟

版本

  1. v1当前

总结

A.abc100_d

题意

有三个长度为 nn 的序列 a,b,ca, b, c,选出一个长度为 mm 的子序列 x1,x2,...,xmx_1, x_2,...,x_m,求最大 ∣∑i=1maxi∣+∣∑i=1mbxi∣+∣∑i=1mcxi∣\vert \sum\limits_{i=1}^m a_{x_i} \vert + \vert \sum\limits_{i=1}^m b_{x_i} \vert + \vert \sum\limits_{i=1}^m c_{x_i} \vert。

思路

枚举 a,b,ca, b, c 的正负性,随后对其价值进行排序,选出前 mm 大的即可,时间复杂度 O(nlog⁡(n))O(n\log(n))。

代码

C++
#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int MAXN = 1e3 + 1;

int n, m, f1, f2, f3;
ll ans, sum;

struct Node{
  ll x, y, z;
  bool operator<(const Node &i) const{
    return x * f1 + y * f2 + z * f3 > i.x * f1 + i.y * f2 + i.z * f3;
  }
}a[MAXN];

int main(){
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> a[i].x >> a[i].y >> a[i].z;
  }
  for(const auto &i : {-1, 1}){
    for(const auto &j : {-1, 1}){
      for(const auto &k : {-1, 1}){
        f1 = i, f2 = j, f3 = k;
        sort(a + 1, a + 1 + n);
        for(int p = 1; p <= m; p++){
          sum += f1 * a[p].x + f2 * a[p].y + f3 * a[p].z;
        }
        ans = max(ans, sum);
        sum = 0;
      }
    }
  }
  cout << ans;
  return 0;
}

B.洛谷P3456

题意

山峰、山谷的定义分别为周围方格都高于、低于其方格的连通块,给定一个 n×nn \times n 大小的网格图,求其山峰、山谷的数量。

思路

直接寻找连通块,随后枚举判断即可,由于每个方格只遍历一次,所以时间复杂度 O(n2)O(n^2)。

代码

C++
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e3 + 1;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};

int n, ans1, ans2, w[MAXN][MAXN];
bool f[MAXN][MAXN];

vector<pair<int, int>> vt;

inline void dfs(int x, int y, int v){
  if(x < 1 || x > n || y < 1 || y > n || f[x][y] || w[x][y] != v){
    return;
  }
  f[x][y] = 1;
  vt.push_back({x, y});
  for(int i = 0; i < 8; i++){
    dfs(x + dx[i], y + dy[i], v);
  }
}

int main(){
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n;
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      cin >> w[i][j];
    }
  }
  for(int i = 1; i <= n; i++){
    for(int j = 1; j <= n; j++){
      if(!f[i][j]){
        dfs(i, j, w[i][j]);
        bool f1 = 1, f2 = 1;
        for(const auto &p : vt){
          int x = p.first, y = p.second;
          for(int dir = 0; dir < 8; dir++){
            int nx = x + dx[dir], ny = y + dy[dir];
            if(nx >= 1 && nx <= n && ny >= 1 && ny <= n){
              f1 &= (w[nx][ny] <= w[x][y]);
              f2 &= (w[nx][ny] >= w[x][y]);
            }
          }
        }
        ans1 += f1, ans2 += f2;
        vt.clear();
      }
    }
  }
  cout << ans1 << ' ' << ans2;
  return 0;
}

C.洛谷P9588

题意

题目上讲得很清楚。

思路

考虑到数据范围,不能直接按题意模拟。由于每个插入的序列格式都是固定的,所以我们只需要记录其最大值即可。同时,需要记录每个序列的长度以便进行删除和查询操作。由于用到 set,经过分析可得时间复杂度为 O(qlog⁡(q))O(q\log(q))。

代码

C++
#include <bits/stdc++.h>
#define int long long

using namespace std;

const int MAXQ = 2e5 + 1;

int c, q, op, x, y, z, cnt, tot, pos = 1, v[MAXQ], sz[MAXQ];

multiset<int> s;

inline void Solve(){
  cin >> op;
  if(op == 1){
    cin >> x;
    v[++tot] = x;
    sz[tot] = sz[tot - 1] + x;
    s.insert(x);
  }else if(op == 2){
    cin >> y;
    cnt += y;
    while(pos <= tot && cnt >= sz[pos]){
      s.erase(s.find(v[pos]));
      pos++;
    }
  }else if(op == 3){
    cin >> z;
    z += cnt;
    int p = lower_bound(sz + 1, sz + 1 + tot, z) - sz - 1;
    cout << z - sz[p] << '\n';
  }else{
    cout << *s.rbegin() << '\n';
  }
}

signed main(){
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> c >> q;
  while(q--){
    Solve();
  }
  return 0;
}

D.洛谷P3069

题意

给定一个数组,相同的数为同一种类,删除 kk 个种类的数,求最大相同连续段的长度。

思路

考虑枚举包含 k+1k + 1 个种类的数区间,我们发现:随着左端点的移动,右端点需不断向右扩张才能满足区间内存在 k+1k + 1 个种类的数。于是考虑双指针,由于是求最大值,所以用 map 记录,直接在右指针扩张时求解即可。由于每个点只会被遍历一次,故时间复杂度 O(n)O(n)。

代码

C++
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5 + 1;

int n, k, ans, a[MAXN];

map<int, int> m;

int main(){
  ios::sync_with_stdio(0), cin.tie(0);
  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]]++;
      ans = max(ans, m[a[r]]);
      r++;
    }
    m[a[l]]--;
    if(!m[a[l]]){
      m.erase(a[l]);
    }
  }
  cout << ans;
  return 0;
}
原文