Codeforces Round #295 (Div. 1) B. Cubes(最大最小堆+拓扑模拟)

来源:互联网 发布:suse11 linux iso下载 编辑:程序博客网 时间:2024/06/08 04:44

B. Cubes
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Once Vasya and Petya assembled a figure of m cubes, each of them is associated with a number between 0 and m - 1 (inclusive, each number appeared exactly once). Let's consider a coordinate system such that the OX is the ground, and the OY is directed upwards. Each cube is associated with the coordinates of its lower left corner, these coordinates are integers for each cube.

The figure turned out to be stable. This means that for any cube that is not on the ground, there is at least one cube under it such that those two cubes touch by a side or a corner. More formally, this means that for the cube with coordinates (x, y) either y = 0, or there is a cube with coordinates (x - 1, y - 1)(x, y - 1) or (x + 1, y - 1).

Now the boys want to disassemble the figure and put all the cubes in a row. In one step the cube is removed from the figure and being put to the right of the blocks that have already been laid. The guys remove the cubes in such order that the figure remains stable. To make the process more interesting, the guys decided to play the following game. The guys take out the cubes from the figure in turns. It is easy to see that after the figure is disassembled, the integers written on the cubes form a number, written in the m-ary positional numerical system (possibly, with a leading zero). Vasya wants the resulting number to be maximum possible, and Petya, on the contrary, tries to make it as small as possible. Vasya starts the game.

Your task is to determine what number is formed after the figure is disassembled, if the boys play optimally. Determine the remainder of the answer modulo 109 + 9.

Input

The first line contains number m (2 ≤ m ≤ 105).

The following m lines contain the coordinates of the cubes xi, yi ( - 109 ≤ xi ≤ 1090 ≤ yi ≤ 109) in ascending order of numbers written on them. It is guaranteed that the original figure is stable.

No two cubes occupy the same place.

Output

In the only line print the answer to the problem.

Sample test(s)
input
32 11 00 1
output
19
input
50 00 10 20 30 4
output
2930



大致思路:

先手始终取出可拆点中的最大值,后手始终取出可拆点中的最小值

所以每次把可拆点放在最大堆和最小堆里,然后每次拆掉一个点,就类似和拓扑排序一样,更新其他点是否可拆

吐槽一下居然是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
原创粉丝点击