Codeforces Round #295 (Div. 1) B. Cubes (STL+類拓撲)
來源:程序員人生 發布時間:2015-03-25 11:41:06 閱讀次數:2410次
最近課業沉重,這題寫了兩天。。昨晚睡覺的時候才突然想到了最后1點的解決方法。
不知道該不該叫做拓撲。。感覺還是挺像的。。就把標題稱之為類拓撲了。。這題的方法是用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(⑴.0)
const int mod=1e9+9;
const int INF=0x3f3f3f3f;
const double eqs=1e⑼;
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⑴,y+1))!=mp.end()){
if(mp.find(make_pair(x⑵,y))==mp.end()&&mp.find(make_pair(x⑴,y))==mp.end()) return 0;
}
if(mp.find(make_pair(x,y+1))!=mp.end()){
if(mp.find(make_pair(x⑴,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
",u);
x=fei[u].x⑴;y=fei[u].y⑴;
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⑴;
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⑴;
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
",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;
}
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