第一道 暴力
// 暴力
#include
using namespace std;
int main() {
int T, L, n;
cin >> T;
while (T--) {
cin >> L >> n;
vector left(n);
vector right(n);
for (int i = 0; i
cin >> left[i];
cin >> right[i];
}
vector res(L + 1, 0);
for (int i = 0; i
for (int j = left[i]; j <= right[i]; ++j) {
++res[j];
}
}
for (int i = 1; i <= L; ++i) {
cout <
}
cout <
}
}
第二道 双指针
// 双指针
#include
using namespace std;
typedef pair PAIR;
bool cmp(const PAIR& p1, const PAIR& p2) {
return p1.first
}
int main() {
int T, n, m;
cin >> T;
while (T--) {
cin >> n >> m;
vector a(n);
vector b(m);
int t;
for (int i = 0; i
cin >> t;
a[i] = {t, i};
}
for (int i = 0; i
cin >> t;
b[i] = {t, i};
}
sort(a.begin(), a.end(), cmp);
sort(b.begin(), b.end(), cmp);
vector res(n, -1);
int i = 0, j = 0;
while (i
while (j b[j].first)
++j;
if (j == m)
break;
res[a[i].second] = ++b[j].second;
++i;
++j;
}
for (int i = 0; i
cout <
}
cout <
}
}
第三道 dfs + 状态保存(可以替换成动态规划)
#include
using namespace std;
const int mod = 1e9+7;
// n: 台阶总数
// m: 单步跨越最多m级台阶
int n, m;
int dfs(int pre2, int pre1, int r, vector>>& dp) {
if (r == 0)
return 1;
if (r
return 0;
if (dp[pre2][pre1][r] != -1)
return dp[pre2][pre1][r];
int res = 0;
for (int i = 1; i <= min(m, r); ++i) {
if (i != pre1 && i != pre2) {
res = (res + dfs(pre1, i, r - i, dp)) % mod;
}
}
return dp[pre2][pre1][r] = res;
}
int main() {
cin >> n >> m;
vector>> dp(m + 1, vector>(m + 1, vector(n + 1, -1)));
cout <
return 0;
}