Denso Create Programming Contest 2024(AtCoder Beginner Contest 361)

A - Insert 

思路:

大水题

code:

#include <iostream>
using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k, x; cin >> n >> k >> x;
    for (int i = 1; i <= n; i ++ ) {
        int a; cin >> a;
        cout << a << ' ';
        if (i == k) cout << x << ' ';
    }
    return 0;
}

B - Intersection of Cuboids 

思路:

判断一个立体,我们可以转化到三个坐标轴上。那只要判断每个坐标轴上,两者是否相交即可。

code:

#include <bits/stdc++.h>
using namespace std;
int main() {
	vector<int> a(12);
	for (int i = 0; i < 12; i ++ ) cin >> a[i];
	for (int i = 0; i < 3; i ++ ) {
		int p = max(a[i], a[i + 6]);
		int q = min(a[i + 3], a[i + 9]);
		if (p >= q) return cout << "No\n", 0;
	}
	cout << "Yes\n";
	return 0;
}

C - Make Them Narrow 

思路:

滑动窗口

code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n, k; cin >> n >> k;
    vector<ll> a(n + 1);
    for (int i = 1; i <= n; i ++ ) cin >> a[i];
    sort(a.begin() + 1, a.end());
    ll ans = a[n - k] - a[1];
    for (int i = 1; i <= k; i ++ ) {
        ans = min(a[i + n - k] - a[i + 1], ans);
    }
    cout << ans << endl;
    return 0;
}

D - Go Stone Puzzle

思路:

数据很小,最差是阶乘的复杂度,还是符合题意的。可以直接bfs暴力。

code:

typedef pair<string, int> PSI;
inline void solve() {
     int n; cin >> n;
     string s, t; cin >> s >> t;
     s += "  ", t += "  ";
     map<string, bool> vis;
     queue<PSI> q;
     int ans = -1;
     q.push({s, 0});
     vis[s] = true;
     while (q.size()) {
     	auto p = q.front(); q.pop();
     	if (p.first == t) {
     		if (ans == -1) {
     			ans = p.second;
     		}else {
     			ans = min(ans, p.second);
     		}
     	}
     	for (int i = 0; i < n + 1; i ++ ) {
     		string temp = p.first;
     		if (temp[i] != ' ' && temp[i + 1] != ' ') {
     			for (int j = 0; j < n + 1; j ++ ) {
     				if (temp[j] == ' ') {
     					temp[j] = temp[i];
     					temp[j + 1] = temp[i + 1];
     					temp[i] = ' ';
     					temp[i + 1] = ' ';
     					if (!vis[temp]) {
     						q.push({temp, p.second + 1});
     						vis[temp] = true;
     					}
     					break;
     				}
     			}
     		}
     	}
     }
     cout << ans << endl;
	 return;
}

E - Tree and Hamilton Path 2

思路:

我们从任意一个点出发,假设有3条相连的边,那么就会有两条走到叶节点再回到起点,走了两倍长度,而另一条只需走到叶节点。

那么我们就考虑最长的可以少走的距离,它是树的直径。

code:

inline void solve() {
     int n; cin >> n;
     vector<vector<int>> e(n + 1);
     vector<ll> d(n + 1);
     map<PII, int> edge;
     ll sum = 0;
     for (int i = 0; i < n - 1; i ++ ) {
     	int a, b, v; cin >> a >> b >> v;
     	sum += 2 * (ll)v;
     	e[a].push_back(b), e[b].push_back(a);
     	edge[{min(a, b), max(a, b)}] = v;
     }
     int c = 0;
     function<void(int ,int)> dfs = [&](int u, int pre) {
     	for (auto v : e[u]) {
     		if (v == pre) continue;
     		d[v] = d[u] + edge[{min(u, v), max(u, v)}];
     		if (d[v] > d[c]) c = v;
     		dfs(v, u);
     	}
     };
     dfs(1, 0);
     d[c] = 0; dfs(c, 0);
     cout << sum - d[c] << endl;
	 return;
}

 

 

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值