Codeforces 520D. Cubes 贪心模拟



每一步都取当前稳定的格子里面数字最大或者最小的数.

用一个set维护当前可取的格子 *begin 最大  *(--end) 最小

每删除一个格子都要对这个格子周围的6个格子进行稳定性检查

一个格子上面的3个格子确定了这个格子的稳定性

D. 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
3
2 1
1 0
0 1
output
19
input
5
0 0
0 1
0 2
0 3
0 4
output
2930


[cpp]  view plain  copy
  1. /* *********************************************** 
  2. Author        :CKboss 
  3. Created Time  :2015Äê03ÔÂ03ÈÕ ÐÇÆÚ¶þ 23ʱ46·Ö15Ãë 
  4. File Name     :D.cpp 
  5. ************************************************ */  
  6.   
  7. #include <iostream>  
  8. #include <cstdio>  
  9. #include <cstring>  
  10. #include <algorithm>  
  11. #include <string>  
  12. #include <cmath>  
  13. #include <cstdlib>  
  14. #include <vector>  
  15. #include <queue>  
  16. #include <set>  
  17. #include <map>  
  18.   
  19. using namespace std;  
  20.   
  21. typedef long long int LL;  
  22.   
  23. const LL mod = 1e9+9;  
  24. const int maxn=100100;  
  25.   
  26. struct BOX  
  27. {  
  28.     int x,y,id;  
  29. }box[maxn];  
  30.   
  31. typedef pair<int,int> pII;  
  32.   
  33. int n;  
  34. map<pII,int> mPI;  
  35. set<pII> Pt;  
  36. set<int> St;  
  37.   
  38. bool isST(int id)  
  39. {  
  40.     int x=box[id].x;  
  41.     int y=box[id].y;  
  42.   
  43.     pII tp;  
  44.   
  45.     /// check (x-1,y+1)  
  46.     tp = make_pair(x-1,y+1);  
  47.     if(Pt.count(tp)==1)  
  48.     {  
  49.         if(Pt.count( make_pair(x-2,y) )   
  50.                 || Pt.count( make_pair(x-1,y) ))  
  51.             ;  
  52.         else return false;  
  53.     }  
  54.   
  55.     /// check (x,y+1)  
  56.     tp = make_pair(x,y+1);  
  57.     if(Pt.count(tp)==1)  
  58.     {  
  59.         if(Pt.count( make_pair(x-1,y) )  
  60.                     || Pt.count( make_pair(x+1,y) ))  
  61.             ;  
  62.         else return false;  
  63.     }  
  64.   
  65.     /// check (x+1,y+1)  
  66.     tp = make_pair(x+1,y+1);  
  67.     if(Pt.count(tp)==1)  
  68.     {  
  69.         if(Pt.count( make_pair(x+1,y) )  
  70.                     || Pt.count( make_pair(x+2,y) ))  
  71.             ;  
  72.         else return false;  
  73.     }  
  74.   
  75.     return true;  
  76. }  
  77.   
  78. void checkST(int id)  
  79. {  
  80.     if(isST(id))  
  81.     {  
  82.         St.insert(id);  
  83.     }  
  84.     else  
  85.     {  
  86.         if(St.count(id)) St.erase(id);  
  87.     }  
  88. }  
  89.   
  90. void checkround(int id)  
  91. {  
  92.     int x=box[id].x;  
  93.     int y=box[id].y;  
  94.       
  95.     checkST(id);  
  96.       
  97.     for(int i=-1;i<=1;i++) /// dx  
  98.     {  
  99.         for(int j=-1;j<=1;j+=2) /// dy  
  100.         {  
  101.             int nx=x+i,ny=y+j;  
  102.             pII tp = make_pair(nx,ny);  
  103.             if(Pt.count(tp)==1)  
  104.             {  
  105.                 int dd=mPI[tp];  
  106.                 checkST(dd);  
  107.             }  
  108.         }  
  109.     }  
  110. }  
  111.   
  112. void Remove(int id)  
  113. {  
  114.     int x=box[id].x;  
  115.     int y=box[id].y;  
  116.   
  117.     /// erase  
  118.     Pt.erase(make_pair(x,y));  
  119.     St.erase(id);  
  120.   
  121.     /// check other stable box  
  122.       
  123.     for(int i=-1;i<=1;i++) // dx  
  124.     {  
  125.         for(int j=-1;j<=1;j+=2) // dy  
  126.         {  
  127.             int nx=x+i,ny=y+j;  
  128.             pII tp = make_pair(nx,ny);  
  129.             if(Pt.count(tp)==1)  
  130.             {  
  131.                 int ID=mPI[tp];  
  132.                 checkround(ID);  
  133.             }  
  134.         }  
  135.     }  
  136.       
  137. }  
  138.   
  139. int main()  
  140. {  
  141.     //freopen("in.txt","r",stdin);  
  142.     //freopen("out.txt","w",stdout);  
  143.   
  144.     scanf("%d",&n);  
  145.     for(int i=0;i<n;i++)  
  146.     {  
  147.         int x,y;  
  148.         scanf("%d%d",&x,&y);  
  149.         box[i]=(BOX){x,y,i};  
  150.         mPI[make_pair(x,y)]=i;  
  151.         Pt.insert(make_pair(x,y));  
  152.     }  
  153.     /// check stable disassemble box  
  154.   
  155.     int mxid=0,miid=0;  
  156.     for(int i=0;i<n;i++)  
  157.     {  
  158.         if(isST(i))  
  159.         {  
  160.             St.insert(i);  
  161.             mxid=max(mxid,i);  
  162.             miid=min(miid,i);  
  163.         }  
  164.     }  
  165.   
  166.     vector<int> vi;  
  167.       
  168.     for(int loop=0;loop<n;loop++)  
  169.     {  
  170.         if(loop%2==0) //find max  
  171.         {  
  172.             vi.push_back(mxid);  
  173.             Remove(mxid);  
  174.         }  
  175.         else // find min  
  176.         {  
  177.             vi.push_back(miid);  
  178.             Remove(miid);  
  179.         }  
  180.         if(loop==n-1) continue;  
  181.         miid=*St.begin();  
  182.         mxid=*(--St.end());  
  183.     }  
  184.   
  185.     LL ans=0,base=1;  
  186.     for(int sz=vi.size(),i=sz-1;i>=0;i--)  
  187.     {  
  188.         ans=(ans+base*vi[i]%mod)%mod;  
  189.         base=(base*n)%mod;  
  190.     }  
  191.   
  192.     cout<<ans%mod<<endl;  
  193.       
  194.     return 0;  
  195. }  

