跳到正文

个人记录

7.25 模拟赛2

Max0823约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟

版本

  1. v1当前

day 5: 今天打了模拟赛,赛时AC了两道,赛后补了两道。总的来说偏简单,但是第三题爆0了,下次注意。

A: 水,有散装苹果和苹果礼盒,问买至少 n 个苹果最少需要多少钱。买苹果有三种方案,全散装,全礼盒,礼盒加散装,把三种方案取min就行,但是我是后面检查的时候才发现,下次小心点。此题也可以枚举做,可见水分。 赛时100分。

B: 一个需要常识的模拟题,判断闰年(如果是整世纪要整除400才是闰年,其他整除4即可,也就是400年里有97个闰年)写对问题就不大,模拟一下咯。 赛时100分。

C: 看见桌数与钱的函数单调,考虑二分答案,枚举x桌所需的k,再二分找就行,注意右边界为1e18+1e9+1,但是记得这是浮点数,可以转成整数,也可以直接浮点数计算。 赛时0分,因为我把check函数中的自变量命名为k,而我全局变量又有个k,导致我防止ll爆的提前返回没用,全WA了,真气死我了。

D: 看见数据范围1e6,就知道时间O(nlog(n+m))了,而且空间也是O(nlogn),可以考虑LCA做,再维护一个最大子树大小,考虑分类讨论:

  1. 若 x=y, 则所有点都是答案
  2. 若 x,y 深度相同, 则 lca(x,y) 及其所有祖先及其不含 x,y 的子树是答案点
  3. 若 x,y 深度不同, 则只有 x,y 最短路径上的中点(至多1)及其不同于 x,y 的子树是答案点(若两者距离为奇数,则答案为0)。 至于LCA,可以使用倍增,好写而且满足题目要求。

赛时骗了20分,赛后补了100。赛时到LCA之后思路就断了hhh,下次多做点图论练习下。

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 20;
const int mod = 1e9 + 7;
void work() {
    ll n, m, a, b;
    cin >> n >> m >> a >> b;
    ll num = n / m, res = n % m;
    ll ans = a * n;//plan a
    ans = min(b*(num+1), ans);
    ans = min(b*num + a*res, ans);
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while(T--) {
        work();
    }
}
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 2e5 + 20;
const int mod = 1e9 + 7;
const int mth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool run(ll a) {
    if((a % 100 == 0 && a % 400 != 0) || a % 4 != 0) return 0;
    return 1;
}
void work() {
    ll a, b, c;
    cin >> a >> b >> c;
    ll y = (ll)sqrt(a) + 1;
    ll d = mth[b-1] - c + 1;
    for(int i = b; i < 12; i++) {
        d += mth[i];
        if(i == 2 && run(a)) d++;
    }
    ll del = y * y - a - 1;
    d += del * 365;
    if(del >= 400) {
        ll num = del/400;
        del %= 400;
        d += num * 97;
    }
    for(int i = a+1; i <= a+del; i++) {
        if(run(i)) d++;
    }
    cout << d << '\n';
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T = 1;
//    cin >> T;
    while(T--) {
        work();
    }
}
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll INF = LLONG_MAX;
const int N = 2e5 + 20;
vector<ll> a, b;
ll n, k;
ll calc(ll x) {
    ll cost = 0;
    for(int i = 1; i <= n; i++) {
        if(a[i] == 0) continue;

        if(x > 0 && a[i] > INF / x) return INF;
        ll needed = a[i] * x;

        if(b[i] >= needed) continue;

        if(cost > INF - (needed - b[i])) return INF;
        cost += needed - b[i];

        if(cost > k) return INF;
    }
    return cost;
}

void solve() {
    cin >> n >> k;
    a.resize(n+1), b.resize(n+1);
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];

    ll left = 0, right = 0, ans = 0;
    for(int i = 1; i <= n; i++) {
        if(a[i] == 0) continue;
        ll possible = (b[i] + k) / a[i];
        right = max(right, possible);
    }
    right = min(right, (ll)2e18);

    while(left <= right) {
        ll mid = left + (right - left) / 2;
        ll cost = calc(mid);

        if(cost <= k) {
            ans = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}
C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 5e5 + 5;
vector<int> v[N];
int dep[N], f[N][25], sz[N];
void dfs(int u, int fa) {
	dep[u] = dep[fa] + 1;
	sz[u] = 1;
	f[u][0] = fa;
	for(int i = 1; i <= 19; i++)
		f[u][i] = f[f[u][i-1]][i-1];

	for(auto i : v[u]) {
		if(i != fa) {
			dfs(i, u);
			sz[u] += sz[i];
		}
	}
}
int lca(int x, int y) {
	if(dep[x] < dep[y]) swap(x, y);
	for(int i = 20; i >= 0; i--) {
		if(dep[f[x][i]] >= dep[y]) {
			x = f[x][i];
		}
	}
	if(x == y) return x;
	for(int i = 20; i >= 0; i--) {
		if(f[x][i] != f[y][i]) {
			x = f[x][i];
			y = f[y][i];
		}
	}
	return f[x][0];
}
int kthfa(int x, int k) {
	for(int i = 19; i >= 0; i--) {
		if(k >= (1 << i)) {
			x = f[x][i];
			k -= (1 << i);
		}
	}
	return x;
}
int dis(int u, int v) {
    return dep[u] + dep[v] - 2 * dep[lca(u, v)];
}
void work() {
	int n;
	cin >> n;
	for(int i = 1; i < n; i++) {
		int x, y;
		cin >> x >> y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	dfs(1, 0);
	int m;
	cin >> m;
	for(int i = 0; i < m; i++) {
		int x, y, ans = 0;
		cin >> x >> y;
		if(dep[x] < dep[y]) swap(x, y);
		int fa = lca(x, y);
		int dl = dep[fa], d = dis(x, y);
		int dx = dep[x] - dl, dy = dep[y] - dl;
		if(x == y) {
			cout << n << '\n';
		} else if((d & 1) == 1) {
			cout << 0 << '\n';
		} else if(dep[x] == dep[y]) {
			int px = kthfa(x, dx-1), py = kthfa(y, dy-1);
			ans = n - sz[px] - sz[py];
			cout << ans << '\n';
		} else {
			int px = kthfa(x, d/2 - 1);
			ans = sz[f[px][0]] - sz[px];
			cout << ans << '\n';
		}
	}
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	int T = 1;
//	cin >> T;
	while(T--) {
		work();
	}
	return 0;
}
原文