Codeforces Round #295 (Div. 1) B. Cubes(最大最小堆+拓扑模拟)
来源:互联网 发布:suse11 linux iso下载 编辑:程序博客网 时间:2024/06/08 04:44
大致思路:
先手始终取出可拆点中的最大值,后手始终取出可拆点中的最小值
所以每次把可拆点放在最大堆和最小堆里,然后每次拆掉一个点,就类似和拓扑排序一样,更新其他点是否可拆
吐槽一下居然是mod 1e9+9..而不是+7
//#pragma comment(linker, "/STACK:1024000000,1024000000")#include <iostream>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <ctime>#include <bitset>#include <algorithm>#define SZ(x) ((int)(x).size())#define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i)#define REP(i,n) for ( int i=1; i<=int(n); i++ )#define rep(i,n) for ( int i=0; i<int(n); i++ )using namespace std;typedef long long ll;#define X first#define Y secondtypedef pair<int,int> pii;template <class T>inline bool RD(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c<'0' || c>'9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1;}template <class T>inline void PT(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) pt(x / 10); putchar(x % 10 + '0');}const int N = 1e5+100;map<pii,int>id;pii poi[N];int in[N];bool vis[N];vector<int>G[N];priority_queue<int> qma;priority_queue<int,vector<int>,greater<int> > qmi;bool ok(int u,int v){ int sx = poi[u].X, sy = poi[u].Y; int x = poi[v].X, y = poi[v].Y; if( sx == x-1 && sy == y-1 ) return true; if( sx == x && sy == y-1) return true; if( sx == x+1 && sy == y-1 ) return true; return false;}bool judge(int u){ foreach(it,G[u]){ int v = *it; if( vis[v] || poi[v].Y == 0 || ok(v,u) ) continue; if( in[v] == 1) return false; } return true;}vector<int> ans;bool inqmi[N],inqma[N];const ll mod = 1e9+9;int main(){ int m; cin>>m; int n = m; rep(i,n){ int x,y; RD(x),RD(y); id[pii(x,y)] = i; poi[i] = pii(x,y); } rep(u,n){ int x = poi[u].X, y = poi[u].Y; if( id.count(pii(x-1,y-1)) ){ int v = id[pii(x-1,y-1)]; G[u].push_back(v); G[v].push_back(u); in[u]++; } if( id.count(pii(x,y-1))){ int v = id[pii(x,y-1)]; G[u].push_back(v); G[v].push_back(u); in[u]++; } if( id.count(pii(x+1,y-1)) ){ int v = id[pii(x+1,y-1)]; G[u].push_back(v); G[v].push_back(u); in[u]++; } } rep(i,n){ if( judge(i) ) { qmi.push(i),qma.push(i); inqmi[i] = inqma[i] = 1; } } REP(CNT,n){ if( CNT&1 ){ int u; while(true){ u = qma.top(); qma.pop(); inqma[u] = 0; if( vis[u] ) continue; if( judge(u) ) break; } vis[u] = 1; ans.push_back(u); foreach(it,G[u]) { int v = *it; if( vis[v] ) continue; if( ok(u,v) ) in[v]--; else{ if(judge(v)) { if( !inqma[v] ) qma.push(v),inqma[v] = 1; if( !inqmi[v] ) qmi.push(v),inqmi[v] = 1; } } } }else{ int u; while(true){ u = qmi.top(); qmi.pop(); inqmi[u] = 0; if( vis[u]) continue; if( judge(u) ) break; } ans.push_back(u); vis[u] = 1; foreach(it,G[u]){ int v = *it; if( vis[v] ) continue; if( ok(u,v) ) in[v]--; else{ if(judge(v)){ if( !inqma[v] ) qma.push(v),inqma[v] = 1; if( !inqmi[v] ) qmi.push(v),inqmi[v] = 1; } } } } } ll res = 0; ll tmp = 1; reveach(it,ans){ res = (res+tmp*(*it))%mod; tmp = tmp*(ll)m%mod; } cout<<res<<endl;}
0 0
- Codeforces Round #295 (Div. 1) B. Cubes(最大最小堆+拓扑模拟)
- Codeforces Round #295 (Div. 1) B. Cubes (STL+类拓扑)
- stl模拟 Codeforces Round #295 (Div. 1) B. Cubes
- Codeforces Round #295 (Div. 1) B. Cubes
- Codeforces Round #395(Div. 2)B. Timofey and cubes【模拟】
- Codeforces Round #225 (Div. 1) B 模拟:
- Codeforces Round #295 (Div. 2) -- B. Two Buttons (模拟)
- Codeforces Round #444 (Div. 2) B. Cubes for Masha
- Codeforces Round #444 (Div. 2) B. Cubes for Masha
- Codeforces Round #444(Div.2) B. Cubes for Masha
- Codeforces Round #356 (Div. 1)-B. Bear and Tower of Cubes
- Codeforces Round #395 (Div. 2) B. Timofey and cubes(水题)
- Codeforces Round #438 (Div. 1 + Div. 2 combined) B. Race Against Time(模拟)
- Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)
- Codeforces Round #107 (Div. 2) B (简单模拟)
- Codeforces Round #265 (Div. 2) B. Inbox (100500)(模拟)
- Codeforces Round #316 (Div. 2) B. Simple Game (模拟)
- Codeforces Round #339 (Div. 2) (B 模拟)
- android动画(Animation)
- 【Python基础】生成器——generator
- I学霸官方免费教程六:Java基础之流程控制
- 【Android开源项目分析】TAB导航栏PagerSlidingTabStrip的使用和源码分析
- 第一课 MongoDB 概述与安装
- Codeforces Round #295 (Div. 1) B. Cubes(最大最小堆+拓扑模拟)
- jQuery效果之淡入淡出
- SOAPUI 使用groovy发送http请求 自动化测试
- 将指定字符串分割成字符数组
- Java并发编程:volatile关键字解析
- Java类对象的定义和使用
- 微信接口获取昵称中的表情符的过滤
- 第81讲:Scala中List的构造时的类型约束逆变、协变、下界详解学习笔记
- 互联网金融四大暴富机会:P2P 征信 支付 供应链金融