勰码金牌夏令营补题报告3
勰码金牌夏令营补题报告3
刘慕贤
题目报告
错误之处
取模了,但只取模了一半
两个统计答案,只取模了一个
题目大意
熊喵喵要打一个最大血量为
如果一个史莱姆受到攻击后,当前血量小于等于最大血量的一半,并且仍未死亡,它就会分裂成两个史莱姆。两个新史莱姆的当前血量和最大血量都等于分裂前的当前血量。
史莱姆不会攻击,只会挨打。
求熊喵喵打掉所有史莱姆需要的回合数。答案可能很大,请输出答案对
你需要处理多组数据。
题目解析
清新小思维题
观察样例易得,史莱姆的分裂类似于树,只需要
正解代码
奉上代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MOD = 100000007;
ll cd(ll _, ll __)
{
return (_ + __ - 1) / __;
}
int main(){
ios::sync_with_stdio(NULL);
cin.tie(NULL); cout.tie(NULL);
int _;
cin >> _;
while (_--)
{
ll n, k;
cin >> n >> k;
ll __ = 1;
ll ___ = 0;
while (n > 0)
{
ll need = (n + 1) / 2;
ll cnt = cd(need, k);
___ = (___ + __ % MOD * cnt % MOD) % MOD;
n -= (cnt * k);
__ = (__ * 2) % MOD;
if (n <= 0) break;
}
cout << ___ << '\n';
}
return 0;
}
T2 安可(encore)
题目详情
未通过,
错误之处
单调队列判断谁更劣的时候判断反了
题目大意
一只蝴蝶想飞过沧海。
沧海可以看作一个长度为
初始时蝴蝶位于
作为魔法使的你可以进行至多
请判断是否存在一种变换方案,使得变换后的串允许蝴蝶顺利飞过沧海。
题目解析
观察到
代码实现并不困难,见下
正解代码
奉上代码
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10, K = 210;
int a[N], b[N], dp[N][K];
deque<int> q[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tid, T;
cin >> tid >> T;
while (T--)
{
int n, m, k;
cin >> n >> m >> k;
int cnt = 0;
for (int i = 1; i <= n; i += 1)
{
char x; cin >> x;
if (x == '0') a[i] = 0;
else a[i] = 1, cnt += 1;
}
if ((n + 1) <= m)
{
cout << "Yes" << endl;
continue;
}
for (int i = 0; i <= k; i += 1) while (!q[i].empty()) q[i].pop_front();
for (int i = 0; i <= n; i += 1) for (int j = 0; j <= k; j += 1) dp[i][j] = 1e18 + 7;
dp[0][0] = 0;
q[0].push_back(0);
for (int i = 1; i <= n; i += 1)
{
for (int j = 0; j <= k; j += 1)
{
while (!q[j].empty() and q[j].front() < (i - m)) q[j].pop_front();
}
for (int j = 0; j <= k; j += 1)
{
int from = j - (a[i] == 0);
if (from < 0) continue;
if (!q[from].empty())
{
if (dp[q[from].front()][from] != (1e18 + 7)) dp[i][j] = dp[q[from].front()][from] + 1;
}
}
for (int j = 0; j <= k; j += 1)
{
if (dp[i][j] == (1e18 + 7)) continue;
while (!q[j].empty() and dp[q[j].back()][j] >= dp[i][j]) q[j].pop_back();
while (!q[j].empty() and q[j].front() <= (i - m)) q[j].pop_front();
q[j].push_back(i);
}
}
bool res = false;
for (int i = max(1ll, n - m + 1); i <= n; i += 1)
{
for (int j = 0; j <= k; j += 1)
{
if (dp[i][j] == (1e18 + 7) or dp[i][j] > cnt) continue;
res = true;
break;
}
if (res) break;
}
if (res == true) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
T3 最大公约数查询 (gcd)
题目详情
未通过,
题目大意
哥萨克
为了确定
- 选择
1 ≤ l ≤ r ≤ n - 得到区间和
b_l + b_{l+1} + \cdots + b_r - 支付
gcd(a_l, a_{l+1}, \cdots , a_r) 戈比
求保证能够确定整个数组
随后数组
题目解析
假如有一个前缀和数组,那么对于一次
这题的难点不是怎样猜
然后用一些差分约束就可以了
正解代码
奉上代码
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 10;
int a[4 * N];
void push_up(int o)
{
a[o] = __gcd(a[o << 1], a[o << 1 | 1]);
}
void Build_tree(int o, int l, int r)
{
if (l == r)
{
cin >> a[o];
return;
}
int mid = l + r >> 1;
Build_tree(o << 1, l, mid); Build_tree(o << 1 | 1, mid + 1, r);
push_up(o);
return;
}
void modify(int o, int l, int r, int id, int num)
{
if (l == r)
{
a[o] = num;
return;
}
int mid = l + r >> 1;
if (id <= mid) modify(o << 1, l, mid, id, num);
else modify(o << 1 | 1, mid + 1, r, id, num);
push_up(o);
return;
}
int query(int o, int l, int r, int x, int y)
{
if (l == r) return l;
int p = __gcd(a[o << 1], x);
int q = __gcd(a[o << 1 | 1], y);
int mid = l + r >> 1;
if (p <= q) return query(o << 1, l, mid, x, q);
else return query(o << 1 | 1, mid + 1, r, p, y);
}
int calc_1(int o, int l, int r, int id, int num)
{
if (l == r) return __gcd(a[o], num);
int mid = l + r >> 1;
int p = __gcd(a[o << 1 | 1], num);
int q = __gcd(a[o << 1], p);
int res = 0;
if (id <= mid) res += calc_1(o << 1, l, mid, id, p);
else
{
if (p == q) res += (mid - l + 1) * p;
else res += calc_1(o << 1, l, mid, id, p);
res += calc_1(o << 1 | 1, mid + 1, r, id, num);
}
return res;
}
int calc_2(int o, int l, int r, int id, int num)
{
if (l == r) return __gcd(a[o], num);
int mid = l + r >> 1;
int p = __gcd(a[o << 1], num);
int q = __gcd(a[o << 1 | 1], p);
int res = 0;
if (id > mid) res += calc_2(o << 1 | 1, mid + 1, r, id, p);
else
{
if (p == q) res += (r - mid) * p;
else res += calc_2(o << 1 | 1, mid + 1, r, id, p);
res += calc_2(o << 1, l, mid, id, num);
}
return res;
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, _;
cin >> n >> _;
Build_tree(1, 1, n);
int p = query(1, 1, n, 0, 0);
cout << calc_1(1, 1, n, p, 0) + calc_2(1, 1, n, p, 0) - a[1] << endl;
while (_--)
{
int id, num; cin >> id >> num;
modify(1, 1, n, id, num);
int p = query(1, 1, n, 0, 0);
cout << calc_1(1, 1, n, p, 0) + calc_2(1, 1, n, p, 0) - a[1] << endl;
}
return 0;
}
T4 ABC交换(abc)
题目详情
未通过,
错误之处
莫名奇妙的被一些小小的边界情况卡了
同时
题目大意
给定一个只包含字符
一次操作中,可以选择两个相邻字符,只有当它们依次为
求最多能够连续进行多少次操作
每个测试文件包含多组数据,各组数据相互独立
题目解析
正解太难,此处只展示暴力
正解代码
奉上代码
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10;
char a[N];
int tmp[N];
bool f(int x, int y)
{
if (a[x] == 'A' and a[y] == 'B') return 1;
else if (a[x] == 'B' and a[y] == 'C') return 1;
else if (a[x] == 'C' and a[y] == 'A') return 1;
else return 0;
}
int dfs_ans;
void dfs(string s, int cnt)
{
dfs_ans = max(dfs_ans, cnt);
for (int i = 1; i < s.size(); i += 1)
{
if ((s[i - 1] == 'A' and s[i] == 'B') or (s[i - 1] == 'B' and s[i] == 'C') or (s[i - 1] == 'C' and s[i] == 'A'))
{
swap(s[i - 1], s[i]);
dfs(s, cnt + 1);
swap(s[i - 1], s[i]);
}
}
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
if (n <= 12)
{
dfs_ans = 0;
string s;
cin >> s;
dfs(s, 0);
cout << dfs_ans << endl;
continue;
} // 10 pts
for (int i = 1; i <= n; i += 1) cin >> a[i];
if (n <= 3)
{
if (n == 1) cout << 0 << endl;
else if (n == 2)
{
if (f(1, 2)) cout << 1 << endl;
else cout << 0 << endl;
}
else
{
int res = 0;
if (f(1, 2))
{
res += 1;
swap(a[1], a[2]);
if (f(2, 3)) res += 1;
}
else
{
if (f(2, 3))
{
res += 1;
swap(a[2], a[3]);
if (f(1, 2)) res += 1;
}
}
cout << res << endl;
}
continue;
} // 3 pts
bool flag = true;
for (int i = 1; i <= n; i += 1) if (a[i] == 'C') flag = false;
if (flag == true)
{
int cnt = 0;
for (int i = 1; i <= n; i += 1)
{
if (a[i] == 'A') cnt += 1;
else tmp[i] = cnt;
}
int res = 0;
for (int i = 1; i <= n; i += 1) res += tmp[i];
cout << res << endl;
continue;
} // 5 pts
int cnt = 0;
for (int i = 2; i <= n; i += 1)
{
if (a[i - 1] == 'A' and a[i] == 'B')
{
if (cnt == 0) cnt += 1;
else cnt = -1;
}
else if (a[i - 1] == 'B' and a[i] == 'C')
{
if (cnt == 1) cnt += 1;
else cnt = -1;
}
else if ((a[i - 1] == 'A' and a[i] == 'A') or (a[i - 1] == 'B' and a[i] == 'B') or (a[i - 1] == 'C' and a[i] == 'C')) continue;
else cnt = -1;
if (cnt == -1) break;
}
if (cnt == 2)
{
int cnta = 1, cntb = 0, cntc = 0;
for (int i = 2; i <= n; i += 1)
{
if (a[i] == 'A') cnta += 1;
if (a[i] == 'B') cntb += 1;
if (a[i] == 'C') cntc += 1;
}
cout << max(cnta * cntb, cntb * cntc) << endl;
continue;
} // 5 pts
int ans = 0;
for (int i = 2; i <= n; i += 1)
{
if ((a[i - 1] == 'A' and a[i] == 'B') or (a[i - 1] == 'B' and a[i] == 'C') or (a[i - 1] == 'C' and a[i] == 'A')) ans += 1;
} // 10 pts
cout << ans << endl;
}
return 0;
}
总结
一般