PTA 520 钻石争霸赛 2021
比赛链接:https://pintia.cn/problem-sets/1392022091148099584/problems/type/7
7-1 自动编程 (5 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x;
cin >> x;
cout << "print(" << x << ")" << "\n";
return 0;
}
7-2 加油冲鸭 (10 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, s;
cin >> n >> m >> s;
int left = n - m * s;
if (left > n / 2) {
cout << "hai sheng " << left << " mi! jia you ya!" << "\n";
} else {
cout << "hai sheng " << left << " mi! chong ya!" << "\n";
}
return 0;
}
7-3 520的表白 (10 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
for (int i = 0; i < 520; i++) {
cout << s << "\n";
}
return 0;
}
7-4 奇葩楼层 (15 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, d;
cin >> n >> d;
int sub = 0;
for (int i = 1; i <= n; i++) {
if (to_string(i).find('0' + d) != string::npos) {
++sub;
}
}
cout << n - sub << "\n";
return 0;
}
7-5 大勾股定理 (15 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int ans = 3, step = 7;
for (int i = 1; i < n; i++) {
ans += step;
step += 4;
}
for (int i = 0; i < n + 1; i++) {
cout << ans++ << "^2" << (i == n ? " =\n" : " + ");
}
for (int i = 0; i < n; i++) {
cout << ans++ << "^2" << (i == n - 1 ? "\n" : " + ");
}
return 0;
}
7-6 矩阵列平移 (20 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, k, x;
cin >> n >> k >> x;
vector<vector<int>> a(n, vector<int> (n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> a[i][j];
}
}
vector<vector<int>> b(n, vector<int> (n));
int val_k = 0;
for (int j = 0; j < n; j++) {
if (j & 1) {
for (int i = 0; i <= val_k; i++) {
b[i][j] = x;
}
for (int i = val_k + 1; i < n; i++) {
b[i][j] = a[i - val_k - 1][j];
}
val_k = (val_k + 1) % k;
} else {
for (int i = 0; i < n; i++) {
b[i][j] = a[i][j];
}
}
}
for (int i = 0; i < n; i++) {
cout << accumulate(b[i].begin(), b[i].end(), 0) << " \n"[i == n - 1];
}
return 0;
}
7-7 约会大作战 (20 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q;
cin >> n >> m >> q;
vector<vector<int>> a(n, vector<int> (m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
vector<vector<int>> b(m, vector<int> (n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
cin >> b[i][j];
}
}
vector<vector<int>> a_vised(n), b_vised(m);
vector<bool> a_yes(n), b_yes(m);
vector<pair<int, int>> date;
for (int i = 0; i < q; i++) {
int x, y;
cin >> x >> y;
--x, --y;
a_vised[x].push_back(a[x][y]);
b_vised[y].push_back(b[y][x]);
if (not a_yes[x] and not b_yes[y] and a_vised[x].size() >= 3 and b_vised[y].size() >= 3) {
int j = a_vised[x].size(), k = b_vised[y].size();
if (a_vised[x][j - 1] > a_vised[x][j - 2] and a_vised[x][j - 1] > a_vised[x][j - 3] and b_vised[y][k - 1] > b_vised[y][k - 2] and b_vised[y][k - 1] > b_vised[y][k - 3]) {
a_yes[x] = b_yes[y] = true;
date.emplace_back(x, y);
}
}
}
if (date.size()) {
for (auto pr : date) {
cout << pr.first + 1 << ' ' << pr.second + 1 << "\n";
}
} else {
cout << "PTA is my only love" << "\n";
}
return 0;
}
7-8 浪漫侧影 (25 分)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> InOrder(n);
for (int i = 0; i < n; i++) {
cin >> InOrder[i];
}
vector<int> PostOrder(n);
for (int i = 0; i < n; i++) {
cin >> PostOrder[i];
}
map<int, int> lson, rson, vis;
function<int(int, int)> Build_BinaryTree = [&](int l, int r) {
if (l > r) {
return -1;
}
int root = PostOrder[r];
vis[root] = true;
int mid = find(InOrder.begin(), InOrder.end(), root) - InOrder.begin();
int l1 = INT_MAX, r1 = INT_MIN;
for (int i = mid - 1; i >= 0; i--) {
if (vis[InOrder[i]]) {
break;
}
int pos = find(PostOrder.begin(), PostOrder.end(), InOrder[i]) - PostOrder.begin();
l1 = min(l1, pos), r1 = max(r1, pos);
}
lson[root] = Build_BinaryTree(l1, r1);
int l2 = INT_MAX, r2 = INT_MIN;
for (int i = mid + 1; i < n; i++) {
if (vis[InOrder[i]]) {
break;
}
int pos = find(PostOrder.begin(), PostOrder.end(), InOrder[i]) - PostOrder.begin();
l2 = min(l2, pos), r2 = max(r2, pos);
}
rson[root] = Build_BinaryTree(l2, r2);
return root;
};
vector<vector<int>> LayerOrder(n);
function<void(int, int)> Get_LayerOrder = [&](int root, int dep) {
if (root != -1) {
LayerOrder[dep].push_back(root);
if (lson[root] != -1) {
LayerOrder[dep + 1].push_back(lson[root]);
}
if (rson[root] != -1) {
LayerOrder[dep + 1].push_back(rson[root]);
}
Get_LayerOrder(lson[root], dep + 1);
Get_LayerOrder(rson[root], dep + 1);
}
};
Get_LayerOrder(Build_BinaryTree(0, n - 1), 0);
vector<int> L, R;
for (auto vec : LayerOrder) {
if (vec.size()) {
L.push_back(vec.front());
R.push_back(vec.back());
}
}
cout << "R: ";
for (int i = 0; i < (int)R.size(); i++) {
cout << R[i] << " \n"[i == (int)R.size() - 1];
}
cout << "L: ";
for (int i = 0; i < (int)L.size(); i++) {
cout << L[i] << " \n"[i == (int)L.size() - 1];
}
return 0;
}