codeforces Hello 2021
A. Stable Arrangement of Rooks
类似一个八皇后问题,不过增加了一个条件,每个棋子移动一步后仍需满足要求。所以每个棋子之间应该间隔一行一列。我们只需要在二维数组中不断添加棋子,最后判断添加棋子数量是否和数据相等就知道能否建立一个stable arrangement。
#include<bits/stdc++.h>
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 50;
int t, n, k;
bool chess[N][N];
int main() {
io_opt;
cin >> t;
while (t--) {
cin >> n >> k;
int num = 0, x = 1, y = 1;
memset(chess, 0, sizeof(chess));
while (x <= n && y <= n && num < k) {
chess[x][y] = true;
x += 2, y += 2;
num++;
}
if (num < k) cout << -1 << endl;
else {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!chess[i][j]) cout << '.';
else cout << 'R';
}
cout << endl;
}
}
}
return 0;
}
B. Integers Shop
第二题的大意是,题目会给你n个线段,并且告诉你线段的左右端点和它的价格,要求你获得最长的线段,并且价格最小。分析题目的意思,我们知道会出现以下情况:
- 一个最长的线段,左端点为x1,右端点为x2。
- 两个线段,第一个线段左端点为x1,第二个线段右端点为x2。(题意中两个线段中间我们也会得到)
我们只需分别记录左端点,和右端点位置及其价格,还有最长线段的长度价格进行判断就行了。
#include<bits/stdc++.h>
#define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define int_inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6 + 10;
int t, n;
int main() {
io_opt;
cin >> t;
while (t--) {
cin >> n;
ll mnp = 1e18, mxp = 0, L = 0, R = 0, LR = 0;
for (int i = 1; i <= n; i++) {
ll l, r, c;
cin >> l >> r >> c;
if (l < mnp) L = c, LR = 1e18, mnp = l;
if (r > mxp) R = c, LR = 1e18, mxp = r;
if (l == mnp && r == mxp) LR = min(LR, c);
if (l == mnp) L = min(L, c);
if (r == mxp) R = min(R, c);
cout << min(L + R, LR) << endl;
}
}
return 0;
}