总结
今天的模拟赛太抽象了,难度难评,但坑是真的多!!!
T1
有 元 个和 元 个的苹果,要求花最少的钱 至少 买 个苹果。没看到 至少 ,少了 。
错误代码:
#include <bits/stdc++.h>
using namespace std;
int n, m, a, b;
int main()
{
cin >> n >> m >> a >> b;
if (a * m <= b)
{
cout << a * n << endl;
}
else
{
cout << n / m * b + (n % m) * a << endl;
}
return 0;
}正确代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, a, b;
signed main()
{
cin >> n >> m >> a >> b;
if (a * m <= b)
{
cout << a * n << endl;
}
else
{
cout << n / m * b + min(b, (n % m) * a) << endl;
}
return 0;
}T2
题目大意:给定一天的日期,求这一天 至 有多少天,其中 为整数且 ,一道恶心的模拟题,要注意的边界问题很多。
代码如下:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int y, m, d;
int s[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
signed main()
{
cin >> y >> m >> d;
int nxt = (int)sqrt(y) + 1;
nxt *= nxt;
int now = (y - 1) * 365LL;
if (y > 400)
now += (y - 1) / 400;
if (y > 100)
{
now += (y - 1) / 100 * 24;
y -= (y - 1) / 100 * 100;
}
now += (y - 1) / 4;
now += s[m - 1] + d;
if (m > 2 && (y % 400 == 0 || (y % 4 == 0 && y % 100)))
now ++ ;
int target = (nxt - 1) * 365;
if (nxt > 400)
target += (nxt - 1) / 400;
if (nxt > 100)
{
target += (nxt - 1) / 100 * 24;
nxt -= (nxt - 1) / 100 * 100;
}
target += (nxt - 1) / 4;
cout << target - now + 1LL << endl;
return 0;
}T3
一道比较简单的二分,但是会爆 long long ,注意一下就可以了。
代码如下:
#include <bits/stdc++.h>
#define int long long
using namespace std;
using LL = __int128_t;
const int N = 100010;
int n, k;
struct Type
{
int a, b;
bool operator <(const Type& other) const
{
return b * other.a < other.b * a;
}
} arr[N];
bool check(int mid)
{
LL sum = 0;
for (int i = 1; i <= n && (LL)mid * arr[i].a > arr[i].b; i ++ )
sum += (LL)arr[i].a * mid - arr[i].b;
return sum <= k;
}
signed main()
{
cin >> n >> k;
for (int i = 1; i <= n; i ++ ) cin >> arr[i].a;
for (int i = 1; i <= n; i ++ ) cin >> arr[i].b;
sort(arr + 1, arr + n + 1);
int l = arr[1].b / arr[1].a, r = 2e18;
while (l < r)
{
int mid = (l + r + 1) >> 1;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << r << endl;
return 0;
}T4
题目大意:给定 个节点的一棵树, 次询问,求从 这样的 在这棵树上有多少个。考察 LCA ,逻辑稍稍复杂,但还是不难的,由于代码又长又臭,小错误特别多,我因为右移写成左移以及距离边界问题痛失 !!!
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010, M = 20;
int n, Q;
int head[N], cur;
struct Edge
{
int to, nxt;
} edge[N << 1];
int f[N][M], lg[N], Size[N], dep[N];
inline void add(int u, int v)
{
edge[cur] = {v, head[u]};
head[u] = cur ++ ;
}
void dfs(int u, int p, int d)
{
f[u][0] = p, Size[u] = 1, dep[u] = d;
for (int i = head[u]; ~i; i = edge[i].nxt)
{
int j = edge[i].to;
if (j == p) continue;
dfs(j, u, d + 1);
Size[u] += Size[j];
}
}
inline void init()
{
lg[1] = 0;
for (int i = 2; i <= n; i ++ ) lg[i] = lg[i >> 1] + 1;
for (int j = 1; j < M; j ++ )
for (int i = 1; i <= n; i ++ )
f[i][j] = f[f[i][j - 1]][j - 1];
}
int LCA(int x, int y)
{
if (dep[x] < dep[y]) swap(x, y);
while (dep[x] != dep[y])
{
int k = lg[dep[x] - dep[y]];
x = f[x][k];
}
if (x == y) return x;
for (int j = M - 1; ~j; j -- )
if (f[x][j] != f[y][j])
{
x = f[x][j];
y = f[y][j];
}
return f[x][0];
}
int g(int u, int d)
{
for (int j = M - 1; ~j; j -- )
if (dep[f[u][j]] >= d)
u = f[u][j];
return u;
}
int main()
{
scanf("%d", &n);
memset(head, -1, sizeof head);
for (int i = 1; i < n; i ++ )
{
int u, v; scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
dfs(1, 0, 1);
init();
scanf("%d", &Q);
while (Q -- )
{
int x, y; scanf("%d%d", &x, &y);
if (x == y)
{
printf("%d\n", n);
continue;
}
int root = LCA(x, y);
int d = dep[x] + dep[y] - 2 * dep[root];
if (d % 2)
{
puts("0");
continue;
}
d >>= 1;
if (dep[x] < dep[y]) swap(x, y);
if (x == root || y == root)
{
int mid = g(x, dep[x] - d + 1);
printf("%d\n", Size[f[mid][0]] - Size[mid]);
}
else
{
if (dep[x] == dep[y])
{
int a = g(x, dep[root] + 1), b = g(y, dep[root] + 1);
printf("%d\n", n - Size[a] - Size[b]);
continue;
}
int mid = g(x, dep[x] - d + 1);
printf("%d\n", Size[f[mid][0]] - Size[mid]);
}
}
return 0;
}反思
关于计算一定要充分考虑所有情况,也不要犯一些小错误,一定要通读代码,考虑每个字符的作用!!!