Codeforces 520D. Cubes 状态模拟+贪心
2015-08-03 19:55
471 查看
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 ≤ 109, 0 ≤ 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
Output
Input
Output
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 ≤ 109, 0 ≤ 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; }
相关文章推荐
- uva12299 RMQ with Shifts(线段树基础)
- 十六进制文本用eclipse转换可执行文件方法
- jni常见错误
- 数组下标为偶数,ASCII为偶数的字符重新输出,其他删除
- 树的重心及其一些性质
- select函数分析
- p2098 求sky数
- LeetCode(86) Partition List
- HDU 2571 命运(DP)
- jni 使用java 调用C代码
- wifi万能钥匙下载 v3.2.36 官方手机版
- mongodb个人研究
- hdoj 超级楼梯 【递归】
- 高效MacBook工作环境配置
- HDU 2063.过山车【二分图、二分匹配初接触】【8月3】
- Currency Exchange【POJ--1860】【SPFA】
- [BZOJ 2038][cogs 1775]小Z的袜子
- 【Qt OpenGL教程】10:加载3D世界,并在其中漫游
- HBase Region split 策略
- C++零碎知识点