Codeforces Round #295 (Div. 1) B. Cubes (STL+类拓扑)

最近课业繁重,这题写了两天。。昨晚睡觉的时候才突然想到了最后一点的解决方法。

不知道该不该叫做拓扑。。感觉还是挺像的。。就把标题称之为类拓扑了。。这题的方法是用map来标记状态是否存在,然后用类似拓扑的方法不断的找拿走后依然稳定的方块,我用了两个优先队列来维护,分别取最大和最小。然后就是模拟这个过程取方块了。

代码如下:

#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL long long
#define pi acos(-1.0)
const int mod=1e9+9;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
struct node
{
        int x, y;
}fei[110000];
priority_queue<int,vector<int>,greater<int> >q1;
priority_queue<int,vector<int>,less<int> >q2;
map<pair<int,int>, int> mp;
int vis[110000];
bool check(int x, int y)
{
        if(mp.find(make_pair(x-1,y+1))!=mp.end()){
                if(mp.find(make_pair(x-2,y))==mp.end()&&mp.find(make_pair(x-1,y))==mp.end()) return 0;
        }
        if(mp.find(make_pair(x,y+1))!=mp.end()){
                if(mp.find(make_pair(x-1,y))==mp.end()&&mp.find(make_pair(x+1,y))==mp.end()) return 0;
        }
        if(mp.find(make_pair(x+1,y+1))!=mp.end()){
                if(mp.find(make_pair(x+1,y))==mp.end()&&mp.find(make_pair(x+2,y))==mp.end()) return 0;
        }
        return 1;
}
void topo(int n)
{
        int i, j, k=0, u, x, y, v;
        LL ans=0;
        map<pair<int,int>,int>::iterator it;
        for(it=mp.begin();it!=mp.end();it++){
                if(check(it->first.first,it->first.second)){
                        u=it->second;
                        q1.push(u);q2.push(u);
                }
        }
        while(k<n){
                if((k&1)==0){
                        while(!q2.empty()){
                                u=q2.top();
                                q2.pop();
                                if(!vis[u]&&check(fei[u].x,fei[u].y)){
                                        vis[u]=1;
                                        break;
                                }
                        }
                }
                else{
                        while(!q1.empty()){
                                u=q1.top();
                                q1.pop();
                                if(!vis[u]&&check(fei[u].x,fei[u].y)){
                                        vis[u]=1;
                                        break;
                                }
                        }
                }
                ans=(ans*n%mod+u)%mod;
                mp.erase(make_pair(fei[u].x,fei[u].y));
                //printf("%d\n",u);
                x=fei[u].x-1;y=fei[u].y-1;
                if(mp.find(make_pair(x,y))!=mp.end()){
                        v=mp[make_pair(x,y)];
                        if(!vis[v]&&check(x,y)){
                                q1.push(v);
                                q2.push(v);
                        }
                }
                x=fei[u].x;y=fei[u].y-1;
                if(mp.find(make_pair(x,y))!=mp.end()){
                        v=mp[make_pair(x,y)];
                        if(!vis[v]&&check(x,y)){
                                q1.push(v);
                                q2.push(v);
                        }
                }
                x=fei[u].x+1;y=fei[u].y-1;
                if(mp.find(make_pair(x,y))!=mp.end()){
                        v=mp[make_pair(x,y)];
                        if(!vis[v]&&check(x,y)){
                                q1.push(v);
                                q2.push(v);
                        }
                }
                k++;
        }
        printf("%I64d\n",ans);
}
int main()
{
        int n, i, j, pos, x, y;
        scanf("%d",&n);
        mp.clear();
        memset(vis,0,sizeof(vis));
        for(i=0;i<n;i++){
                scanf("%d%d",&fei[i].x,&fei[i].y);
                mp[make_pair(fei[i].x,fei[i].y)]=i;
        }
        topo(n);
        return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值