zoukankan      html  css  js  c++  java
  • Codeforces Round #295 D. Cubes [贪心 set map]

    传送门

    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 ≤ 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 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

    题意:有m个方块 每个方块有一个值 并且是堆起来稳定的 一个方块可以拿掉当且仅当剩下的还是稳定的 双方轮流拿 从左到右放组成一个m进制的数 (转自 http://blog.csdn.net/u011686226/article/details/44036875)

    10129550 2015-03-03 11:00:30 njczy2010 D - Cubes GNU C++ Accepted 499 ms 32596 KB
    10129479 2015-03-03 10:52:18 njczy2010 D - Cubes GNU C++ Wrong answer on test 21 421 ms 21600 KB
    10129457 2015-03-03 10:49:33 njczy2010 D - Cubes GNU C++ Wrong answer on test 21 436 ms 21700 KB
    10129412 2015-03-03 10:44:12 njczy2010 D - Cubes GNU C++ Wrong answer on test 3 0 ms 41100 KB
      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdlib>
      4 #include<cstdio>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<map>
      9 #include<set>
     10 #include<stack>
     11 #include<string>
     12 
     13 #define N 100005
     14 #define M 10005
     15 #define mod 1000000007
     16 //#define p 10000007
     17 //#define mod2 1000000009
     18 #define ll long long
     19 #define ull unsigned long long
     20 #define LL long long
     21 #define eps 1e-6
     22 //#define inf 2147483647
     23 #define maxi(a,b) (a)>(b)? (a) : (b)
     24 #define mini(a,b) (a)<(b)? (a) : (b)
     25 
     26 using namespace std;
     27 
     28 int n;
     29 int x[N],y[N];
     30 map<pair<int,int>,int>mp;
     31 int r[N];
     32 set<int>s;
     33 int ans[N];
     34 int vis[N];
     35 ll mod2=1000000009;
     36 
     37 int ok(int i)
     38 {
     39     int nx,ny,te;
     40     nx=x[i]-1;ny=y[i]+1;
     41     te=mp[ make_pair(nx,ny) ];
     42     if(te!=0 && r[te-1]==1){
     43         return 0;
     44     }
     45     nx=x[i];
     46     te=mp[ make_pair(nx,ny) ];
     47     if(te!=0 && r[te-1]==1){
     48         return 0;
     49     }
     50     nx=x[i]+1;
     51     te=mp[ make_pair(nx,ny) ];
     52     if(te!=0 && r[te-1]==1){
     53         return 0;
     54     }
     55     return 1;
     56 }
     57 
     58 void ini()
     59 {
     60     int i;
     61     int nx,ny;
     62     s.clear();
     63     memset(r,0,sizeof(r));
     64     memset(vis,0,sizeof(vis));
     65     for(i=0;i<n;i++){
     66         scanf("%d%d",&x[i],&y[i]);
     67         mp[ make_pair(x[i],y[i]) ]=i+1;
     68     }
     69     for(i=0;i<n;i++){
     70         nx=x[i]-1;ny=y[i]-1;
     71         if(mp[ make_pair(nx,ny) ]!=0){
     72             r[i]++;
     73         }
     74         nx=x[i];
     75         if(mp[ make_pair(nx,ny) ]!=0){
     76             r[i]++;
     77         }
     78         nx=x[i]+1;
     79         if(mp[ make_pair(nx,ny) ]!=0){
     80             r[i]++;
     81         }
     82     }
     83     for(i=0;i<n;i++){
     84         if(ok(i)==1){
     85             s.insert(i);
     86             vis[i]=-1;
     87         }
     88     }
     89 }
     90 
     91 void changeerase(int i)
     92 {
     93     int nx,ny,te;
     94     nx=x[i]-1;ny=y[i]-1;
     95     te=mp[ make_pair(nx,ny) ];
     96     if(te!=0){
     97         if(vis[te-1]==-1){
     98             vis[te-1]=0;s.erase(te-1);
     99         }
    100         return;
    101     }
    102     nx=x[i];
    103     te=mp[ make_pair(nx,ny) ];
    104     if(te!=0){
    105         if(vis[te-1]==-1){
    106             vis[te-1]=0;s.erase(te-1);
    107         }
    108         return;
    109     }
    110     nx=x[i]+1;
    111     te=mp[ make_pair(nx,ny) ];
    112     if(te!=0){
    113         if(vis[te-1]==-1){
    114             vis[te-1]=0;s.erase(te-1);
    115         }
    116         return;
    117     }
    118 }
    119 
    120 void changeadd(int i)
    121 {
    122     int nx,ny,te;
    123     nx=x[i]-1;ny=y[i]-1;
    124     te=mp[ make_pair(nx,ny) ];
    125     if(te!=0 && ok(te-1)==1){
    126         s.insert(te-1);
    127         vis[te-1]=-1;
    128     }
    129     nx=x[i];
    130     te=mp[ make_pair(nx,ny) ];
    131     if(te!=0 && ok(te-1)==1){
    132         s.insert(te-1);
    133         vis[te-1]=-1;
    134     }
    135     nx=x[i]+1;
    136     te=mp[ make_pair(nx,ny) ];
    137     if(te!=0 && ok(te-1)==1){
    138         s.insert(te-1);
    139         vis[te-1]=-1;
    140     }
    141 }
    142 
    143 void updata(int i)
    144 {
    145     int nx,ny,te;
    146     nx=x[i]-1;ny=y[i]+1;
    147     te=mp[ make_pair(nx,ny) ];
    148     if(te!=0){
    149         r[te-1]--;
    150         if(r[te-1]==1)
    151             changeerase(te-1);
    152     }
    153     nx=x[i];
    154     te=mp[ make_pair(nx,ny) ];
    155     if(te!=0){
    156         r[te-1]--;
    157         if(r[te-1]==1)
    158             changeerase(te-1);
    159     }
    160     nx=x[i]+1;
    161     te=mp[ make_pair(nx,ny) ];
    162     if(te!=0){
    163         r[te-1]--;
    164         if(r[te-1]==1)
    165             changeerase(te-1);
    166     }
    167 }
    168 
    169 void solve()
    170 {
    171     int i,index;
    172     set<int>::iterator it;
    173     for(i=0;i<n;i++){
    174         if(i%2==0){
    175             it=s.end();
    176             it--;
    177             ans[i]=*it;
    178         }
    179         else{
    180             it=s.begin();
    181             ans[i]=*it;
    182         }
    183         index=*it;
    184         s.erase(*it);
    185         mp[ make_pair(x[index],y[index]) ]=0;
    186         vis[index]=1;
    187         updata(index);
    188         changeadd(index);
    189     }
    190 }
    191 
    192 void out()
    193 {
    194     int i;
    195     ll aa=0;
    196     /*
    197     for(i=0;i<n;i++){
    198         printf("%d",ans[i]);
    199     }
    200     printf("
    ");*/
    201     for(i=0;i<n;i++){
    202         aa=(aa*(ll)n)%mod2;
    203         aa=(aa+(ll)ans[i])%mod2;
    204     }
    205     printf("%I64d
    ",aa);
    206 }
    207 
    208 int main()
    209 {
    210     //freopen("data.in","r",stdin);
    211     //freopen("data.out","w",stdout);
    212     //scanf("%d",&T);
    213     //for(int ccnt=1;ccnt<=T;ccnt++)
    214     //while(T--)
    215     //scanf("%d%d",&n,&m);
    216     while(scanf("%d",&n)!=EOF)
    217     {
    218         ini();
    219         solve();
    220         out();
    221     }
    222     return 0;
    223 }
  • 相关阅读:
    初识DJango框架
    web框架基础
    前端——JavaScript
    前端——css(下)
    前端——css(上)
    前端——html
    spring注解
    spring boot 详解(1)spring boot +mybatis+mysql+jsp
    spring 事务控制
    maven pom文件管理
  • 原文地址:https://www.cnblogs.com/njczy2010/p/4311203.html
Copyright © 2011-2022 走看看