雅礼cch杯 round 3总结
T3MLE了我心态炸了😭😭😭😭😭😡😡😡😡😡
T1
记录
思路
竟然还有小学数学。
第一眼看到这个式子:
其中
思路
首先对于往四周跑,直接弄就行了,没技术含量。然后考虑打枪瞬移(鉴定为把自己打出去了),直接往四个方向延伸,然后碰到墙就退出,并把所有的即将碰到墙但还没碰到墙的位置存下来,至于为什么不直接存墙的位置,是因为不然你也不知道你会从哪个方向出来。然后对于这四个位置,两两枚举,然后让当前坐标于其中一个连边,边权为到达另外一个位置的距离加一(你还要瞬移)。边建完了,就直接跑最短路即可。
但是,因为通道会保留两次,难道这么弄不会导致答案错误?其实不会,分两种情况考虑:新打了一个通道,然后往新/旧里面钻。先考虑往旧的里面钻,因为打枪没有代价,所以可以直接打出一个新通道(即朝旧通道再开一枪),不需要保留之前的,因为这样效果不变。然后如果往新的里面钻,那么你会回到旧的通道外,还不如不进去。复杂度
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2010;
int n, m, dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}, sx, sy, tx, ty, vis[N * N], dis[N * N];
string s[N];
vector<pair<int, int> > E[N * N];
int id(int i, int j) {
return (i - 1) * m + j;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> s[i];
s[i] = " " + s[i];
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(s[i][j] != '#') {
for(int k = 0; k < 4; k++) {
int ni = i + dx[k], nj = j + dy[k];
if(ni < 1 || ni > n || nj < 1 || nj > m) {
continue;
}
if(s[ni][nj] != '#') {
E[id(i, j)].push_back({id(ni, nj), 1});
}
}
if(s[i][j] == 'C') {
sx = i;
sy = j;
}
if(s[i][j] == 'F') {
tx = i;
ty = j;
}
vector<int> node;
for(int k = 0; k < 4; k++) {
int si = i + dx[k], sj = j + dx[k], ni = i, nj = j;
if(si < 1 || si > n || sj < 1 || sj > m) {
continue;
}
while(1) {
if(s[ni + dx[k]][nj + dy[k]] == '#') {
node.push_back(id(ni, nj));
break;
}
ni += dx[k];
nj += dy[k];
}
}
for(int x = 0; x < node.size(); x++) {
for(int y = x + 1; y < node.size(); y++) {
E[id(i, j)].push_back({node[y], abs(i - (node[x] + m - 1) / m) + abs(j - (node[x] % m ? node[x] % m : m)) + 1});
E[id(i, j)].push_back({node[x], abs(i - (node[y] + m - 1) / m) + abs(j - (node[y] % m ? node[y] % m : m)) + 1});
}
}
}
}
}
for(int i = 1; i <= n * m; i++) {
dis[i] = 1e18;
}
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
q.push({0, id(sx, sy)});
dis[id(sx, sy)] = 0;
while(!q.empty()) {
int x = q.top().second;
q.pop();
if(vis[x]) {
continue;
}
vis[x] = 1;
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i].first, w = E[x][i].second;
if(dis[y] > dis[x] + w) {
dis[y] = dis[x] + w;
q.push({dis[y], y});
}
}
}
if(dis[id(tx, ty)] == 1e18) {
cout << "nemoguce\n";
}
else {
cout << dis[id(tx, ty)]<< "\n";
}
return 0;
}
T3
记录
我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了我MLE了
思路
看到这么小的时间(我想知道然后会怎么样,所以可以直接抛掉中间位置,只保留两端。然后转移方程就非常好推(但是太多不好写上去)。但是,这不是 注意空间注意空间注意空间复杂度
#include <bits/stdc++.h>
using namespace std;
const int N = 2010, M = 110;
int n, k, m, dp[M][M][N][2], a[N], b[N], c[N], w[M][N], lsh[N], lshc, ans;
// dp[l][r][t][0/1] 代表当前访问过的区间为[l,r],用时为t,当前位置在最左侧/最右侧时的最大分数
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> m;
for(int i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i];
lsh[++lshc] = a[i];
}
lsh[++lshc] = k;
sort(lsh + 1, lsh + lshc + 1);
lshc = unique(lsh + 1, lsh + lshc + 1) - lsh - 1;
for(int i = 1; i <= m; i++) {
a[i] = lower_bound(lsh + 1, lsh + lshc + 1, a[i]) - lsh;
for(int j = 1; j <= c[i]; j++) {
w[a[i]][j] += b[i];
}
}
k = lower_bound(lsh + 1, lsh + lshc + 1, k) - lsh;
memset(dp, -1, sizeof dp);
dp[k][k][1][0] = dp[k][k][1][1] = w[k][1];
for(int len = 1; len <= lshc; len++) {
for(int l = 1; l + len - 1 <= lshc; l++) {
int r = l + len - 1;
for(int t = 1; t <= 2000; t++) {
if(t + (lsh[r] - lsh[l]) <= 2000) {
dp[l][r][t + (lsh[r] - lsh[l])][0] = max(dp[l][r][t + (lsh[r] - lsh[l])][0], dp[l][r][t][1]);
dp[l][r][t + (lsh[r] - lsh[l])][1] = max(dp[l][r][t + (lsh[r] - lsh[l])][1], dp[l][r][t][0]);
}
dp[l][r][t][0] = max(dp[l][r][t][0], dp[l][r][t - 1][0]);
dp[l][r][t][1] = max(dp[l][r][t][1], dp[l][r][t - 1][1]);
}
for(int t = 1; t <= 2000; t++) {
ans = max(ans, max(dp[l][r][t][0], dp[l][r][t][1]));
if(l > 1) {
if(t + (lsh[l] - lsh[l - 1]) <= 2000 && dp[l][r][t][0] != -1) {
dp[l - 1][r][t + (lsh[l] - lsh[l - 1])][0] = max(dp[l - 1][r][t + (lsh[l] - lsh[l - 1])][0], dp[l][r][t][0] + w[l - 1][t + (lsh[l] - lsh[l - 1])]);
}
if(t + (lsh[r] - lsh[l - 1]) <= 2000 && dp[l][r][t][1] != -1) {
dp[l - 1][r][t + (lsh[r] - lsh[l - 1])][0] = max(dp[l - 1][r][t + (lsh[r] - lsh[l - 1])][0], dp[l][r][t][1] + w[l - 1][t + (lsh[r] - lsh[l - 1])]);
}
}
if(r < lshc) {
if(t + (lsh[r + 1] - lsh[r]) <= 2000 && dp[l][r][t][1] != -1) {
dp[l][r + 1][t + (lsh[r + 1] - lsh[r])][1] = max(dp[l][r + 1][t + (lsh[r + 1] - lsh[r])][1], dp[l][r][t][1] + w[r + 1][t + (lsh[r + 1] - lsh[r])]);
}
if(t + (lsh[r + 1] - lsh[l]) <= 2000 && dp[l][r][t][0] != -1) {
dp[l][r + 1][t + (lsh[r + 1] - lsh[l])][1] = max(dp[l][r + 1][t + (lsh[r + 1] - lsh[l])][1], dp[l][r][t][0] + w[r + 1][t + (lsh[r + 1] - lsh[l])]);
}
}
}
}
}
cout << ans << "\n";
return 0;
}
T4
记录
前三题(主要第二题)用时太长了,会写也没时间了。
思路
看懂题面是这题最难的
题目的意思就是让你选择一个点作为一棵树的根节点,然后使代价最小。代价为需要消耗的总体力值,然后走一条边消耗的体力值为边权减去深度小的那个点的点权,然后第一次走一条边会被弹回根节点。
那这不就是再明显不过的换根 DP 吗?首先一条边经过的次数为以这个边连接的两点中深度大的那个点为根的子树的大小(每一个点都有一条连向父节点的边,都需要走一遍),累积起来就是整棵树的代价。然后换根时,权值发生变动的就只有连接这两个点的边,因为只有这两个点父子关系发生了变换。
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 10;
int n, a[N], sz[N], dp[N], ans1 = 1e18, ans2;
vector<pair<int, int>> E[N];
void dfs1(int x, int fa) {
sz[x] = 1;
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i].first, w = E[x][i].second;
if(y == fa) {
continue;
}
dfs1(y, x);
dp[x] += dp[y] + sz[y] * max(0ll, w - a[x]);
sz[x] += sz[y];
}
}
void dfs2(int x, int fa) {
for(int i = 0; i < E[x].size(); i++) {
int y = E[x][i].first, w = E[x][i].second;
if(y == fa) {
continue;
}
dp[y] = dp[x] - sz[y] * max(0ll, w - a[x]) + (n - sz[y]) * max(0ll, w - a[y]);
dfs2(y, x);
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
E[u].push_back({v, w});
E[v].push_back({u, w});
}
dfs1(1, 0);
dfs2(1, 0);
for(int i = 1; i <= n; i++) {
if(ans1 > dp[i]) {
ans2 = i;
ans1 = dp[i];
}
}
cout << ans2 << "\n" << ans1 << "\n";
return 0;
}
总结
-
做题不够细致,把数组开大了。
-
还是写代码太慢了,需要改进代码的结构(比如上面的T2T3两个屎山代码)