URAL 2041 Nanomatryoshkas(貪心)

題意:給出n個俄羅斯套娃,分別有內體積和外體積,要一個套住另外一個必須內體積要大於另一個的外體積,問你能不能找出一個排列使得每一個都不能套住前面一個。對於一個套娃,保證外體積大於等於內體積

做法:我們先按照外體積降序排序,相同按照內體積降序排序,顯然可以發覺對於外體積不同的來說,這個排列必定可以滿足題目意思,因爲內體積肯定小於等於自己的外體積的,自然也就小於比自己外體積大的前面一個套娃的外體積了。但是對於外體積相同的來說呢?可以發現只有內體積等於外體積的纔會出現不滿足題意,爲了滿足題意,我們需要把這個套娃往上放到內體積小於這個套娃外體積的上面,所以用一個set去維護下套娃的內體積和標號,而對於內體積和外體積相同的好幾個套娃需要一起操作,因爲放的時候,這些套娃並不能放在一起。

AC代碼:

#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 51123987
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.end())
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b; }
template<class T> inline T Min(T a, T b){ return a < b ? a : b; }

const int maxn = 100000+10;
struct node
{
    int a,b,id;
}no[maxn],no2[maxn];
bool cmp1(node t1, node t2)
{
    return t1.b == t2.b ? t1.a > t2.a : t1.b > t2.b;
}
struct cmp2
{
    bool operator()(node t1, node t2)
    {
        return t1.a == t2.a ? t1.id < t2.id : t1.a < t2.a;
    }
};

vector<int> v[maxn];
set<node,cmp2>s;
set<node,cmp2>::iterator it;

node make_node(int a, int pos)
{
    node t; t.a = a; t.id = pos;
    return t;
}

int nct,n;
bool flag[maxn];
void dfs(int x)
{
    flag[x] = 1;
    int sz = v[x].size();
    for(int i = 0; i < sz; i++) if(flag[v[x][i]] == 0)
    {
        dfs(v[x][i]);
    }
    printf("%d",no[x].id);
    if(nct == n) printf("\n");
    else printf(" ");
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    while(~scanf("%d",&n))
    {
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d",&no[i].a,&no[i].b);
            no2[i].a = no[i].a; no2[i].b = no[i].b;
            no[i].id = i;
            v[i].clear();
        }
        memset(flag,0,sizeof(flag));
        sort(no+1,no+1+n,cmp1);
//        for(int i = 1; i <= n; i++)
//            cout<<no[i].a<<" "<<no[i].b<<" "<<no[i].id<<endl;
        s.clear();
        s.insert(make_node(-1,-1));
        int pp = 0, gg = 1;
        for(int i = 1; i <= n; i++)
        {
            if(pp == 0 && no[i].a == no[i].b)
            {
                pp = no[i].a;
                s.insert(make_node(no[i].a,i));
            }
            else if(pp == no[i].a && no[i].a == no[i].b)
            {
                it = s.lower_bound(make_node(no[i].b,0));
                int woqu = 0;
                while(1)
                {
                    if(pp != no[i].a || no[i].a != no[i].b)
                    {
                        i--;
                        break;
                    }
                    it--;
                    if(it->a == -1)
                    {
                        gg = 0;
                        break;
                    }
                    woqu++;
                    v[it->id].push_back(i);
                    i++;
                    if(i == n+1) break;
                }
                if(gg == 0) break;
                for(int j = 1; j <= woqu; j++)
                {
                    s.insert(make_node(no[i].a,i-j));
                }
                pp = 0;
            }
            else
            {
                pp = no[i].a == no[i].b ? no[i].a : 0;
                s.insert(make_node(no[i].a,i));
            }
        }
        if(gg == 0)
        {
            printf("No\n");
            continue;
        }
        printf("Yes\n");
        nct = 0;
        for(int i = 1; i <= n; i++)
        {
            if(flag[i] == 0) dfs(i);
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章