瓦西亚和皮台亚摆放了m个方块。方块被编号为0到m-1(每个号码出现恰好一次)。现在建立一个座标系OX表示地面,OY的方向是竖直向上的。每一方块的左下角有一个座标而且是整点座标。
摆放好的方块一定要是稳定的。稳定的含意是每一个不在地面上的方块在他的下面至少有一个方块与他相接触。可以是共边,也可以是共点的。也就是说如果方块座标为(x,y),要么y=0,或者存在一个方块的座标为(x-1,y-1)或者 (x,y-1) 或者 (x+1,y-1)。
现在瓦西亚和皮台亚要轮流把这些方块一个个拆下来。按照拆下来的顺序从左到右摆成一行,那么方块上面的编号就会组成一个m进制的数字。
拆的过程中,要始终保持剩下的方块稳定。瓦西亚想要最终的数字尽可能大,而皮台亚想要尽可能小,瓦西亚先开始拆。
请帮助计算一下最终形成的数字是多少,结果比较大,输出对 109+9 取余后的结果。
样例解释:
先拿(0,1)编号为2
再拿(2,1)编号为0
再拿(1,0)编号为1
最后形成的数字是201,转成10进制是19。
Input
单组测试数据。 第一行有一个整数 m (2≤m≤10^5)。 接下来m行,每一行有一个方块的座标xi,yi (-10^9≤xi≤10^9, 0≤yi≤10^9),第i个的编号为i。i从0开始。 输入保证刚开始的摆放是稳定的,而且没有两个方块的座标是一样的。
Output
输出结果占一行。
Input示例
样例输入1 3 2 1 1 0 0 1
Output示例
样例输出1 19
#include <cstdio>
#include <map>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 9;
const int MAXN = 1e5 + 10;
struct Loc
{
Loc(int a, int b)
{
x = a;
y = b;
}
int x;
int y;
bool operator< (const Loc &a) const
{
if (y == a.y)
{
return x < a.x;
}
return y < a.y;
}
};
map<Loc, int> table;
bool vis[MAXN];
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int> > q2;
ll result = 0;
void add(int a, int m)
{
result = (result * m + a) % MOD;
}
int xPos[MAXN];
int yPos[MAXN];
bool judge(int x, int y)
{
if (table.find(Loc(x, y + 1)) != table.end())
{
if (table.find(Loc(x - 1, y)) == table.end() && table.find(Loc(x + 1, y)) == table.end())
{
return false;
}
}
if (table.find(Loc(x - 1, y + 1)) != table.end())
{
if (table.find(Loc(x - 1, y)) == table.end() && table.find(Loc(x - 2, y)) == table.end())
{
return false;
}
}
if (table.find(Loc(x + 1, y + 1)) != table.end())
{
if (table.find(Loc(x + 1, y)) == table.end() && table.find(Loc(x + 2, y)) == table.end())
{
return false;
}
}
return true;
}
int main()
{
int m;
int x, y;
cin >> m;
for (int i = 0; i < m; i++)
{
cin >> x >> y;
xPos[i] = x;
yPos[i] = y;
table[Loc(x, y)] = i;
}
for (int i = 0; i < m; i++)
{
if (judge(xPos[i], yPos[i]))
{
q1.push(i);
q2.push(i);
}
}
memset(vis, false, sizeof(vis));
int top = 0;
int count = 0;
while (count < m)
{
if ((count & 1) == 0)
{
top = q1.top();
}
else
{
top = q2.top();
}
x = xPos[top];
y = yPos[top];
bool found = false;
if (!vis[top] && judge(x, y))
{
add(top, m);
table.erase(Loc(x, y));
vis[top] = true;
found = true;
}
if ((count & 1) == 0)
{
q1.pop();
}
else
{
q2.pop();
}
if (found)
{
count++;
if (table.find(Loc(x, y - 1)) != table.end() && judge(x, y - 1))
{
q1.push(table[Loc(x, y - 1)]);
q2.push(table[Loc(x, y - 1)]);
}
if (table.find(Loc(x - 1, y - 1)) != table.end() && judge(x - 1, y - 1))
{
q1.push(table[Loc(x - 1, y - 1)]);
q2.push(table[Loc(x - 1, y - 1)]);
}
if (table.find(Loc(x + 1, y - 1)) != table.end() && judge(x + 1, y - 1))
{
q1.push(table[Loc(x + 1, y - 1)]);
q2.push(table[Loc(x + 1, y - 1)]);
}
}
}
cout << result << endl;
return 0;
}