2024年天梯成信校赛
2024年天梯成信校赛
L1-1 代码胜于雄辩 - 2024年天梯成信校赛补题 (pintia.cn)
就用PHP
No PHP can be used in this contests
L1-2 收水费 - 2024年天梯成信校赛补题 (pintia.cn)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
if(n <= 100) cout << 2 * n << '\n';
else if(n < 500) cout << 200 + (n - 100) * 4 << '\n';
else cout << 10 * n << '\n';
return 0;
}
L1-3 日期 - 2024年天梯成信校赛补题 (pintia.cn)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
if(s.size() <6){
if(s.substr(0,2) <= "24") s = "20" + s;
else s = "19" + s;
}
cout << s.substr(0,4) << '-' << s.substr(4) << '\n';
return 0;
}
L1-4 回文数 - 2024年天梯成信校赛补题 (pintia.cn)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t --){
string s;
cin >>s;
bool ok = true;
for(int i = 0,j = s.size() - 1;i < j;i ++,j--){
if(s[i] != s[j]){
ok = false;
break;
}
}
cout << (ok ? "true" : "false") << '\n';
}
return 0;
}
L1-5 yihan的新函数 - 2024年天梯成信校赛补题 (pintia.cn)
数的奇偶性和其位数的奇偶性一致时,就从第二位往后依次变为\(0\),否则就是从第一位开始
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
i64 ans = 0;
while(n --){
string s;
cin >> s;
for(int i = ((stoi(s) & 1) == (s.size() & 1));i < s.size();i += 2){
s[i] = '0';
}
ans += stoi(s);
}
cout << ans << '\n';
return 0;
}
L1-6 二进制 - 2024年天梯成信校赛补题 (pintia.cn)
两字符串反转后正常模拟二进制,最后再反转一下
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string a, b;
cin >> a >> b;
const int N = 1e5 + 10;
vector<int> ans(N);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int n = max(a.size(), b.size()) + 1;
a += string(n-a.size(),'0');
b += string(n-b.size(),'0');
string res = "";
for(int i = 0;i <= n;i ++){
int now = (a[i] - '0') + (b[i] - '0') + ans[i];
ans[i + 1] += now / 2;
ans[i] = now % 2;
res += (ans[i] + '0');
}
while(res.back() == '0') res.pop_back();
reverse(res.begin(),res.end());
cout << res << '\n';
return 0;
}
L1-7 大山中的学院 - 2024年天梯成信校赛补题 (pintia.cn)
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
vector val(n, vector<int>(m));
vector<string> s(n);
for (auto &i : s) cin >> i;
vector<int> a(k);
for (auto &i : a) cin >> i;
int dex = 0, ans = 0, x, y;
int u[] = {1, -1, 0, 0}, v[] = {0, 0, 1, -1};
for (int i = 0; i < n; i ++) {
for (int j = 0; j < m; j ++) {
if (s[i][j] == '*') {
for (int p = 0; p < 4; p ++) {
int dx = i + u[p];
int dy = j + v[p];
if (dx >= 0 && dx < n && dy >= 0 && dy < m && s[dx][dy] == '-') {
val[dx][dy] += a[dex];
if (val[dx][dy] > ans) {
ans = val[dx][dy];
x = dx + 1, y = dy + 1;
}
}
}
dex ++;
}
}
}
cout << x << ' ' << y << '\n';
cout << ans << '\n';
return 0;
}
L1-8 堆积木 - 2024年天梯成信校赛补题 (pintia.cn)
模拟
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n),Top,num;
for(auto &i : a) cin >> i;
for(auto i : a){
if(Top.empty()){
Top.push_back(i);
num.push_back(1);
}else{
int now = INT_MAX, loc = -1,h = 0;
for(int j = 0;j < Top.size();j ++){
if(Top[j] > i && Top[j] <= now && num[j] > h){
now = Top[j];
loc = j;
h = num[j];
}
}
if(loc == -1){
Top.push_back(i);
num.push_back(1);
}else{
Top[loc] = i;
num[loc] ++;
}
}
}
int mx = 0;
for(auto i : num)
mx = max(i, mx);
cout << mx << ' ' << Top.size() << '\n';
return 0;
}
L2-1 买!买!买! - 2024年天梯成信校赛补题 (pintia.cn)
dfs判环,注意应该设置一个全局标记,而不是dfs返回布尔值(反正我写的返回布尔值写挂了
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin >> n >> m;
vector g(n + 1,vector<int>());
for(int i = 0;i < m;i ++){
int u,v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
int ans = 0, res = 0;
bool ok = false;
vector<bool> vis(n + 1);
auto dfs = [&](auto self,int u,int fa)->void{
vis[u] = true;
for(auto v : g[u]){
if(v == fa) continue;
if(vis[v]) {
ok = 1;
continue;
}
self(self,v,u);
}
};
for(int i = 1;i <= n;i ++){
if(!vis[i]){
ans ++;
dfs(dfs,i,0);
res += ok;
ok = 0;
}
}
cout << ans << ' ' << res << '\n';
return 0;
}
L2-2 洗牌&发牌 - 2024年天梯成信校赛补题 (pintia.cn)
数据不大,暴力神话
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
const string K[] = {"0", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
int n, m, k, p;
cin >> n >> k >> m >> p;
vector s(m + 1, vector<string>(k));
for (auto &i : s[0])
cin >> i;
for (int i = 1; i <= m; i ++) {
int l = k / 2, r = 0, cnt = 0;
while (cnt < k) {
s[i][cnt ++] = s[i - 1][l ++];
s[i][cnt ++] = s[i - 1][r ++];
}
}
vector<string> ans;
for (int i = p - 1, x = 0; i < k && x < 4; i += n, x ++) {
ans.push_back(s[m][i]);
}
if (ans.size() < 4) cout << "Error:cards not enough\n";
else {
for (auto v : ans) {
int num = 0, cnt = 0;
while (v[cnt] >= '0' && v[cnt] <= '9')
num = num * 10 + (v[cnt] - '0'), cnt ++;
cout << K[num] << v.back() << '\n';
}
}
return 0;
}
L2-3 Gwen的小剪刀 - 2024年天梯成信校赛补题 (pintia.cn)
先二分找到满足成一棵树的最小美观度;
然后就是按照快乐感排序做最小生成树;
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
struct Node{
int u,v,w,c;
};
struct UFS {
int sz;
vector<int> rank, p;
void link(int x, int y) {
if (x == y)
return;
if (rank[x] > rank[y])
p[y] = x;
else
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
void init(int n) {
sz = n;
rank.resize(n + 1);
p.resize(n + 1);
for (int i = 0; i <= sz; i++) {
p[i] = i;
rank[i] = 0;
}
}
int find(int x) {
return x == p[x] ? x : (p[x] = find(p[x]));
}
void unin(int x, int y) {
link(find(x), find(y));
}
void compress() {
for (int i = 0; i < sz; i++)
find(i);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin >> n >> m;
vector<Node> Edge(m);
for(int i = 0;i < m;i ++){
int u,v,w,c;
cin >> u >> v >> w >> c;
Edge[i] = {u,v,w,c};
}
sort(Edge.begin(),Edge.end(),[&](Node a, Node b){
return a.w < b.w;
});
auto check = [&](i64 x){
UFS ufs;
ufs.init(n);
int cnt = 0;
for(auto [u,v,w,c] : Edge){
if(w < x) continue;
u = ufs.find(u), v = ufs.find(v);
if(u != v){
ufs.unin(u,v);
cnt ++;
}
}
return cnt == n - 1;
};
i64 l = 1, r = 1e9, mi = 1;
while(l <= r){
i64 mid = (l + r) >> 1;
if(check(mid)) l = mid + 1, mi = mid;
else r = mid - 1;
}
sort(Edge.begin(),Edge.end(),[&](Node a,Node b){
return a.c < b.c;
});
UFS ufs;
ufs.init(n);
i64 ans = 0;
for(int i = 0;i < m;i ++){
auto [u,v,w,c] = Edge[i];
if(w < mi) continue;
u = ufs.find(u),v = ufs.find(v);
if(u != v){
ufs.unin(u,v);
ans += c;
}
}
cout << mi << '\n' << ans << '\n';
return 0;
}
L2-4 超时空之恋 - 2024年天梯成信校赛补题 (pintia.cn)
多层图最短路,按照过去、现在,未来建立三层图,以及每层图之间的可行边;
城镇出现的时间随机,需要排序;
对于某城镇地牢,如果到达该城镇的时间小于地牢出现时间,可以等着地牢出现,也就最短时间是地牢的出现时间,如果到达城镇的时间在该地牢和下一个地牢出现之间,那么最短时间就是到达时间;
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<'\n';
using namespace std;
using i64 = long long;
typedef pair<i64, i64> PII;
struct DIJ {
using i64 = long long;
using PII = pair<i64, i64>;
vector<i64> dis;
vector<vector<PII>> G;
DIJ() {}
DIJ(int n) {
dis.assign(n + 1, 1e18);
G.resize(n + 1);
}
void add(int u, int v, int w) {
G[u].emplace_back(v, w);
}
void dijkstra(int s) {
priority_queue<PII, vector<PII>, greater<PII>> que;
dis[s] = 0;
que.push({0, s});
while (!que.empty()) {
auto p = que.top();
que.pop();
int u = p.second;
if (dis[u] < p.first) continue;
for (auto [v, w] : G[u]) {
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
que.push({dis[v], v});
}
}
}
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, k;
cin >> n >> m >> k;
DIJ dij(4 * n);
for (int i = 0; i < m; i ++) {
int x, u, v, w;
cin >> x >> u >> v >> w;
u += x * n, v += x * n;
dij.add(u, v, w);
dij.add(v, u, w);
}
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= 2; j ++) {
dij.add(i, i + j * n, k);
dij.add(i + j * n, i, k);
}
dij.dijkstra(1);
int T;
cin >> T;
vector<PII> ab(T);
for (auto &[x, y] : ab)
cin >> y >> x;
sort(ab.begin(), ab.end());
ab.push_back({LLONG_MAX, LLONG_MAX});
i64 ans = 1e18;
for (int i = 0; i < T; i ++) {
auto [x, y] = ab[i];
if (dij.dis[y] < x) {
ans = min(ans, x);
} else if (dij.dis[y] < ab[i + 1].first) {
ans = min(ans, dij.dis[y]);
}
}
if (ans == 1e18) cout << "-1\n";
else cout << ans << '\n';
return 0;
}