跳到正文

个人记录

代码源 Day19 总结

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

文章信息

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

版本

  1. v1当前

A

数学不是 DP。

设 o=⌊log⁡2n⌋o = \lfloor \log_2n\rfloor 输出 n(n+1)2−2o+1+1\frac{n(n+1)}{2}-2^{o+1}+1。

B

贪心不是 DP。

贪心选前缀和后缀。

C++
#include <iostream>

using namespace std;
using ll = long long;

const int kMaxN = 2e5 + 1;

ll t, n, x, a[kMaxN];

int main() {
  for (cin >> t; t; t--) {
    cin >> n >> x;
    ll ans = -1, s = 0;
    for (ll i = 1; i <= n; i++) {
      cin >> a[i], s += a[i];
    }
    ll sm = 0;
    for (ll i = 1; i <= n; i++) {
      sm += a[i];
      if ((s - sm) % x != 0) {
        ans = n - i;
        break;
      }
    }
    sm = 0;
    for (ll i = n; i >= 1; i--) {
      sm += a[i];
      if ((s - sm) % x != 0) {
        ans = max(ans, i - 1);
        break;
      }
    }
    cout << (s % x != 0 ? n : ans) << '\n';
  }
  return 0;
}

## C

DP题,转移比较复杂。

```cpp
#include<bits/stdc++.h>

using namespace std;

const int kMaxN = 101;

int n, a[kMaxN], dp[kMaxN][3];

int main() {
	memset(dp, 0x3f, sizeof(dp));
	cin >> n;
	for(int i = 1; i <= n; i++) {
    cin >> a[i];
  }
	dp[0][0] = dp[0][1] = dp[0][2] = 0;
	for(int i = 1; i <= n; i++) {
		dp[i][0] = min(min(dp[i][0], dp[i - 1][0]), min(dp[i - 1][1], dp[i - 1][2])) + 1;
		if (a[i] == 1 || a[i] == 3) {
			dp[i][1] = min(dp[i-1][0],dp[i - 1][2]);
		}
		if (a[i] == 2 || a[i] == 3) {
			dp[i][2] = min(dp[i-1][0], dp[i - 1][1]);
		}
	}
	cout << min(dp[n][0], min(dp[n][1], dp[n][2]));
  return 0;
}

D

树形 DP,一遍过。

C++
#include <iostream>
#include <vector>

using namespace std;

const int kMaxN = 1e5 + 1;

long long n, m, ans, vis[kMaxN], d[kMaxN], in[kMaxN], l[kMaxN];
vector<int> g[kMaxN];

void D(int x) {
  vis[x] = 1;
  for (int i : g[x]) {
    l[i] = max(l[i], l[x] + 1);
    in[i]--;
    if (in[i] == 0) {
      D(i);
    }
  }
}

int main() {
  cin >> n >> m;
  for (int i = 1, u, v; i <= m; i++) {
    cin >> u >> v;
    g[min(u, v)].push_back(max(u, v));
    d[u]++, d[v]++;
    in[max(u, v)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      l[i] = 1;
      D(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, l[i] * d[i]);
  }
  cout << ans;
  return 0;
}

E

写了题解:)

错因就是少情况。

C++
#include <iostream>
#include <unordered_map>

using namespace std;
using ll = long long;

const int kMaxN = 2e5 + 1;

ll n, k, a[kMaxN];
unordered_map<ll, ll> mp[2];

int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[1][a[i]]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    mp[1][a[i]]--;
    if (a[i] % k == 0) {
      ans += mp[0][a[i] / k] * mp[1][a[i] * k];
    }
    mp[0][a[i]]++;
  }
  cout << ans << '\n';
  return 0;
}

F

树形01背包,一遍过。

C++
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

const ll kMaxN = 2e5 + 1;

ll n, ans, siz[kMaxN];
vector<ll> g[kMaxN];
bool f[kMaxN];

void D(ll x) {
  siz[x] = 1;
  for (ll i : g[x]) {
    D(i);
    siz[x] += siz[i];
  }
  memset(f, 0, sizeof f);
  f[0] = 1;
  ll sm = 0;
  for (ll i : g[x]) {
    sm += siz[i];
    for (ll j = sm; j >= siz[i]; j--) {
      f[j] |= f[j - siz[i]];
    }
  }
  ll maxn = 0;
  for (ll i = 0; i <= sm; i++) {
    if (f[i]) {
      maxn = max(maxn, 1ll * i * (sm - i));
    }
  }
  ans += maxn;
}

int main() {
  cin >> n;
  for (ll i = 2, x; i <= n; i++) {
    cin >> x, g[x].push_back(i);
  }
  D(1);
  cout << ans << '\n';
  return 0;
}

G

找规律然后分治。

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

using namespace std;
using ll = long long;

const int kMaxN = 2e5 + 1;

ll t, n, x, a[kMaxN];

ll Xor(int l, int r) { return a[r] ^ a[l - 1]; }

ll D(ll x) {
  if (x <= n) {
    return Xor(x, x);
  }
  if (x / 2 <= n) {
    return Xor(1, x / 2);
  }
  ll ans = Xor(1, n);
  if (((x / 2) & 1) == 0) {
    ans ^= D(x / 2);
  }
  if ((n & 1) == 0) {
    ans ^= D(n + 1);
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  for (cin >> t; t; t--) {
    cin >> n >> x >> x;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
      a[i] ^= a[i - 1];
    }
    cout << D(x) << '\n';
  }
  return 0;
}
原文