Skip to content

Commit fda0f9f

Browse files
committed
HDOJ: 2024“钉耙编程”中国大学生算法设计超级联赛(4)
1 parent 28a7b53 commit fda0f9f

File tree

6 files changed

+340
-3
lines changed

6 files changed

+340
-3
lines changed

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"'${fileBasename}'",
1313
"-o",
1414
"'${fileBasenameNoExtension}.exe'",
15-
"-std=c++17",
15+
"-std=c++20",
1616
"-Wall",
1717
"-Wextra",
1818
"-O2",
@@ -72,7 +72,7 @@
7272
"-o",
7373
"'${fileBasenameNoExtension}.exe'",
7474
"-glldb",
75-
"-std=c++17",
75+
"-std=c++20",
7676
"-Wall",
7777
"-Wextra",
7878
"-O2",

HDOJ/7471.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @file 7471.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-07-29
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 200005
14+
15+
typedef pair<int, int> pii;
16+
17+
vector<int> prime;
18+
bool notPrime[maxn];
19+
20+
int a[maxn], f[maxn];
21+
int64_t sum[maxn];
22+
int n, k;
23+
24+
int read() {
25+
char ch = getchar();
26+
int res = 0, f = +1;
27+
while (ch < '0' || ch > '9') {
28+
if (ch == '-') f = -f;
29+
ch = getchar();
30+
}
31+
while (ch >= '0' && ch <= '9') {
32+
res = res * 10 + ch - '0';
33+
ch = getchar();
34+
}
35+
return res * f;
36+
}
37+
38+
bool check(int64_t lim) {
39+
set<pii> S;
40+
for (int r = 1, cnt = 0; r <= n; r++) {
41+
S.emplace(sum[r - 1], r);
42+
for (auto [v, l] : S) {
43+
if (sum[r] - v < lim) break;
44+
if (notPrime[r - l + 1]) continue;
45+
cnt++, S.clear();
46+
break;
47+
}
48+
if (cnt == k) return true;
49+
}
50+
return false;
51+
}
52+
53+
void solve(void) {
54+
n = read(), k = read();
55+
for (int i = 1; i <= n; i++) a[i] = read(), sum[i] = sum[i - 1] + a[i];
56+
57+
if (2 * k > n) return cout << "impossible" << endl, void();
58+
59+
int64_t l = -1e6, r = +1e6;
60+
while (l + 1 < r) {
61+
int64_t mid = (l + r) >> 1;
62+
(check(mid) ? l : r) = mid;
63+
}
64+
65+
cout << l << endl;
66+
67+
return;
68+
}
69+
70+
int main() {
71+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
72+
73+
notPrime[1] = true;
74+
for (int i = 2; i < maxn; i++) {
75+
if (!notPrime[i]) prime.push_back(i);
76+
for (auto j = prime.begin(); j != prime.end() && i * *j < maxn; j++) {
77+
notPrime[i * *j] = true;
78+
if (i % *j == 0) break;
79+
}
80+
}
81+
82+
int _ = read();
83+
while (_--) solve();
84+
85+
return 0;
86+
}

