codeforces Hello 2021

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个线段,并且告诉你线段的左右端点和它的价格,要求你获得最长的线段,并且价格最小。分析题目的意思,我们知道会出现以下情况:

  1. 一个最长的线段,左端点为x1,右端点为x2。
  2. 两个线段,第一个线段左端点为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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值