每一步都取当前稳定的格子里面数字最大或者最小的数.

用一个set维护当前可取的格子 *begin 最大  *(--end) 最小

每删除一个格子都要对这个格子周围的6个格子进行稳定性检查

一个格子上面的3个格子确定了这个格子的稳定性

D. 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
3
2 1
1 0
0 1
output
19
input
5
0 0
0 1
0 2
0 3
0 4
output
2930


[cpp]  view plain  copy
  1. /* *********************************************** 
  2. Author        :CKboss 
  3. Created Time  :2015Äê03ÔÂ03ÈÕ ÐÇÆÚ¶þ 23ʱ46·Ö15Ãë 
  4. File Name     :D.cpp 
  5. ************************************************ */  
  6.   
  7. #include <iostream>  
  8. #include <cstdio>  
  9. #include <cstring>  
  10. #include <algorithm>  
  11. #include <string>  
  12. #include <cmath>  
  13. #include <cstdlib>  
  14. #include <vector>  
  15. #include <queue>  
  16. #include <set>  
  17. #include <map>  
  18.   
  19. using namespace std;  
  20.   
  21. typedef long long int LL;  
  22.   
  23. const LL mod = 1e9+9;  
  24. const int maxn=100100;  
  25.   
  26. struct BOX  
  27. {  
  28.     int x,y,id;  
  29. }box[maxn];  
  30.   
  31. typedef pair<int,int> pII;  
  32.   
  33. int n;  
  34. map<pII,int> mPI;  
  35. set<pII> Pt;  
  36. set<int> St;  
  37.   
  38. bool isST(int id)  
  39. {  
  40.     int x=box[id].x;  
  41.     int y=box[id].y;  
  42.   
  43.     pII tp;  
  44.   
  45.     /// check (x-1,y+1)  
  46.     tp = make_pair(x-1,y+1);  
  47.     if(Pt.count(tp)==1)  
  48.     {  
  49.         if(Pt.count( make_pair(x-2,y) )   
  50.                 || Pt.count( make_pair(x-1,y) ))  
  51.             ;  
  52.         else return false;  
  53.     }  
  54.   
  55.     /// check (x,y+1)  
  56.     tp = make_pair(x,y+1);  
  57.     if(Pt.count(tp)==1)  
  58.     {  
  59.         if(Pt.count( make_pair(x-1,y) )  
  60.                     || Pt.count( make_pair(x+1,y) ))  
  61.             ;  
  62.         else return false;  
  63.     }  
  64.   
  65.     /// check (x+1,y+1)  
  66.     tp = make_pair(x+1,y+1);  
  67.     if(Pt.count(tp)==1)  
  68.     {  
  69.         if(Pt.count( make_pair(x+1,y) )  
  70.                     || Pt.count( make_pair(x+2,y) ))  
  71.             ;  
  72.         else return false;  
  73.     }  
  74.   
  75.     return true;  
  76. }  
  77.   
  78. void checkST(int id)  
  79. {  
  80.     if(isST(id))  
  81.     {  
  82.         St.insert(id);  
  83.     }  
  84.     else  
  85.     {  
  86.         if(St.count(id)) St.erase(id);  
  87.     }  
  88. }  
  89.   
  90. void checkround(int id)  
  91. {  
  92.     int x=box[id].x;  
  93.     int y=box[id].y;  
  94.       
  95.     checkST(id);  
  96.       
  97.     for(int i=-1;i<=1;i++) /// dx  
  98.     {  
  99.         for(int j=-1;j<=1;j+=2) /// dy  
  100.         {  
  101.             int nx=x+i,ny=y+j;  
  102.             pII tp = make_pair(nx,ny);  
  103.             if(Pt.count(tp)==1)  
  104.             {  
  105.                 int dd=mPI[tp];  
  106.                 checkST(dd);  
  107.             }  
  108.         }  
  109.     }  
  110. }  
  111.   
  112. void Remove(int id)  
  113. {  
  114.     int x=box[id].x;  
  115.     int y=box[id].y;  
  116.   
  117.     /// erase  
  118.     Pt.erase(make_pair(x,y));  
  119.     St.erase(id);  
  120.   
  121.     /// check other stable box  
  122.       
  123.     for(int i=-1;i<=1;i++) // dx  
  124.     {  
  125.         for(int j=-1;j<=1;j+=2) // dy  
  126.         {  
  127.             int nx=x+i,ny=y+j;  
  128.             pII tp = make_pair(nx,ny);  
  129.             if(Pt.count(tp)==1)  
  130.             {  
  131.                 int ID=mPI[tp];  
  132.                 checkround(ID);  
  133.             }  
  134.         }  
  135.     }  
  136.       
  137. }  
  138.   
  139. int main()  
  140. {  
  141.     //freopen("in.txt","r",stdin);  
  142.     //freopen("out.txt","w",stdout);  
  143.   
  144.     scanf("%d",&n);  
  145.     for(int i=0;i<n;i++)  
  146.     {  
  147.         int x,y;  
  148.         scanf("%d%d",&x,&y);  
  149.         box[i]=(BOX){x,y,i};  
  150.         mPI[make_pair(x,y)]=i;  
  151.         Pt.insert(make_pair(x,y));  
  152.     }  
  153.     /// check stable disassemble box  
  154.   
  155.     int mxid=0,miid=0;  
  156.     for(int i=0;i<n;i++)  
  157.     {  
  158.         if(isST(i))  
  159.         {  
  160.             St.insert(i);  
  161.             mxid=max(mxid,i);  
  162.             miid=min(miid,i);  
  163.         }  
  164.     }  
  165.   
  166.     vector<int> vi;  
  167.       
  168.     for(int loop=0;loop<n;loop++)  
  169.     {  
  170.         if(loop%2==0) //find max  
  171.         {  
  172.             vi.push_back(mxid);  
  173.             Remove(mxid);  
  174.         }  
  175.         else // find min  
  176.         {  
  177.             vi.push_back(miid);  
  178.             Remove(miid);  
  179.         }  
  180.         if(loop==n-1) continue;  
  181.         miid=*St.begin();  
  182.         mxid=*(--St.end());  
  183.     }  
  184.   
  185.     LL ans=0,base=1;  
  186.     for(int sz=vi.size(),i=sz-1;i>=0;i--)  
  187.     {  
  188.         ans=(ans+base*vi[i]%mod)%mod;  
  189.         base=(base*n)%mod;  
  190.     }  
  191.   
  192.     cout<<ans%mod<<endl;  
  193.       
  194.     return 0;  
  195. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值