HDOJ/7472.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @file 7472.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-07-29
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
typedef pair<int, int> pii;
14+
15+
int read() {
16+
char ch = getchar();
17+
int res = 0, f = +1;
18+
while (ch < '0' || ch > '9') {
19+
if (ch == '-') f = -f;
20+
ch = getchar();
21+
}
22+
while (ch >= '0' && ch <= '9') {
23+
res = res * 10 + ch - '0';
24+
ch = getchar();
25+
}
26+
return res * f;
27+
}
28+
29+
int a[20], w[1 << 20], f[1 << 20], xorS[1 << 20], ans, n2, m2;
30+
bool vis[1 << 20];
31+
vector<pii> rec;
32+
33+
void check(int S, int T, int val) {
34+
f[S | T] = min(f[S | T], w[xorS[T]]);
35+
ans = min(ans, max(f[S | T], f[(n2 - 1) ^ (S | T)]) - val);
36+
return;
37+
};
38+
39+
void solve(void) {
40+
int n = read(), m = read();
41+
n2 = 1 << n, m2 = 1 << m;
42+
for (int i = 0; i < n; i++) a[i] = read();
43+
for (int i = 0; i < m2; i++) w[i] = read();
44+
for (int i = 0; i < n2; i++) vis[i] = false, f[i] = INT_MAX;
45+
for (int S = 0; S < n2; S++) {
46+
xorS[S] = 0;
47+
for (int j = 0; j < n; j++)
48+
if (S >> j & 1) xorS[S] ^= a[j];
49+
}
50+
51+
rec.clear();
52+
for (int S = 0; S < n2; S++) rec.emplace_back(w[xorS[S]], S);
53+
sort(rec.begin(), rec.end(), greater<pii>());
54+
55+
ans = INT_MAX;
56+
for (auto [val, S] : rec) {
57+
vis[S] = true;
58+
for (int fT = S ^ (n2 - 1), T = fT; T; T = (T - 1) & fT)
59+
if (vis[T]) check(S, T, val);
60+
if (vis[0]) check(S, 0, val);
61+
}
62+
63+
cout << ans << endl;
64+
65+
return;
66+
}
67+
68+
int main() {
69+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
70+
71+
int _ = read();
72+
while (_--) solve();
73+
74+
return 0;
75+
}

HDOJ/7474.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @file 7474.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-07-29
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define mod 1000000007
14+
#define maxn 52
15+
16+
bool mem1;
17+
18+
const int way[2][4] = {{0, 0, +1, -1}, {+1, -1, 0, 0}};
19+
20+
int f[maxn][maxn][maxn][11][11][6], g[maxn][maxn][maxn];
21+
string a[maxn];
22+
23+
bool mem2;
24+
25+
void solve(void) {
26+
int n, m, k, hp;
27+
cin >> n >> m >> k >> hp;
28+
29+
a[0] = a[n + 1] = string(n + 2, '#');
30+
for (int i = 1; i <= n; i++) cin >> a[i], a[i] = '#' + a[i] + '#';
31+
32+
int sx, sy, tx, ty;
33+
for (int i = 1; i <= n; i++)
34+
for (int j = 1; j <= n; j++)
35+
if (a[i][j] == 'P')
36+
sx = i, sy = j, a[i][j] = '.';
37+
else if (a[i][j] == 'E')
38+
tx = i, ty = j, a[i][j] = '.';
39+
40+
for (int t = 0; t <= m - k; t++)
41+
for (int i = 1; i <= n; i++)
42+
for (int j = 1; j <= n; j++)
43+
for (int dx = 0; dx <= 2 * hp; dx++)
44+
for (int dy = 0; dy <= 2 * hp; dy++)
45+
for (int vhp = 0; vhp <= hp; vhp++) f[t][i][j][dx][dy][vhp] = 0;
46+
47+
for (int t = 0; t <= k; t++)
48+
for (int i = 0; i <= n + 1; i++)
49+
for (int j = 0; j <= n + 1; j++) g[t][i][j] = 0;
50+
for (int i = 1; i <= n; i++)
51+
for (int j = 1; j <= n; j++)
52+
if (a[i][j] == '.') g[0][i][j] = 1;
53+
for (int t = 1; t <= k; t++)
54+
for (int i = 1; i <= n; i++)
55+
for (int j = 1; j <= n; j++)
56+
if (a[i][j] == '.')
57+
g[t][i][j] =
58+
((int64_t)g[t - 1][i - 1][j] + g[t - 1][i + 1][j] + g[t - 1][i][j - 1] + g[t - 1][i][j + 1]) % mod;
59+
60+
f[0][sx][sy][hp][hp][hp] = 1;
61+
62+
int64_t ans = 0;
63+
for (int t = 0; t < m - k; t++)
64+
for (int i = 1; i <= n; i++)
65+
for (int j = 1; j <= n; j++)
66+
for (int dx = 0; dx <= 2 * hp; dx++)
67+
for (int dy = 0; dy <= 2 * hp; dy++)
68+
for (int vhp = 1; vhp <= hp; vhp++) {
69+
if (!f[t][i][j][dx][dy][vhp]) continue;
70+
int ox = i - sx + tx + (dx - hp), oy = j - sy + ty + (dy - hp);
71+
for (int x = 0; x < 4; x++) {
72+
int Px = i + way[0][x], Py = j + way[1][x], Ex = ox + way[0][x], Ey = oy + way[1][x];
73+
int Ehp = vhp, Edx = dx, Edy = dy;
74+
if (a[Px][Py] == '#') continue;
75+
if (a[Ex][Ey] == '#')
76+
Ex -= way[0][x], Ey -= way[1][x], Ehp--, Edx -= way[0][x], Edy -= way[1][x];
77+
if (Ehp == 0) {
78+
ans = (ans + (int64_t)f[t][i][j][dx][dy][vhp] * g[k][Px][Py]) % mod;
79+
continue;
80+
}
81+
f[t + 1][Px][Py][Edx][Edy][Ehp] =
82+
(f[t + 1][Px][Py][Edx][Edy][Ehp] + f[t][i][j][dx][dy][vhp]) % mod;
83+
}
84+
}
85+
86+
cout << ans << endl;
87+
88+
return;
89+
}
90+
91+
int main() {
92+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
93+
94+
int _;
95+
cin >> _;
96+
while (_--) solve();
97+
98+
return 0;
99+
}

