传送门
作者:hans774882968以及hans774882968
A
水,略。
B
模拟即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define re_(i,a,b) for(int i = (a);i < (b);++i)
#define dwn(i,a,b) for(int i = (a);i >= (b);--i)
const int N = 5 + 5;
int n, a[N];
void dbg() {
puts ("");
}
template<typename T, typename... R>void dbg (const T &f, const R &... r) {
cout << f << " ";
dbg (r...);
}
template<typename Type>inline void read (Type &xx) {
Type f = 1;
char ch;
xx = 0;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar() ) if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar() ) xx = xx * 10 + ch - '0';
xx *= f;
}
void read() {
}
template<typename T, typename ...R>void read (T &x, R &...r) {
read (x);
read (r...);
}
int main() {
read (n);
int ans = 0;
rep (_, 1, n) {
int x;
read (x);
a[0]++;
dwn (i, 3, 0) {
if (i + x < 4) a[i + x] += a[i];
else ans += a[i];
a[i] = 0;
}
}
printf ("%d\n", ans);
return 0;
}
C-枚举
题意:有一个九宫格,每格填一个正整数,输入3 <= h[0~2] <= 30, 3 <= w[0~2] <= 30分别表示期望的每行和每列的数字的和。求方案数。
只需要枚举4个格子就能确定所有的数了,计算量30^4完全可行。我选择的是(0,0), (0,1), (1,0), (1,1)这4个格子。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define re_(i,a,b) for(int i = (a);i < (b);++i)
#define dwn(i,a,b) for(int i = (a);i >= (b);--i)
const int N = 2e5 + 5;
int n, w[5], h[5];
void dbg() {
puts ("");
}
template<typename T, typename... R>void dbg (const T &f, const R &... r) {
cout << f << " ";
dbg (r...);
}
template<typename Type>inline void read (Type &xx) {
Type f = 1;
char ch;
xx = 0;
for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar() ) if (ch == '-') f = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar() ) xx = xx * 10 + ch - '0';
xx *= f;
}
void read() {
}
template<typename T, typename ...R>void read (T &x, R &...r) {
read (x);
read (r...);
}
int main() {
n = 3;
re_ (i, 0, n) read (w[i]);
re_ (i, 0, n) read (h[i]);
int ans = 0;
re_ (i0, 1, min (w[0], h[0]) - 1) {
re_ (i1, 1, min (w[1], h[0]) - 1) {
re_ (i3, 1, min (w[0], h[1]) - 1) {
re_ (i4, 1, min (w[1], h[1]) - 1) {
int i2 = h[0] - i0 - i1, i5 = h[1] - i3 - i4, i6 = w[0] - i0 - i3, i7 = w[1] - i1 - i4;
if (i2 > 0 && i5 > 0 && i6 > 0 && i7 > 0) {
int i81 = h[2] - i6 - i7, i82 = w[2] - i2 - i5;
if (i81 == i82 && i81 > 0) ++ans;

本文详细解析了多种算法模板,包括水题、区间合并、图论建模(函数图)、树状数组、矩阵快速幂优化动态规划、线段树等,并给出了相应的代码实现。通过这些模板,可以解决各种复杂问题,如九宫格填数、区间操作和图论求解等。
最低0.47元/天 解锁文章
276

被折叠的 条评论
为什么被折叠?



