Codeforces Round #295 (Div. 2) D.Cubes(STL SET应用)

STL中的SET是一个可以自动去重,排序的集合,底层为红黑树。

遍历SET语法:

set <int> iterator p;

for(p=S.begin();p!=S.end();p++){.....}


删除元素 erase():参数即可以是key值也可以是迭代器地址。


用两个Set数组Below和Top标记每个格子上面三格和下面三格分别有几个格子及序号。

Rem也是一个Set,维护当前可以拿的格子集合。

每一步都贪心的从这集合中取,取完可能会导致同行的一些格子不能取,想象一个品字型的三个格子,取完左边边会导致右边不能取,还会导致下面一行一些格子能取。按这两条维护Rem集合。


代码:

//
//  main.cpp
//  295B. Cubes
//
//  Created by Baoli1100 on 15/3/2.
//  Copyright (c) 2015年 Baoli1100. All rights reserved.
//

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define  LL long long
#include <queue>
#include <map>
#include <vector>
#include <set>
const LL mod = (1e9+9);

struct node{
    int x,y;
}G[100005];

bool vis[100005];
set<int> Rem;

map<pair<int,int>,int > cur;
set<int> Top[100005];
set<int> Below[100005];
int N;
bool C(int x,int y){
    return cur.count(make_pair(x,y));
}


bool OK(int n){
    if(Rem.count(n)||vis[n]) return 0;
    set<int>::iterator p;
    for(p=Top[n].begin();p!=Top[n].end();p++){
        int t=*p;
        if(Below[t].size()<=1) return 0;
    }
    return 1;
}

int main(){
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        cur[make_pair(x,y)]=i;
        G[i].x=x;
        G[i].y=y;
    }
    for(int i=0;i<N;i++){
        int tx=G[i].x,ty=G[i].y;
        for(int j=-1;j<=1;j++){
            if(C(tx+j,ty+1)){
                int k=cur[make_pair(tx+j,ty+1)];
                Top[i].insert(k);
                Below[k].insert(i);
            }
        }
    }
    for(int i=0;i<N;i++){
        if(OK(i)){
            Rem.insert(i);
        }
    }
    
    LL res=0;
    for(int i=1;i<=N;i++){
        set<int>::iterator p;
        
        if(i&1){
            p=--Rem.end();
        }
        else{
            p=Rem.begin();
        }
        int tmp=*p;
        Rem.erase(p);
        vis[tmp]=1;
        res*=N;
        res+=tmp;
        res%=mod;
        for(p=Top[tmp].begin();p!=Top[tmp].end();p++){
            Below[*p].erase(tmp);
            if(Below[*p].size()==1){
                int k=*Below[*p].begin();
                Rem.erase(k);
            }
        }
        for(p=Below[tmp].begin();p!=Below[tmp].end();p++){
            Top[*p].erase(tmp);
            if(OK(*p)) Rem.insert(*p);
        }
    }
    printf("%lld",res);
    return 0;
}
    


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值