Codeforces 520D. Cubes 状态模拟+贪心

Codeforces 520D


Description

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 Input


Input
3
2 1
1 0
0 1
Output
19
Input
5
0 0
0 1
0 2
0 3
0 4
Output
2930


先取编号大的 再取编号小的  
每次取完都要更新状态  使图保持稳定
((x,y)的下面有(x-1,y-1),(x,y-1)或(x+1,y-1)或y=0 都算稳定状态)
每次取前都要判断上方三个的支撑是不是只有当前箱子  
若其中一个是 则不能取 取走当前箱子上方三个都稳定 才能取
取的数放在前面 取得的结果为m进制的数
最后转化为十进制输出

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<climits>
#include<map>
#include<set>
#include<vector>
#define MOD 1000000009

using namespace std;

priority_queue<int>large;
priority_queue<int,vector<int>,greater<int> >small;
map<pair<int,int>,int>st;
pair<int ,int>coor[100010],temp;
int ans[100010];

bool fnd(pair<int,int>pai)
{
    return st.count(pai);///用迭代器逐个判断会超时
}

bool removable(pair<int,int>pai)
{
    if(!fnd(pai))
    {
        return 0;
    }
    else
    {
        if(!(!fnd(pair<int,int>(pai.first-1,pai.second+1))||fnd(pair<int,int>(pai.first-2,pai.second))||fnd(pair<int,int>(pai.first-1,pai.second))))
        {
            return 0;
        }
        if(!(!fnd(pair<int,int>(pai.first,pai.second+1))||fnd(pair<int,int>(pai.first-1,pai.second))||fnd(pair<int,int>(pai.first+1,pai.second))))
        {
            return 0;
        }
        if(!(!fnd(pair<int,int>(pai.first+1,pai.second+1))||fnd(pair<int,int>(pai.first+1,pai.second))||fnd(pair<int,int>(pai.first+2,pai.second))))
        {
            return 0;
        }
        return 1;
    }
}

void del(int p)
{
    temp=coor[p];
    st.erase(temp);
}

void judge(int id,int s)
{
    temp=coor[id];
    if(s)
    {
        large.pop();
    }
    else
    {
        small.pop();
    }

    if(removable(pair<int,int>(temp.first-1,temp.second-1)))
    {

        large.push(st[pair<int,int>(temp.first-1,temp.second-1)]);
        small.push(st[pair<int,int>(temp.first-1,temp.second-1)]);
    }

    if(removable(pair<int,int>(temp.first,temp.second-1)))
    {
        large.push(st[pair<int,int>(temp.first,temp.second-1)]);
        small.push(st[pair<int,int>(temp.first,temp.second-1)]);
    }
    if(removable(pair<int,int>(temp.first+1,temp.second-1)))
    {
        large.push(st[pair<int,int>(temp.first+1,temp.second-1)]);
        small.push(st[pair<int,int>(temp.first+1,temp.second-1)]);
    }
}

int main()
{
    int m;
    int i;
    int t=0;
    scanf("%d",&m);
    for(i=0; i<m; ++i)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        st[pair<int,int>(x,y)]=i;
        coor[i]=pair<int,int>(x,y);
    }
    map<pair<int,int>,int>::iterator pos;
    for(pos=st.begin();pos!=st.end();pos++)
    {
        if(removable(pos->first))
        {
            large.push(pos->second);
            small.push(pos->second);
        }
    }
    while(!st.empty())
    {
        while(!large.empty()&&(!fnd(coor[large.top()])||!removable(coor[large.top()])))
        {
            large.pop();
        }
        if(!large.empty())
        {
            del(large.top());
            ans[t++]=large.top();
//            cout<<ans[t-1]<<endl;
            judge(large.top(),1);
        }
//        cout<<st.size()<<"size"<<endl;
//        cout<<removable(coor[small.top()])<<"LL"<<endl;
        while(!small.empty()&&(!fnd(coor[small.top()])||!removable(coor[small.top()])))
        {
            small.pop();
        }
        if(!small.empty())
        {
            del(small.top());
            ans[t++]=small.top();
//            cout<<ans[t-1]<<endl;
            judge(small.top(),0);
        }
    }
//    for(i=0;i<t;++i)
//    {
//        cout<<ans[i];
//    }
//    cout<<endl;
    long long sum=0;
    long long base=1;
    for(i=t-1;i>=0;--i)
    {
        sum+=ans[i]*base;
        if(sum>=MOD)///坑点  取mod
        {
            sum%=MOD;
        }
        base*=m;
        if(base>=MOD)
        {
            base%=MOD;
        }
    }
    cout<<sum<<endl;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值