E. Correct Placement

这篇博客讲述了如何使用C++解决Codeforces竞赛中的题目E.CorrectPlacement,涉及二维坐标排序和优化答案路径。作者首先定义了一个结构体node,包含高度、宽度、节点编号和答案,然后实现两个排序函数cmp_1和cmp_2。通过排序找出最优放置策略,最后输出每个节点的最终答案。
摘要由CSDN通过智能技术生成

Codeforces Round #693 (Div. 3)

E. Correct Placement

排序

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node{
    long long h, w;
    int n, ans;
};
bool cmp_1(node a, node b) {
    if (a.h == b.h) {
        return a.w > b.w;
    }
    else {
        return a.h < b.h;
    }
}
bool cmp_2(node a, node b) {
    return a.n < b.n;
}
int main() {
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<node> num(n);
        for (int i = 0; i < n; ++i) {
            cin >> num[i].h;
            cin >> num[i].w;
            num[i].ans = -1;
            num[i].n = i;
            if (num[i].h > num[i].w) {
                long long m = num[i].h;
                num[i].h = num[i].w;
                num[i].w = m;
            }
        }
        sort(num.begin(), num.end(), cmp_1);
        int min = 0;
        for (int i = 1; i < n; ++i) {
            if (num[i].w > num[min].w) {
                num[i].ans = num[min].n+1;
            }
            if (num[i].w < num[min].w) {
                min = i;
            }
        }
        sort(num.begin(), num.end(), cmp_2);
        for (int i = 0; i < n; ++i) {
            cout << num[i].ans << " ";
        }
        cout << endl;
    }
    return 0;
}

作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值