HDOJ/7477.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @file 7477.cpp
3+
* @author Macesuted ([email protected])
4+
* @date 2024-07-29
5+
*
6+
* @copyright Copyright (c) 2024
7+
*
8+
*/
9+
10+
#include <bits/stdc++.h>
11+
using namespace std;
12+
13+
#define maxn 1000005
14+
15+
string S;
16+
17+
int nxt[maxn][40], pre[maxn][40];
18+
int f[maxn];
19+
20+
int code(char c) { return '0' <= c && c <= '9' ? c - '0' : c - 'a' + 10; }
21+
22+
void solve(void) {
23+
int n, m;
24+
cin >> n >> m;
25+
cin >> S, S = ' ' + S;
26+
for (int i = 1; i <= m; i++) f[i] = 0;
27+
28+
for (int j = 0; j < 40; j++) nxt[m][j] = nxt[m + 1][j] = m + 1;
29+
for (int i = m - 1; ~i; i--) {
30+
for (int j = 0; j < 40; j++) nxt[i][j] = nxt[i + 1][j];
31+
nxt[i][code(S[i + 1])] = i + 1;
32+
}
33+
34+
for (int j = 0; j < 40; j++) pre[1][j] = pre[0][j] = 0;
35+
for (int i = 2; i <= m + 1; i++) {
36+
for (int j = 0; j < 40; j++) pre[i][j] = pre[i - 1][j];
37+
pre[i][code(S[i - 1])] = i - 1;
38+
}
39+
40+
for (int i = 1; i <= 12; i++) {
41+
int day = 30;
42+
if (i == 2) day = 29;
43+
if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) day = 31;
44+
for (int j = 1; j <= day; j++) {
45+
string s = "0000";
46+
int mon = i, day = j;
47+
s[1] = mon % 10 + '0', s[0] = mon / 10 % 10 + '0';
48+
s[3] = day % 10 + '0', s[2] = day / 10 + '0';
49+
int p = m + 1;
50+
for (int t = 3; ~t; t--) p = pre[p][code(s[t])];
51+
f[p]++;
52+
}
53+
}
54+
55+
for (int i = m - 1; i; i--) f[i] += f[i + 1];
56+
57+
int64_t ans = 0;
58+
while (n--) {
59+
string s;
60+
cin >> s;
61+
int p = 0;
62+
for (char c : s) p = nxt[p][code(c)];
63+
if (p < m) ans += f[p + 1];
64+
}
65+
cout << ans << endl;
66+
return;
67+
}
68+
69+
int main() {
70+
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
71+
72+
int _;
73+
cin >> _;
74+
while (_--) solve();
75+
76+
return 0;
77+
}

compile_flags.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-xc++
2-
-std=c++17
2+
-std=c++20
33
-Wall
44
-Wextra
55
-IE:/Code/libs/testlib/

0 commit comments

Comments
 (0)