8月29日总结
T661376 母亲的牛奶
-
时间复杂度:
O(ABC) -
正确思路:
dfs直接搜索
-
错误原因:
考过的知识没有进行巩固复习
-
正确代码:
#include <bits/stdc++.h> using namespace std;
const int N = 25, M = 1005; int A, B, C, cnt, ans[M]; int vis[N][N][N], st[M];
void dfs(int a, int b, int c) { if (vis[a][b][c]) { return; } vis[a][b][c] = 1;
if (a == 0 && !st[c]) {
st[c] = 1;
ans[++cnt] = c;
}
// 从a倒向b
if (a > 0 && b < B) {
int pour = min(a, B - b);
dfs(a - pour, b + pour, c);
}
// 从a倒向c
if (a > 0 && c < C) {
int pour = min(a, C - c);
dfs(a - pour, b, c + pour);
}
// 从b倒向a
if (b > 0 && a < A) {
int pour = min(b, A - a);
dfs(a + pour, b - pour, c);
}
// 从b倒向c
if (b > 0 && c < C) {
int pour = min(b, C - c);
dfs(a, b - pour, c + pour);
}
// 从c倒向a
if (c > 0 && a < A) {
int pour = min(c, A - a);
dfs(a + pour, b, c - pour);
}
// 从c倒向b
if (c > 0 && b < B) {
int pour = min(c, B - b);
dfs(a, b + pour, c - pour);
}
}
int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> A >> B >> C; memset(vis, 0, sizeof(vis)); memset(st, 0, sizeof(st)); dfs(0, 0, C); sort(ans + 1, ans + cnt + 1); for (int i = 1; i <= cnt; i++) { cout << ans[i] << ' '; } return 0; }
## P1141 01迷宫
1. **时间复杂度**:
$ O(n^2 + m) $
3. **正确思路**:
dfs直接搜索,然后走过了用数组标记一下
6. **正确代码**:
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, ans[100002], x, y, f[1002][1002];
char s[1002][1002];
void dfs(int r, int c, int z, int lll) {
if (r < 0 || r >= n || c < 0 || c >= n || f[r][c] != -1 || s[r][c] - '0' != z) {
return;
}
f[r][c] = lll;
ans[lll]++;
dfs(r - 1, c, !z, lll);
dfs(r + 1, c, !z, lll);
dfs(r, c - 1, !z, lll);
dfs(r, c + 1, !z, lll);
}
int main() {
cin >> n >> m;
for (int i = 0; i < n;i++) {
cin >> s[i];
}
memset(f, -1, sizeof(f));
for (int i = 0; i < m; i++) {
cin >> x >> y;
x--;
y--;
if (f[x][y] == -1) {
dfs(x, y, s[x][y] - '0', i);
} else {
ans[i] = ans[f[x][y]];
}
}
for (int i = 0; i < m; i++) {
cout << ans[i] << endl;
}
return 0;
}
T659298 敲鼓
-
时间复杂度:
O(所有的|p_i| + |s_i|) -
正确思路:
将原始敲击序列 p 和听到的声音序列 s 分别进行分段,得到每个连续相同字符的段,然后逐段对比是否符合扩展规则
-
正确代码:
#include <bits/stdc++.h> using namespace std;
void ans() { string p, s; cin >> p >> s;
if (p.size() > s.size() || p.size() * 2 < s.size() || p[0] != s[0]) {
cout << "No" << endl;
return;
}
vector<pair<char, int>> a;
int cnt = 1;
for (int i = 1; i < p.size(); i++) {
if (p[i] != p[i - 1]) {
a.push_back({p[i - 1], cnt});
cnt = 1;
} else {
cnt++;
}
}
a.push_back( {p[p.size() - 1], cnt} );
vector<pair<char, int>> b;
cnt = 1;
for (int i = 1; i < s.size(); i++) {
if (s[i] != s[i - 1]) {
b.push_back({s[i - 1], cnt});
cnt = 1;
} else {
cnt++;
}
}
b.push_back( {s[s.size() - 1], cnt} );
if (a.size() != b.size()) {
cout << "No" << endl;
return;
}
for (int i = 0; i < a.size(); i++) {
if (a[i].first != b[i].first ||
a[i].second > b[i].second ||
b[i].second > 2 * a[i].second) {
cout << "No" << endl;
return;
}
}
cout << "Yes" << endl;
}
int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { ans(); } return 0; }
## T661712 圆
1. **时间复杂度**:
$ O(n) $
3. **正确思路**:
应为曲线想怎么弯就怎么弯,所以只有一个圆在一个圆的里面另一个在圆外面才会穿过边界,答案就是这种情况的个数,那么如何判断这种情况?可以用异或,因为异或的特点是输入的值不同就位真,否则为假,那么当一个点在圆内,另一个点在圆外,结果成立,否则,结果不成立,最后看圆的两点到圆心距是否小于半径即可。
6. **正确代码**:
```cpp
#include <bits/stdc++.h>
using namespace std;
int x[60], y[60], r[60];
double jl(int x1, int y1, int x2, int y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, x1, y1, x2, y2, ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x[i];
}
for (int i = 1; i <= n; i++) {
cin >> y[i];
}
for (int i = 1; i <= n; i++) {
cin >> r[i];
}
cin >> x1 >> y1 >> x2 >> y2;
for (int i = 1; i <= n; i++) {
if ((jl(x1, y1, x[i], y[i]) < r[i]) ^ (jl(x2, y2, x[i], y[i])) < r[i]) {
ans++;
}
}
cout << ans;
return 0;
}
T661718 Bessie Goes Moo S
-
时间复杂度:
O(7 ^ 7) -
正确思路:
dfs暴力枚举所以方案数,g数组存储每一个变量摸7后的余数,最后统计满足条件的个数即可
-
正确代码:
#include <bits/stdc++.h> using namespace std;
int g[10][10]; int x[10]; long long ans = 0; int v[240][10];
void dfs(int now) {
if (now == 7){
long long sum = 1ll (x[0] + x[1] + x[2] + x[2] + x[3] + x[1]) (x[4] + x[5] + x[1] + x[2]) (x[6] + x[5] 2);
if (sum % 7 == 0) {
long long tmp = 1;
for (int i = 0; i < 7; i++) {
tmp *= g[i][x[i]];
}
ans += tmp;
}
return ;
}
for (int i = 0; i < 7; i++) {
if (!g[now][i]) {
continue;
}
x[now] = i;
dfs(now + 1);
}
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
char c;int l;
cin >> c >> l;
l = (l % 7 + 7) % 7;
if (c == 'B') {
g[0][l]++;
}
if (c == 'E') {
g[1][l]++;
}
if (c == 'S') {
g[2][l]++;
}
if (c == 'I') {
g[3][l]++;
}
if (c == 'G') {
g[4][l]++;
}
if (c == 'O') {
g[5][l]++;
}
if (c == 'M') {
g[6][l]++;
}
}
dfs(0);
cout << ans;
return 0;
}