Codeforces Round #295 (Div. 1) B. Cubes (STL+類拓?fù)?
來源:程序員人生 發(fā)布時(shí)間:2015-03-25 11:41:06 閱讀次數(shù):2415次
最近課業(yè)沉重,這題寫了兩天。。昨晚睡覺的時(shí)候才突然想到了最后1點(diǎn)的解決方法。
不知道該不該叫做拓?fù)洹!8杏X還是挺像的。。就把標(biāo)題稱之為類拓?fù)淞恕!_@題的方法是用map來標(biāo)記狀態(tài)是不是存在,然后用類似拓?fù)涞姆椒ú粩嗟恼夷米吆笕匀环€(wěn)定的方塊,我用了兩個(gè)優(yōu)先隊(duì)列來保護(hù),分別取最大和最小。然后就是摹擬這個(gè)進(jìn)程取方塊了。
代碼以下:
#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;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