【例题 8-4 UVA - 11134】Fabled Rooks

【链接】 我是链接,点我呀:)
【题意】


在这里输入题意

【题解】


显然把问题分解成两个子问题。
x轴和y轴分别做。
即n个点要求第i个点在[li,ri]范围内。(ri<=n)
问是否可行.

按左端点、右端点排。尽量取左边的方法是错的。
hack数据:(1,1),(1,3),(2,2)
在安排idx=2的时候,优先用了(1,3)这个区间。导致原本3可以放的。现在放不了了。

所以我们的方法就是。
对于第i个点。
找一个能包含它。
但是右端点又尽量小的区间。
这样,能保证这个选取的区间尽量不影响到后面的点的选取。
(而前面的点无所谓,因为已经安排完了
O(N^2)的复杂度找这样的区间就可以了。

如果数据大的话。
可以考虑用set来处理:比如区间[li,ri]
则可以在li的位置插入一个数据ri
当ri<idx的时候,则在set中把ri去掉。
否则每次都找set中最小的ri.把它删掉。

【代码】

/*
    1.Shoud it use long long ?
    2.Have you ever test several sample(at least therr) yourself?
    3.Can you promise that the solution is right? At least,the main ideal
    4.use the puts("") or putchar() or printf and such things?
    5.init the used array or any value?
    6.use error MAX_VALUE?
    7.use scanf instead of cin/cout?
    8.whatch out the detail input require
*/
/*
    一定在这里写完思路再敲代码!!!
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 5000;

vector < pair<int,pair<int,int> > > v1,v2;
int n;
int x[N+10],y[N+10];

bool ok(vector<pair<int,pair<int,int> > > v,int ju){
    for (int i = 1;i <= n;i++){
        int mi = 100000;
        __typeof v.begin() idx;
        for (auto j = v.begin();j!=v.end();j++){
            auto temp = *j;
            if (temp.first<=i && i<=temp.second.first){
                if (temp.second.first<mi){
                    mi = temp.second.first;
                    idx = j;
                }
            }
        }
        if (mi==100000) return false;
        if (ju==0){
            x[(*idx).second.second] = i;
        }else{
            y[(*idx).second.second] = i;
        }
        v.erase(idx);
    }
    return true;
}

int main(){
    #ifdef LOCAL_DEFINE
        freopen("rush_in.txt", "r", stdin);
    #endif
    ios::sync_with_stdio(0),cin.tie(0);
    while (cin >>n && n){
        v1.clear(),v2.clear();
        for (int i = 1;i <= n;i++){
            int xl,yl,xr,yr;
            cin >> xl >> yl >> xr >> yr;
            v1.push_back({xl,{xr,i}});
            v2.push_back({yl,{yr,i}});
        }
        if (ok(v1,0)&&ok(v2,1)){
            for (int i = 1;i <= n;i++)
                cout << x[i] <<' '<<y[i]<<endl;
        }else{
            cout <<"IMPOSSIBLE"<<endl;
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/8182157.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值