LOJ6502. 「雅礼集训 2018 Day4」Divide（构造＋dp）

题目链接

https://loj.ac/problem/6502

代码

#include<bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef unsigned long long ull;

template<typename T> inline void read(T& x) {
char c = getchar();
bool f = false;
for (x = 0; !isdigit(c); c = getchar()) {
if (c == '-') {
f = true;
}
}
for (; isdigit(c); c = getchar()) {
x = x * 10 + c - '0';
}
if (f) {
x = -x;
}
}

template<typename T> inline bool checkMax(T& a, const T& b) {
return a < b ? a = b, true : false;
}

template<typename T> inline bool checkMin(T& a, const T& b) {
return a > b ? a = b, true : false;
}

const int N = 2e3 + 10, mod = 1e9 + 7;

inline void add(int& x, int y) {
x = (x + y) % mod;
}

int n, m, a[N], all[N], f[N][N], g[N][N];

int main() {
for (register int i = 1; i <= n; ++i) {
}
sort(a + 1, a + 1 + n);
int l = 1, r = n;
for (register int i = n; i; --i) {
if (a[l] + a[r] >= m) {
all[i] = 1, --r;
} else {
all[i] = 0, ++l;
}
}
int ans = 0;
g[0][0] = 1;
for (register int i = 1; i <= n; ++i) {
for (register int j = 0; j <= i; ++j) {
if (j ^ i) {
int v = f[i - 1][j] + (all[i] ? j : 0);
if (v > f[i][j]) {
f[i][j] = v, g[i][j] = g[i - 1][j];
} else if (v == f[i][j]) {
}
} if (j) {
int v = f[i - 1][j - 1] + (all[i] ? i - j : 0);
if (v > f[i][j]) {
f[i][j] = v, g[i][j] = g[i - 1][j - 1];
} else if (v == f[i][j]) {
add(g[i][j], g[i - 1][j - 1]);
}
}
checkMax(ans, f[i][j]);
}
}
int res = 0;
for (register int i = 0; i <= n; ++i) {
if (f[n][i] == ans) {