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

来源:互联网 发布:app拍照软件 编辑:程序博客网 时间:2024/06/07 01:58

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

不知道该不该叫做拓扑。。感觉还是挺像的。。就把标题称之为类拓扑了。。这题的方法是用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
原创粉丝点击