1001-Link with Bracket Sequence II
区间 d p dp dp 分类讨论,具体分类见代码。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 550;
const int mod = 1e9 + 7;
int dp[N][N];
int dp2[N][N];
int a[N];
void solve() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int len = 2; len <= n; len += 2) {
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
dp[i][j] = 0;
//两端匹配
if (a[i] > 0 && (a[i] + a[j]) == 0) {
if (len == 2)
dp[i][j] += 1;
else
dp[i][j] += dp2[i + 1][j - 1];
}
//两端缺失
else if (a[i] == 0 && a[j] == 0) {
if (len == 2)
dp[i][j] += m;
else
dp[i][j] += m * dp2[i + 1][j - 1];
}
//两端一端缺失
else if ((a[i] == 0 && a[j] < 0) || (a[j] == 0 && a[i] > 0)) {
if (len == 2)
dp[i][j] += 1;
else
dp[i][j] += dp2[i + 1][j - 1];
}
dp[i][j] %= mod;
}
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1;
dp2[i][j] = dp[i][j];
for (int k = i + 1; k < j; k += 2) {
dp2[i][j] += dp2[i][k] * dp[k + 1][j];
dp2[i][j] %= mod;
}
}
}
cout << dp2[1][n] << endl;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T = 1;
cin >> T;
for (int i = 1; i <= T; i++) {
solve();
}
}
1007-Climb Stairs
由于要求打完所有怪兽,因此即使跳到后几位也需要通过连续下楼操作将之前跳过的依次取完,考虑采用后缀和,并通过二分答案找到能够击败n+1层怪兽且距当前位最近的应该跳到的层数。若该距离超过 k k k 则不可行。
#include <bits/stdc++.h>
#define int long long
#define endl "\n"
#define PII pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int sum[N];
int now;
bool check(int mid, int pos) {
if (sum[pos + 1] - sum[mid + 1] + now >= a[pos])
return 0;
else
return 1;
}
void solve() {
int n, k;
cin >> n >> now >> k;
for (int i = 1; i <= n + 10; i++)
sum[i] = 0;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = n; i >= 1; i--) //后缀
sum[i] = sum[i + 1] + a[i];
int maxx = 0, pre = 1;
for (int i = 1; i <= n; i++) {
int l = i + 1, r = n + 1;
while (l < r) {
int mid = l + r >> 1;
if (check(mid, i))
l = mid + 1;
else
r = mid;
}
// cout << l << endl;
if (now >= a[i])
l = i;
if (l >= min(pre + k, n + 1)) { //大于k
cout << "NO" << endl;
return;
}
maxx = max(maxx, l);
if (l >= maxx && l == i) {
now += sum[pre] - sum[maxx + 1];
pre = i + 1;
maxx = 0;
}
}
cout << "YES" << endl;
}
signed main() {
int T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
1011-Link is as bear
将会对异或和产生负贡献的数字操作成 0 0 0 后即可转化为线性基板子题
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int p[60], t, n, x, ans;
inline void insert(int x) {
for (int i = 55; i + 1; i--) {
if (!(x >> i))
continue;
if (!p[i]) {
p[i] = x;
break;
}
x ^= p[i];
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
memset(p, 0, sizeof p);
cin >> n;
while (n--) {
cin >> x;
insert(x);
}
for (int i = 55; i >= 0; i--)
if ((ans ^ p[i]) > ans)
ans ^= p[i];
cout << ans << endl;
ans = 0;
}
}