A 密码锁
暴力即可。枚举每一种可能的正确密码,再枚举如何拨动,如果能够产生全部的 个状态就计入答案。
判断两种状态是否相同,既可以 扫一遍,也可以用哈希。我考场代码用的是哈希,运算次数约 ,可以通过。如果不用哈希的话可能会被卡,但应该还是能过的(因为有 O2)。
考场代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 8, maxm = 5, maxk = 1e6;
const int N = maxn + 10, M = maxm + 10, K = maxk + 10;
int n, m, ans, g[M], a[N][M], c[N], b[M], d[K];
void dfs(int x, int y) {
if (x > 5) {
int res = 0;
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 9; ++j) {
res += d[y - b[i] * g[i] + ((b[i] + j) % 10) * g[i]];
}
}
for (int i = 1; i <= 4; ++i) {
for (int j = 1; j <= 9; ++j) {
res += d[y - b[i] * g[i] + ((b[i] + j) % 10) * g[i] - b[i + 1] * g[i + 1] + ((b[i + 1] + j) % 10) * g[i + 1]];
}
}
if (res == n) {
++ans;
}
return;
}
for (int i = 0; i <= 9; ++i) {
b[x] = i;
dfs(x + 1, y + i * g[x]);
}
}
int rd() {
char ch = getchar();
int s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') {
w = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = (s << 3) + (s << 1) + (ch ^ 48);
ch = getchar();
}
return (s * w);
}
void print(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x <= 9) {
putchar(x + 48);
return;
}
print(x / 10);
putchar(x % 10 + 48);
}
int main() {
freopen("lock.in", "r", stdin);
freopen("lock.out", "w", stdout);
n = rd();
g[1] = 1;
for (int i = 2; i <= 5; ++i) {
g[i] = g[i - 1] * 10;
}
for (int i = 1; i <= n; ++i) {
c[i] = 0;
for (int j = 1; j <= 5; ++j) {
a[i][j] = rd();
c[i] += a[i][j] * g[j];
}
++d[c[i]];
}
ans = 0;
dfs(1, 0);
printf("%d", ans);
return 0;
}B 消消乐
这道题有点玄学……
说明:为方便叙述,一个区间 是合法的当且仅当这个区间是可消除的。
区间 dp。
设 表示区间 是否能被消除。
有转移式:
考虑固定 ,让 从左往右扫一遍。
可以维护一个栈,当当前字符 与栈顶字符相等时,将栈顶字符弹出(也就是让 和栈顶字符消掉);否则将 插入栈。
当栈为空时, 即为一个合法的区间,计入答案即可。
时间复杂度 。
代码详见下文 中的 。
我的考场做法。
考虑优化 的暴力。
考虑设
用人话来说就是以 为左端点,往右做 的暴力的过程,栈第一次被清空时的右端点(如栈无法被清空则设为 )。
并设
用人话说就是有多少个以 为左端点的区间是可被消除的。
从右往左枚举 。考虑如何快速地求出 。
当 时,显然区间 是合法的,我们可以将它消掉,因此此时 。而 ,因为除了 中的点可以满足 的要求以外,还有 也可以满足 的要求。
如果 ,考虑先消掉 这一段区间。
此时如果 ,则可以仿照 时的做法更新 。显然此时 。
否则的话可以消掉区间 并继续推下去,直到找到第一个和 相等的字符并更新。如果找不到的话,则 。
答案即为 。
时间复杂度:。
证明我不会,这玩意非常的玄学,不太好证时间复杂度但是又很难 hack 掉,具体的是要证每个 和每个字符 ,位置 至多只会被一个 的 在做左端点时跳到。
贴一下我的考场代码。其中 Sub1 是 暴力, Sub2 是 这种做法。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e6, maxm = 26;
const ll N = maxn + 10, M = maxm + 1;
ll n, m, ans;
string s;
ll rd() {
char ch = getchar();
ll s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') {
w = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = (s << 3) + (s << 1) + (ch ^ 48);
ch = getchar();
}
return (s * w);
}
void rd_str() {
char ch = getchar();
s = " ";
while (ch < 'a' || ch > 'z') {
ch = getchar();
}
while (ch >= 'a' && ch <= 'z') {
s += ch;
ch = getchar();
}
}
void print(ll x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x <= 9) {
putchar(x + 48);
return;
}
print(x / 10);
putchar(x % 10 + 48);
}
namespace Sub1{
ll l;
char st[N];
void initSub1() {
l = 0;
ans = 0;
for (ll i = 1; i <= n; ++i) {
l = 0;
for (ll j = i; j <= n; ++j) {
if (l == 0) {
++l;
st[l] = s[j];
}
else if (s[j] != st[l]) {
++l;
st[l] = s[j];
}
else {
--l;
}
if (l == 0) {
++ans;
}
}
}
printf("%lld", ans);
}
}
namespace Sub2{
ll x, f[N], g[N];
void initSub2() {
ans = 0;
// for (ll i = 1; i <= maxm; ++i) {
// h[n + 1][i] = n + 1;
// }
for (ll i = n; i >= 1; --i) {
// x = h[i + 1][s[i] ^ 96];
// if (x > n) {
// g[i] = 0;
// for (ll j = 1; j <= maxm; ++j) {
// h[i][j] = n + 1;
// }
// h[i]s[i]
// }
// else {
// g[i] = g[x] + 1;
// for (ll j = 1; j <= maxm; ++j) {
// h[i][j] = h[x + 1][j];
// }
// if (x + 1 <= n) {
// h[i][s[x + 1] ^ 96] = x + 1;
// }
// }
// ans += g[i];
ll x = i + 1;
while (x <= n && s[x] != s[i]) {
x = f[x] + 1;
}
if (x > n) {
g[i] = 0;
f[i] = n + 1;
}
else {
if (x == n) g[i] = 1;
else g[i] = g[x + 1] + 1;
f[i] = x;
}
ans += g[i];
}
printf("%lld", ans);
}
}
int main() {
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
n = rd();
rd_str();
if (n <= 8000) {
Sub1::initSub1();
}
else {
Sub2::initSub2();
}
return 0;
}这才是真正的正解。
细心的同学可能发现了,我的考场代码里面有一些注释掉的代码,这里说一下原因:
其实我考场上就想到了正解了并打了出来(但是不保证正确性),我当时脑子瓦特了,以为 中每个字符只会往后跳 次,因此它的时间复杂度是 的(虽然实际上就是,但是我那结论明显错误,导致我赛后几个小时一直在研究这玩意的时间复杂度),而且看到 的空间复杂度更优,常数更小,更不容易出错,于是把正解注释掉,改打了 。
回归正题。设 。也就是对于消除区间的后面一个字符进行分类。当这样的 不存在时,。
注意 ,此时消除区间不存在,只有后面的一个字符 ,这样也是可以转移的。
设 ,则 ;。用人话说就是将 数组先复制到 ,再更新 。若 时,除了 以外都为 。
还要更新 ,当 时,;当 时,。
放一下赛后代码(Sub1 懒得删了):
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 2e6, maxm = 26;
const ll N = maxn + 10, M = maxm + 1;
ll n, m, ans;
string s;
ll rd() {
char ch = getchar();
ll s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') {
w = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = (s << 3) + (s << 1) + (ch ^ 48);
ch = getchar();
}
return (s * w);
}
void rd_str() {
char ch = getchar();
s = " ";
while (ch < 'a' || ch > 'z') {
ch = getchar();
}
while (ch >= 'a' && ch <= 'z') {
s += ch;
ch = getchar();
}
}
void print(ll x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x <= 9) {
putchar(x + 48);
return;
}
print(x / 10);
putchar(x % 10 + 48);
}
namespace Sub1{
ll l;
char st[N];
void initSub1() {
l = 0;
ans = 0;
for (ll i = 1; i <= n; ++i) {
l = 0;
for (ll j = i; j <= n; ++j) {
if (l == 0) {
++l;
st[l] = s[j];
}
else if (s[j] != st[l]) {
++l;
st[l] = s[j];
}
else {
--l;
}
if (l == 0) {
++ans;
}
}
}
printf("%lld", ans);
}
}
namespace Sub2{
ll x, g[N];
int h[N][M];
void initSub2() {
ans = 0;
for (ll i = 1; i <= maxm; ++i) {
h[n + 1][i] = n + 1;
}
for (ll i = n; i >= 1; --i) {
x = h[i + 1][s[i] ^ 96];
if (x > n) {
g[i] = 0;
for (ll j = 1; j <= maxm; ++j) {
h[i][j] = n + 1;
}
h[i][s[i] ^ 96] = i;
}
else {
g[i] = g[x + 1] + 1;
for (ll j = 1; j <= maxm; ++j) {
h[i][j] = h[x + 1][j];
}
h[i][s[i] ^ 96] = i;
}
ans += g[i];
// ll x = i + 1;
// while (x <= n && s[x] != s[i]) {
// x = f[x] + 1;
// }
// if (x > n) {
// g[i] = 0;
// f[i] = n + 1;
// }
// else {
// if (x == n) g[i] = 1;
// else g[i] = g[x + 1] + 1;
// f[i] = x;
// }
// ans += g[i];
}
printf("%lld", ans);
}
}
int main() {
n = rd();
rd_str();
// if (n <= 8000) {
// Sub1::initSub1();
// }
// else {
Sub2::initSub2();
// }
return 0;
}有点意思的是 这个神奇暴力的时间复杂度等于正解,而空间复杂度优于正解……
有人说这道题有原题:CF1223F,其实这两道题并不完全一样,原题中 ,要 加上 map 和 swap 才能过,因此是紫;而本题 只有 ,比 CF1223F 要简单。
C 结构体
大模拟。按照题意模拟即可 (其实是我懒得写题解)。
这是赛后写的代码。我还没过,洛谷上 95pts,云斗 85pts,有没有巨佬能够帮我看看,谢谢!
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1000;
const ll N = maxn + 10;
ll n, m, cnt, l; // cnt 是类型编号的最大值,l 是最大地址 + 1
unordered_map<string, ll> bianhao; // 给类型编号
struct jiedian{ // 存储结构体里面的元素
string p, q; // p 是类型名,q 是变量名
};
vector<jiedian> erzi[N]; // 存储结构体里面的元素
ll duiqi[N], daxiao[N]; // duiqi[i] 是编号为 i 的类型的对齐要求,daxiao[i] 是编号为 i 的类型所占据的大小。
struct jiedian1{ // 处理 opt3, opt4 时用,比 jiedian 额外存储起始地址
string p, q; // p 是类型名,q 是变量名
ll x; // x 是起始地址
};
vector<jiedian1> f; // 最外层定义的元素(即 opt2 定义的元素)
string ans, a[N]; // ans 存储 opt4 的答案,a 存储 opt3 中输入的字符串拆分开来
ll calc(ll x, ll y) { // 当前第一个空的地址为 x,下一个要放的元素的对其要求为 y,求下一个要放的元素的起始地址
return (x + y - 1) / y * y;
}
ll rd() {
char ch = getchar();
ll s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') {
w = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = (s << 3) + (s << 1) + (ch ^ 48);
ch = getchar();
}
return (s * w);
}
void rd_str(string &s) { // 字符串快读
char ch = getchar();
s = "";
while (!((ch >= 'a' && ch <= 'z') || ch == '.')) {
ch = getchar();
}
while ((ch >= 'a' && ch <= 'z') || ch == '.') {
s += ch;
ch = getchar();
}
}
void print(ll x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x <= 9) {
putchar(x + 48);
return;
}
print(x / 10);
putchar(x % 10 + 48);
}
ll fenjie(string s) { // opt3 中将读入的字符串按 '.' 拆分成若干个字符串
ll t = 1;
a[1] = "";
for (ll i = 0; i < (ll)(s.length()); ++i) {
if (s[i] == '.') {
++t;
a[t] = "";
}
else {
a[t] += s[i];
}
}
return t;
}
int main() {
n = rd();
// 给 4 种基本类型编号
bianhao["byte"] = 1;
duiqi[1] = 1;
daxiao[1] = 1;
bianhao["short"] = 2;
duiqi[2] = 2;
daxiao[2] = 2;
bianhao["int"] = 3;
duiqi[3] = 4;
daxiao[3] = 4;
bianhao["long"] = 4;
duiqi[4] = 8;
daxiao[4] = 8;
cnt = 4;
l = 0;
{ll opt, k;
string s, t, u;
for (ll qq = 1; qq <= n; ++qq) {
opt = rd();
if (opt == 1) {
rd_str(s); k = rd();
bianhao[s] = ++cnt; // 对其进行编号
duiqi[cnt] = 0;
daxiao[cnt] = 0;
ll y = 0;
for (ll i = 1; i <= k; ++i) { // 读入结构体内部的元素
rd_str(t); rd_str(u);
erzi[cnt].push_back((jiedian){t, u}); // 用 vector 存储
ll x = bianhao[t]; // x 表示这个元素的类型的编号
duiqi[cnt] = max(duiqi[cnt], duiqi[x]);
ll v = calc(y, duiqi[x]); // 计算这个元素的起始地址
y = v + daxiao[x]; // 更新地址最大值 + 1
}
daxiao[cnt] = calc(y, duiqi[cnt]); // 计算整个结构体的大小
printf("%lld %lld\n", daxiao[cnt], duiqi[cnt]);
}
else if (opt == 2) {
rd_str(s); rd_str(t);
ll y = bianhao[s];
ll x = calc(l, duiqi[y]); // 计算这个元素的起始地址
f.push_back((jiedian1){s, t, x}); // 将这个元素插入 f,留给 opt3, opt4 备用
l = x + daxiao[y]; // 更新 l
printf("%lld\n", x);
}
else if (opt == 3) {
rd_str(s);
ll y;
y = fenjie(s); // 将读入的字符串拆开来
jiedian1 p;
for (auto i : f) { // 找到名称相符的元素,存到 p 里面
if (i.q == a[1]) {
p = i;
break;
}
}
ll w = p.x; // w 存储起始地址
for (ll i = 2; i <= y; ++i) { // 一层一层处理
jiedian1 q;
ll x = bianhao[p.p];
for (auto j : erzi[x]) { // 找到名称相符的元素并计算起始地址,存储到 q 里面
ll pp = bianhao[j.p];
ll v = calc(w, duiqi[pp]); // 计算新的起始地址
if (j.q == a[i]) {
q = (jiedian1){j.p, j.q, v};
break;
}
w = v + daxiao[pp];
}
p = q;
}
cout << p.x << endl;
}
else {
k = rd();
jiedian1 p;
for (auto i : f) { // 找到这个地址所在的元素,根据起始地址判断,找到最后一个起始地址小于 k 的元素
if (i.x <= k) {
p = i;
}
else {
break;
}
}
if (p.x + daxiao[bianhao[p.p]] - 1 < k) { // 在空洞里或超出了最大地址
printf("ERR\n");
continue;
}
ll w = p.x; // w 表示起始地址
ll x = bianhao[p.p]; // x 表示这个元素的类型的编号
ans = p.q; // 处理 ans
bool z = 0;
while (x > 4) { // 这个元素的类型不是基础类型
jiedian1 q;
for (auto j : erzi[x]) { // 找到这个地址所在的元素并重新计算起始地址,存储到 q 里面
ll pp = bianhao[j.p];
ll v = calc(w, duiqi[pp]); // 计算新的起始地址
if (v <= k) {
q = (jiedian1){j.p, j.q, v};
}
else {
break;
}
w = v + daxiao[pp];
}
p = q;
x = bianhao[p.p];
if (p.x + daxiao[x] - 1 < k) { // 在空洞里或超出了结构体内部的最大地址
z = 1;
break;
}
ans += "."; // 处理 ans
ans += p.q;
w = p.x;
}
if (z) {
printf("ERR\n");
continue;
}
cout << ans << endl;
}
}}
return 0;
}D 种树
考虑二分答案。二分使全部的树都长到目标高度的时间 。
然后我们可以对于每一个 ,通过二分求出 表示最晚要在时刻 种下这棵树,否则这棵树就不能在第 天长到目标高度。如在 时刻种树也不能长到目标高度,则 。
为此,我们需要写一个函数 calc(i, p),来计算 节点在 时刻种树,到 时刻时树的高度。发现 calc 实际上就是计算一个分段函数的值。
然后通过差分即可求出 。
处理时有个细节,直接计算会爆 long long,有两种处理方法:
-
开
__int128。 -
处理时小心一点,用除法和减法来判断会不会溢出。
由于我比较懒,就直接使用 __int128 了(即代码中的 po,这个类型名是我随便取的,不要介意)
放一下这部分的代码。这部分数学处理可能有多种实现方法,不一定要按我的来。
po calc(ll x, ll k) { // 计算 x 节点在 1 时刻种树,到 k 时刻终,树的高度
ll u; // u 表示 b[i] + x * c[i] <= 0 的最早时刻
if (a[x].k >= 0) {
u = INF + 1;
}
else {
u = max(1ll, (a[x].y + (-a[x].k) - 1) / (-a[x].k));
}
if (k < u) {
return (po)(k) * a[x].y + (po)(k) * (k + 1) / 2 * a[x].k;
}
else {
return (po)(u - 1) * a[x].y + (po)(u) * (u - 1) / 2 * a[x].k + k - u + 1;
}
}
ll solve(ll x, ll k) { // 计算 x 节点要满足到 k 时刻终树的高度 >= a[x],最晚要在什么时刻种树,如不能满足返回 -1,也就是求 b[x] 的值
ll l = 1, r = n + 1, mid, res = -1;
while (l <= r) {
mid = (l + r) / 2;
if (calc(x, k) - calc(x, mid - 1) >= a[x].x) {
res = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
return res;
}现在我们已经求出了 ,考虑如何判断是否可行。
首先有一个显然的结论:肯定要在 时刻把 棵树全部种完。
因为如果有树在 的时刻种了,那么把它放到 中的某个时刻种肯定更优。
考虑如何处理种儿子必须要先种父亲的限制。设 表示处理完父子关系后, 节点最晚可以种树的时刻,则显然有 ,也就是 。同时还有 。
因此我们可以用树上 dp 的形式求出 。易推出转移式:
直接做树上 dp 求出 即可。
现在要考虑如何安排点的种植顺序。有一个贪心策略:每一次都去取未被种植的点中, 最小的一个,将其种植。其正确性显然,而且不会违反种儿子必须要先种父亲的限制(因为 )
因此只用将所有 插入小根堆,从 到 枚举时刻 ,如果有堆顶 ,则判断无解,直接返回。
否则有解。
至此,这道题已经被我们做完了。但是我交上去的时候因为常数过大而 T 了,只有前 个点没有 T(这不跟 一样了)。
减小常数的话可以删去 calc 函数,将其塞进 solve 函数里面。(虽然看起来有点难受)
下面放一下完整代码。这道题实现的时候主要要注意数学计算的部分,不要写炸。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 po;
const ll maxn = 1e5;
const ll N = maxn + 10;
const ll INF = 1e9;
ll n, m, num, lft, rgh, mid, ans, fa[N], H[N], b[N], f[N];
struct nde{
ll x, y, k;
}a[N];
struct edg{
ll t, nxt;
}E[N * 2];
void add_edg(ll x, ll y) {
++num;
E[num].t = y;
E[num].nxt = H[x];
H[x] = num;
}
ll solve(ll x, ll k) { // 计算 x 节点要满足到 k 时刻终树的高度 >= a[x],最晚要在什么时刻种树,如不能满足返回 -1
ll l = 1, r = INF, mid, res = -1;
ll u; // u 表示 b[i] + x * c[i] <= 0 的最早时刻
if (a[x].k >= 0) {
u = INF + 1;
}
else {
u = max(1ll, (a[x].y + (-a[x].k) - 1) / (-a[x].k));
}
while (l <= r) {
mid = (l + r) / 2;
if ((k < u ? (po)(k) * a[x].y + (po)(k) * (k + 1) / 2 * a[x].k : (po)(u - 1) * a[x].y + (po)(u) * (u - 1) / 2 * a[x].k + k - u + 1)
- (mid - 1 < u ? (po)(mid - 1) * a[x].y + (po)(mid - 1) * mid / 2 * a[x].k : (po)(u - 1) * a[x].y + (po)(u) * (u - 1) / 2 * a[x].k + mid - u) >= a[x].x) {
res = mid;
l = mid + 1;
}
else {
r = mid - 1;
}
}
return res;
}
void dfs(ll x, ll fr) {
f[x] = b[x];
for (ll i = H[x]; i > 0; i = E[i].nxt) {
ll v = E[i].t;
if (v != fr) {
dfs(v, x);
f[x] = min(f[x], f[v] - 1);
}
}
}
bool chck(ll k) {
for (ll i = 1; i <= n; ++i) {
b[i] = solve(i, k);
if (b[i] == -1) {
return 0;
}
// cout << "C " << i << ' ' << k << ' ' << solve(i, k) << endl;
}
dfs(1, 0);
priority_queue<ll, vector<ll>, greater<ll> > q;
for (ll i = 1; i <= n; ++i) {
q.push(f[i]);
}
// cout << "A " << k << endl;
for (ll i = 1; i <= n; ++i) {
ll x = q.top();
q.pop();
// cout << x << endl;
if (x < i) {
return 0;
}
}
return 1;
}
ll rd() {
char ch = getchar();
ll s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') {
w = -1;
}
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = (s << 3) + (s << 1) + (ch ^ 48);
ch = getchar();
}
return (s * w);
}
void print(ll x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x <= 9) {
putchar(x + 48);
return;
}
print(x / 10);
putchar(x % 10 + 48);
}
int main() {
n = rd();
for (ll i = 1; i <= n; ++i) {
a[i].x = rd(); a[i].y = rd(); a[i].k = rd();
}
{ll u, v;
for (ll i = 1; i <= n - 1; ++i) {
u = rd(); v = rd();
add_edg(u, v);
add_edg(v, u);
}}
lft = n; rgh = INF; ans = INF;
while (lft <= rgh) {
mid = (lft + rgh) / 2;
if (chck(mid)) {
ans = mid;
rgh = mid - 1;
}
else {
lft = mid + 1;
}
}
printf("%lld", ans);
return 0;
}总结
感觉今年 S 组偏水。
在思维难度上,T1 和 T3 都很水,T2 和 T4 略难,但是肯定没有去年 T4 的难度。
在代码难度上,今年 T3 可能要比去年的所有题目略难,但是也不能算太难,就是很容易炸掉。其他几题代码难度都比较简单。
综上所述,今年 S 组要比去年水,但是不知道为什么预估分数线比去年低 分。