\[\Huge\mathfrak{CSP - S 2023} \]\[{\color{orange}\textrm{340}}\textrm{ / 400} \]
\(\textrm{A}\;\text{密码锁}\)
\({\color{limegreen}\textrm{100}}\textrm{ / 100}\)
\(\textrm{We decide.}\)
\(\textrm{We choose.}\)
\(\textrm{As we decide and choose, so are our lives formed.}\)
\(\textrm{In the end, forming our own destiny is what ambition is about.}\)
点击查看代码
/*
We decide.
We choose.
As we decide and choose, so are our lives formed.
In the end, forming our own destiny is what ambition is about.
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 8, V = 1e5;
int n;
int cnt[V + 5];
int a[10], b[10];
int vis[V + 5], dfn;
void cpy()
{
for (int i = 1; i <= 5; i++)
b[i] = a[i];
return ;
}
int calc()
{
int res = 0;
for (int i = 1; i <= 5; i++)
res = res * 10 + b[i];
return res;
}
int main()
{
freopen("lock.in", "r", stdin);
freopen("lock.out", "w", stdout);
scanf("%d", &n);
for (int T = 1; T <= n; T++)
{
for (int i = 1; i <= 5; i++)
scanf("%d", a + i);
dfn++;
for (int i = 1; i <= 5; i++)
{
for (int x = 1; x <= 9; x++)
{
cpy();
b[i] = (b[i] + x) % 10;
vis[calc()] = dfn;
cpy();
b[i] = (b[i] + 10 - x) % 10;
vis[calc()] = dfn;
}
}
for (int i = 1; i < 5; i++)
{
for (int x = 1; x <= 9; x++)
{
cpy();
b[i] = (b[i] + x) % 10;
b[i + 1] = (b[i + 1] + x) % 10;
vis[calc()] = dfn;
cpy();
b[i] = (b[i] + 10 - x) % 10;
b[i + 1] = (b[i + 1] + 10 - x) % 10;
vis[calc()] = dfn;
}
}
for (int i = 0; i < V; i++)
cnt[i] += (vis[i] == dfn);
}
int ans = 0;
for (int i = 0; i < V; i++)
ans += (cnt[i] == n);
printf("%d\n", ans);
return 0;
}
\(\textrm{B}\;\text{消消乐}\)
\({\color{limegreen}\textrm{100}}\textrm{ / 100}\)
\(\textrm{I believe that I am who I am}\)
\(\textrm{Unique and unstoppable}\)
\(\textrm{Not afraid of loneliness}\)
\(\textrm{Not afraid of wind and waves}\)
点击查看代码
/*
I believe that I am who I am
Unique and unstoppable
Not afraid of loneliness
Not afraid of wind and waves
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 2e6, V = 26;
int n;
int a[N + 5];
int match[N + 5], cnt[N + 5];
int lst[N + 5][V + 5];
int main()
{
freopen("game.in", "r", stdin);
freopen("game.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
char c;
scanf(" %c", &c);
a[i] = c - 'a' + 1;
}
for (int i = 1; i <= n; i++)
{
match[i] = lst[i - 1][a[i]];
if (match[i])
{
cnt[i] = cnt[match[i] - 1] + 1;
for (int j = 1; j <= V; j++)
lst[i][j] = lst[match[i] - 1][j];
lst[i][a[i]] = i;
}
lst[i][a[i]] = i;
}
long long ans = 0;
for (int i = 1; i <= n; i++)
ans += cnt[i];
printf("%lld\n", ans);
return 0;
}
\(\textrm{C}\;\text{结构体}\)
\({\color{limegreen}\textrm{100}}\textrm{ / 100}\)
\(\textrm{Not enjoyment, and not sorrow}\)
\(\textrm{Is our destined end or way}\)
\(\textrm{But ot act, that each tomorrow}\)
\(\textrm{Finds us further than today}\)
点击查看代码
/*
Not enjoyment, and not sorrow
Is our destined end or way
But ot act, that each tomorrow
Finds us further than today
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int N = 100;
const string Basic[4] = {"byte", "short", "int", "long"};
int n;
map<string, int> mp;
int tot;
int turn(string s)
{
if (mp[s])
return mp[s];
mp[s] = ++tot;
return tot;
}
long long Next(long long x, long long p)
{
if (x % p == 0)
return x;
return x + p - x % p;
}
struct Child
{
int type;
string name;
long long delta;
};
long long sz[N + 5], lim[N + 5];
vector<Child> g[N + 5];
string StructName;
int k;
string Type[N + 5], Name[N + 5];
void Init()
{
for (int i = 0, j = 1; i < 4; i++, j <<= 1)
{
int p = turn(Basic[i]);
sz[p] = lim[p] = j;
}
return ;
}
void NewStruct()
{
int id = turn(StructName);
for (int t = 1; t <= k; t++)
{
Child son;
son.type = turn(Type[t]);
son.name = Name[t];
sz[id] = Next(sz[id], lim[son.type]);//head, 0 ~ (sz - 1)
son.delta = sz[id];
sz[id] += sz[son.type];
lim[id] = max(lim[id], lim[son.type]);
g[id].push_back(son);
}
sz[id] = Next(sz[id], lim[id]);
printf("%lld %lld\n", sz[id], lim[id]);
return ;
}
int ValueType[N + 5];
string ValueName[N + 5];
long long Head[N + 5];
int cnt;
int len;
string Query[N + 5];
void Cut()
{
string s;
cin >> s;
len = 0;
string temp = "";
for (unsigned int i = 0; i < s.size(); i++)
{
if (s[i] == '.')
{
Query[++len] = temp;
temp = "";
}
else
temp += s[i];
}
Query[++len] = temp;
return ;
}
long long GetAddress(int u, int id)
{
for (unsigned int i = 0; i < g[u].size(); i++)
{
if (g[u][i].name != Query[id])
continue;
if (id == len)
return g[u][i].delta;
return g[u][i].delta + GetAddress(g[u][i].type, id + 1);
}
return 0ll;
}
string Path[N + 5];
bool HaveAns;
void FindPath(int u, long long beg, long long pos)
{
if (g[u].empty())//Basic
{
HaveAns = true;
return ;
}
for (unsigned int i = 0; i < g[u].size(); i++)
{
long long Begin = beg + g[u][i].delta;
long long End = Begin + sz[g[u][i].type] - 1;
if (!(Begin <= pos && pos <= End))
continue;
Path[++len] = g[u][i].name;
FindPath(g[u][i].type, beg + g[u][i].delta, pos);
break;
}
return ;
}
int main()
{
freopen("struct.in", "r", stdin);
freopen("struct.out", "w", stdout);
scanf("%d", &n);
Init();
for (int T = 1, op; T <= n; T++)
{
scanf("%d", &op);
if (op == 1)
{
cin >> StructName >> k;
for (int i = 1; i <= k; i++)
cin >> Type[i] >> Name[i];
NewStruct();
}
if (op == 2)
{
cnt++;
string _type;
cin >> _type >> ValueName[cnt];
ValueType[cnt] = turn(_type);
if (cnt == 1)
Head[cnt] = 0;
else
Head[cnt] = Next(Head[cnt - 1] + sz[ ValueType[cnt - 1] ],
lim[ ValueType[cnt] ]);
printf("%lld\n", Head[cnt]);
}
if (op == 3)
{
Cut();
long long pos = 0;
for (int i = 1; i <= cnt; i++)
{
if (ValueName[i] != Query[1])
continue;
if (len == 1)
pos = Head[i];
else
pos = Head[i] + GetAddress(ValueType[i], 2);
break;
}
printf("%lld\n", pos);
}
if (op == 4)
{
long long addr;
scanf("%lld", &addr);
len = 0;
HaveAns = false;
for (int i = 1; i <= cnt; i++)
{
long long Begin = Head[i];
long long End = Begin + sz[ValueType[i]] - 1;
if (!(Begin <= addr && addr <= End))
continue;
Path[++len] = ValueName[i];
FindPath(ValueType[i], Head[i], addr);
}
if (HaveAns)
{
cout << Path[1];
for (int i = 2; i <= len; i++)
cout << "." << Path[i];
printf("\n");
}
else
puts("ERR");
}
}
return 0;
}
\(\textrm{D}\;\text{种树}\)
\({\color{orange}\textrm{40}}\textrm{ / 100}\)
\(\textrm{I shall be telling this with a sigh}\)
\(\textrm{Somewhere ages and ages hence}\)
\(\textrm{Two roads diverged in a wood, and I}\)
\(\textrm{I took the one less travelled by}\)
\(\textrm{And that has made all the difference}\)
考场 code
点击查看代码
/*
I shall be telling this with a sigh
Somewhere ages and ages hence
Two roads diverged in a wood, and I
I took the one less travelled by
And that has made all the difference
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int N = 1e5, E = N << 1;
const long long Max = 1e9;
typedef pair<long long, int> pir;
int n;
long long a[N + 5], b[N + 5], c[N + 5], zero[N + 5];
int head[N + 5], to[E + 5], nxt[E + 5], tot = 1;
void add_edge(int u, int v)
{
tot++;
to[tot] = v;
nxt[tot] = head[u];
head[u] = tot;
return ;
}
void add(int u, int v)
{
add_edge(u, v);
add_edge(v, u);
return ;
}
long long d[N + 5];//limit
int sz[N + 5];
void calc_d(int u, long long ans)
{
long long l = 1, r = ans;
d[u] = Max * 100ll;
while (l <= r)
{
long long mid = (l + r) >> 1, sum = 0;
if (c[u] >= 0)
sum = (ans - mid + 1) * b[u]
+ (mid + ans) * (ans - mid + 1) / 2 * c[u];
else
{
if (mid > zero[u])
sum = ans - mid + 1;
else
sum = (zero[u] - mid + 1) * b[u]
+ (mid + zero[u]) * (zero[u] - mid + 1) / 2 * c[u]
+ ans - zero[u];
}
if (a[u] <= sum)
{
d[u] = mid;
l = mid + 1;
}
else
r = mid - 1;
}
return ;
}
// bool cmp(int x, int y)
// {
// if (d[x] + sz[x] != d[y] + sz[y])
// return d[x] + sz[x] < d[y] + sz[y];
// if (d[x] != d[y])
// return d[x] < d[y];
// return sz[x] < sz[y];
// }
// void dfs(int u, int fa)
// {
// vector<int> son;
// sz[u] = 1;
// for (int i = head[u]; i; i = nxt[i])
// {
// int v = to[i];
// if (v == fa)
// continue;
// dfs(v, u);
// sz[u] += sz[v];
// son.push_back(v);
// }
// if (son.empty())
// return ;
// sort(son.begin(), son.end(), cmp);
// long long sum = 0;
// for (unsigned int t = 0; t < son.size(); t++)
// {
// int v = son[t];
// d[u] = min(d[u], d[v] - 1 - sum);
// sum += sz[v];
// }
// return ;
// }
int fa[N + 5], in[N + 5];
void dfs(int u, int father)
{
fa[u] = father;
in[father]++;
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if (v == father)
continue;
dfs(v, u);
}
return ;
}
priority_queue<pir> q;
int seq[N + 5];
bool check(long long ans)
{
for (int i = 1; i <= n; i++)
{
calc_d(i, ans);
if (d[i] > Max)
return false;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
{
if (in[i] == 0)
q.emplace(d[i], i);
}
for (int T = n; T > 0; T--)
{
int u = q.top().second;
q.pop();
seq[T] = u;
if (fa[u])
{
in[fa[u]]--;
if (in[fa[u]] == 0)
q.emplace(d[fa[u]], fa[u]);
}
}
// for (int i = 1; i <= n; i++)
// {
// cout << i << " " << d[i] << endl;
// }
// cout << endl;
// for (int i = 1; i <= n; i++)
// {
// cout << seq[i] << " ";
// }
// cout << endl;
// dfs(1, 0);
// for (int i = 1; i <= n; i++)
// {
// cout << i << " " << d[i] << endl;
// }
// cout << endl;
// if (d[1] < 1ll)
// return false;
// return true;
for (int i = 1; i <= n; i++)
{
if (1ll * i > d[seq[i]])
return false;
}
return true;
}
int main()
{
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld%lld", a + i, b + i, c + i);
if (c[i] < 0)
zero[i] = (1ll - b[i]) / c[i];
}
for (int i = 1, u, v; i < n; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
}
// check(5);
// exit(0);
long long l = 1, r = Max, ans = 0;
while (l <= r)
{
long long mid = (l + r) >> 1;
if (check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%lld\n", ans);
return 0;
}
AC code
点击查看代码
/*
I shall be telling this with a sigh
Somewhere ages and ages hence
Two roads diverged in a wood, and I
I took the one less travelled by
And that has made all the difference
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
const int N = 1e5, E = N << 1;
const long long Max = 1e9;
typedef pair<long long, int> pir;
int n;
long long a[N + 5], b[N + 5], c[N + 5], zero[N + 5];
int head[N + 5], to[E + 5], nxt[E + 5], tot = 1;
void add_edge(int u, int v)
{
tot++;
to[tot] = v;
nxt[tot] = head[u];
head[u] = tot;
return ;
}
void add(int u, int v)
{
add_edge(u, v);
add_edge(v, u);
return ;
}
long long d[N + 5];//limit
int sz[N + 5];
void calc_d(int u, long long ans)
{
long long l = 1, r = n;
d[u] = -1ll;
while (l <= r)
{
long long mid = (l + r) >> 1;
__int128 sum = 0, one = 1;
if (c[u] >= 0)
sum = one * (ans - mid + 1) * b[u]
+ one * (mid + ans) * (ans - mid + 1) / 2 * c[u];
else
{
if (mid > zero[u])
sum = ans - mid + 1;
else if (ans > zero[u])
sum = one * (zero[u] - mid + 1) * b[u]
+ one * (mid + zero[u]) * (zero[u] - mid + 1) / 2 * c[u]
+ ans - zero[u];
else
sum = one * (ans - mid + 1) * b[u]
+ one * (mid + ans) * (ans - mid + 1) / 2 * c[u];
}
if (one * a[u] <= sum)
{
d[u] = mid;
l = mid + 1;
}
else
r = mid - 1;
}
return ;
}
int fa[N + 5], in[N + 5];
void dfs(int u, int father)
{
fa[u] = father;
in[father]++;
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i];
if (v == father)
continue;
dfs(v, u);
}
return ;
}
priority_queue<pir> q;
int seq[N + 5];
bool check(long long ans)
{
for (int i = 1; i <= n; i++)
{
calc_d(i, ans);
if (d[i] < 0)
return false;
}
dfs(1, 0);
for (int i = 1; i <= n; i++)
{
if (in[i] == 0)
q.emplace(d[i], i);
}
for (int T = n; T > 0; T--)
{
int u = q.top().second;
q.pop();
seq[T] = u;
if (fa[u])
{
in[fa[u]]--;
if (in[fa[u]] == 0)
q.emplace(d[fa[u]], fa[u]);
}
}
for (int i = 1; i <= n; i++)
{
if (1ll * i > d[seq[i]])
return false;
}
return true;
}
int main()
{
// freopen("tree.in", "r", stdin);
// freopen("tree.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%lld%lld%lld", a + i, b + i, c + i);
if (c[i] < 0)
zero[i] = (1ll - b[i]) / c[i];
}
for (int i = 1, u, v; i < n; i++)
{
scanf("%d%d", &u, &v);
add(u, v);
}
long long l = 1, r = Max, ans = 0;
while (l <= r)
{
long long mid = (l + r) >> 1;
if (check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%lld\n", ans);
return 0;
}