模拟赛D5总结
A
首先考虑拆边,可以通过建虚拟点的的方式,使每一条边的权重都为单个字符。
设在时刻
-
点集需去重。
-
点集中所有点可间接到达
n 。
此时我们顺利解决了第二问。那第一问呢?一样的道理,仅需保证点集中所有点处在到达
所有这些可以用bfs预处理搞定。
但此时时间复杂度是玄学的。我考场上就是死在这里,对自己没信心,懒得写,在100pts前自爆了。
但我们是SPFA啊,再加上一些玄学的只会使时间更短的因素,大胆写完后发现自己过了???
此题较卡空间。
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 7;
int n, m, tot, f[MAXN];
vector<pair<int, int> > s[MAXN];
vector<int> t[MAXN];
queue<int> q;
string ans;
bool v[MAXN];
int main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m; tot = n;
for(int i = 1; i <= m; i++){
int u, v; string c;
cin >> u >> v >> c; int pre = u;
for(int j = 0; j + 1 < c.size(); j++){
// s[pre].push_back({++tot, c[j] - 'a'});
s[pre].emplace_back(++tot, c[j] - 'a');
// t[tot].push_back(pre); pre = tot;
t[tot].emplace_back(pre); pre = tot;
}
s[pre].push_back({v, c.back() - 'a'});
t[v].push_back(pre);
}
f[n] = 1; q.push(n);
while(!q.empty()){
int u = q.front(); q.pop();
for(int i : t[u]){
if(f[i]) continue;
f[i] = f[u] + 1; q.push(i);
}
}
q.push(1); //第一问
while(!q.empty()){
vector<int> p[26];
bool flag = 0;
while(!q.empty()){
int u = q.front();q.pop();
if(u == n) flag = 1;
for(pair<int, int> j : s[u]){
if(f[j.first] + 1 == f[u]) p[j.second].push_back(j.first);
}
}
if(flag) break;
for(int i = 0; i < 26; i++){
if(!p[i].empty()){
for(int j : p[i]) if(!v[j]) v[j] = 1, q.push(j);
for(int j : p[i]) v[j] = 0;
ans += (i + 'a'); break;
}
}
}
cout << ans << ' '; ans = "";
q.push(1); //第二问
while(!q.empty()){
vector<int> p[26];
bool flag = 0;
while(!q.empty()){
int u = q.front();q.pop();
if(u == n) flag = 1;
for(pair<int, int> j : s[u]){
if(f[j.first]) p[j.second].push_back(j.first);
}
}
if(flag) break;
for(int i = 0; i < 26; i++){
if(!p[i].empty()){
for(int j : p[i]) if(!v[j]) v[j] = 1, q.push(j);
for(int j : p[i]) v[j] = 0;
ans += i + 'a'; break;
}
}
}
cout << ans;
return 0;
}
B
先看欧拉函数快速解法 (时间复杂度
int phi(int p){
int sum = p;
for(int i = 2; i * i <= p; i++){
if(p % i == 0){
sum = sum / i * (i - 1);
while(p % i == 0) p /= i;
}
}
if(p > 1) sum = sum / p * (p - 1);
return sum;
}
回到本题,瞪眼法:
- 迭代次数极小(到一停止),因为当
x 为偶数,迭代时f(x) 至少会缩减一半。其它以此类推。
于是我们得出:
-
当
x\leq B 时,f(x)=x (显然) -
当
x > B + 100 时,f(x)=1 。
中间这100个数就可以开始时预处理。
现在我们就还有最后一个问题:
再次瞪眼法:当
我们粗暴的得出结论:答案一定出自小于等于
所以预处理的时间复杂度大致为
此题极难。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int X = 70;
int T, B, l, r, ans[X];
bool check(int p){
for(int i = 2; i * i <= p; i++){
if(p % i == 0) return 0;
}
return 1;
}
int phi(int p){
int sum = p;
for(int i = 2; i * i <= p; i++){
if(p % i == 0){
sum = sum / i * (i - 1);
while(p % i == 0) p /= i;
}
}
if(p > 1) sum = sum / p * (p - 1);
return sum;
}
int A(int x){
int p = x;
while(!check(p)) --p;
int zrr = p - 1 - B;
while(zrr > 0 && x > 1){
x = phi(x), --zrr;
}
return x;
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> T >> B;
for(int i = 1; i < X; i++){
ans[i] = A(B + i);
}
while(T--){
cin >> l >> r;
if(r <= B){
cout << r * (r + 1) / 2 - (l - 1) * l / 2 << '\n';
}else if(l >= B + X) cout << r - l + 1 << '\n';
else{
int sum = 0;
for(int i = max(B + 1, l); i <= min(B + X - 1, r); i++){
sum += ans[i - B];
}
sum += max(B * (B + 1) / 2 - (l - 1) * l / 2, 0ll);
sum += max(r - B - X + 1, 0ll);
cout << sum << '\n';
}
}
return 0;
}
C
树上背包,对于每个节点,所有子树大小是物品代价,我们要选到尽量折中的代价总和。裸了,再加上个上下界优化,勉勉强强骗到 84 pts/ll
bitset 啥的我不会也不想学。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 7;
int n, sz[MAXN], ans;
bool dp[MAXN];
vector<int> s[MAXN];
void dfs(int x, int fa){;
sz[x] = 1;
for(int i : s[x]){
if(i == fa) continue;
dfs(i, x); sz[x] += sz[i];
}
for(int i : s[x]){
if(i == fa) continue;
for(int j = (sz[x] - 1) / 2; j >= sz[i]; j--){
dp[j] |= dp[j - sz[i]];
}
}
for(int j = (sz[x] - 1) / 2; ; j--){
if(dp[j]) {ans += j * (sz[x] - 1 - j); break;}
}
for(int i : s[x]){
if(i == fa) continue;
for(int j = sz[i]; j <= (sz[x] - 1) / 2; j++){
dp[j] = 0;
}
}
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
s[u].push_back(v);
s[v].push_back(u);
}
dp[0] = 1; dfs(1, 0); cout << ans;
return 0;
}
D
打暴力,用一些stringSTL,然后注意最后一种询问类型加上个小优化:
- 找到不同字符就立即
break;
成功骗到60 pts/ll
#include <bits/stdc++.h>
#define int long long
using namespace std;
int q;
string zrr, h;
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
cin >> q >> zrr;
while(q--){
string op; cin >> op;
if(op == "Insert"){
int p; string s; cin >> p >> s;
for (int i = s.size() - 1; ~i; i--) h.insert(h.begin() + p, s[i]);
}else if(op == "Delete"){
int l, r; cin >> l >> r;
h.erase(h.begin() + l - 1, h.begin() + r);
}else if(op == "Replace"){
int l, r; string s; cin >> l >> r >> s;
h.replace(h.begin() + l - 1, h.begin() + r, s);
}else if(op == "Count"){
int l, r; char c; cin >> l >> r >> c;
cout << count(h.begin() + l - 1, h.begin() + r, c) << '\n';
}else{
int L, R, ans = 0; cin >> L >> R;
for(int l = L - 1, r = L - 1 + zrr.size(); r <= R; l++, r++){
bool flag = 1;
for(int i = l; i < r; i++) if(h[i] != zrr[i - l]) {flag = 0; break;}
ans += flag;
}
cout << ans << '\n';
}
}
return 0;
}