zoukankan      html  css  js  c++  java
  • 【例题 8-4 UVA

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

    在这里输入题意

    【题解】

    显然把问题分解成两个子问题。 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;
    }
    
  • 相关阅读:
    反射
    如何通过反射调用对象的方法?
    简述一下面向对象的”六原则一法则”。
    用Java写一个单例类。
    什么是UML?
    UML中有哪些常用的图?
    用Java写一个折半查找。
    两个对象值相同(x.equals(y) == true),但却可有不同的hash code,这句话对不对?
    构造器(constructor)是否可被重写(override)?
    用最有效率的方法计算2乘以8?
  • 原文地址:https://www.cnblogs.com/AWCXV/p/8182157.html
Copyright © 2011-2022 走看看