URAL 1015. Test the Difference!

Problem Url: http://acm.timus.ru/problem.aspx?space=1&num=1015

This is a math related problem. First we'll need to determine the total possible scheme of dices, there're 30 schemes. To calculate this, I used the following means:

Totally 720=6! poses(of all schemes' dice in different poses) according to the problem. and each scheme can have 24 different poses. So the scheme count is 720/24 = 30

In the problem, we will need to cache all the detected dice pose and update all those poses of the same scheme(if they haven't been filled in lookup store yet).  For the lookup store, so far I have got two possible approaches, one is using a large 5-dim array as hashtable, this will provide very quick lookup, but waste a lot of memory (since among 6^5 entries, only 720(at most) is used).    Another approach is using map(or hashmap) which may save a lot of memory since only necessary entries will be used. Also, I'll use a large array to store the scheme id of all the input dice, so far I statically define a large global array.  Here are the codes:

1. Use large 5-dim array as lookup table:

/*
AC 0.015 335 KB 
*/
#include 
< cstdio >
#include 
< iostream >

#define  MAXNUM 100000
#define  MAXKIND 30

using   namespace  std;

// global variables 
int  N = 0 ;   // dice number

int  inputs[MAXNUM];  // record the input dice's category
int  startpos[MAXKIND + 1 ]; // record the start item of each category

int  dicehash[ 6 ][ 6 ][ 6 ][ 6 ][ 6 ];   // dice category lookup table, dice[left][right][top][front][bottom](do not need the 6th attribute)

int  g_l = 0 , g_r = 0 , g_t = 0 , g_f = 0 , g_bm = 0 , g_bk = 0 ;

void  updatehash( int  l,  int  r,  int  t,  int  f,  int  bm,  int  bk,  int  idx);

void  main()
{
    memset(dicehash, 
0 sizeof ( int *   6 * 6 * 6 * 6 * 6 );

    
int  i = 0 ;
    
int  curidx  =   0 ;

    scanf(
" %d " & N);

    
for (i = 0 ;i < N; ++ i)
    {
        scanf(
" %d " & g_l); scanf( " %d " & g_r); scanf( " %d " & g_t); scanf( " %d " & g_f); scanf( " %d " & g_bm); scanf( " %d " & g_bk);

        
if ( ! dicehash[g_l - 1 ][g_r - 1 ][g_t - 1 ][g_f - 1 ][g_bm - 1 ]) 
        {
            updatehash(g_l,g_r,g_t,g_f,g_bm,g_bk,
++ curidx);
            startpos[curidx] 
=  i;
        }

        inputs[i] 
=  dicehash[g_l - 1 ][g_r - 1 ][g_t - 1 ][g_f - 1 ][g_bm - 1 ];
    }

    
int  start  =   0 ;

    printf(
" %d " , curidx);

    
for (i = 1 ;i <= curidx; ++ i)
    {
        start 
=  startpos[i];

        
while (start < N)
        {
            
if (inputs[start]  ==  i) printf( " %d  " ,(start + 1 ));

            
++ start;
        }
        printf(
" " );
    }
}

void  updatehash( int  l,  int  r,  int  t,  int  f,  int  bm,  int  bk,  int  idx)
{
    
int  i = 0 ,j = 0 ,k = 0 ;
    
int  temp  = 0 ;

    
for (i = 0 ;i < 4 ; ++ i)
    {
        
for (j = 0 ;j < 4 ; ++ j)
        {
            
for (k = 0 ;k < 4 ; ++ k)
            {
                dicehash[l
- 1 ][r - 1 ][t - 1 ][f - 1 ][bm - 1 =  idx;
                
                
// rotate in direction 3 (left->top, top->right, right->bottom, bottom->left)
                temp  =  l;
                l 
=  bm; bm  =  r; r  =  t; t  =  temp;
            }

            
// rotate in direction 2 (front->top, top->back, back->bottom, bottom->front)
            temp  =  f;
            f 
=  bm; bm  =  bk; bk  =  t; t  =  temp;
        }

        temp 
=  l;
        l 
=  f; f  =  r; r  =  bk; bk  =  temp;
        
// rotate in direction 1 ( left->back, back->right, right->front, front->left)
    }
}

2. Use map as lookup table

/*
change to use map for dice scheme lookup

AC: 0.015 220 KB 
*/


#include 
< cstdio >
#include 
< iostream >
#include 
< map >

#define  MAXNUM 100000
#define  MAXKIND 30

// use macro to define the ID calculation statement
#define  GETID(left, right, top, front, bottom) 
            left
* 6   +  right * 36   +  top * 216   +  front * 1296   +  bottom * 7776

using   namespace  std;


// global variables 
int  N = 0 ;   // dice number

int  inputs[MAXNUM];  // record the input dice's category
int  startpos[MAXKIND + 1 ]; // record the start item of each category

map
< int , int >  dicemap;   // use map instead of large hash lookup table

int  g_l = 0 , g_r = 0 , g_t = 0 , g_f = 0 , g_bm = 0 , g_bk = 0 ;


void  updatemap( int  l,  int  r,  int  t,  int  f,  int  bm,  int  bk,  int  idx);

void  main()
{
    dicemap.clear();
    
    map
<int,int>::const_iterator mapIter;

    
int i=0;
    
int curidx = 0;

    scanf(
"%d"&N);

    
for(i=0;i<N;++i)
    
{
        
//get left, right, top, front, bottom, back values from input
        scanf("%d"&g_l); scanf("%d"&g_r); scanf("%d"&g_t); scanf("%d"&g_f); scanf("%d"&g_bm); scanf("%d"&g_bk);

        mapIter 
= dicemap.find(GETID(g_l, g_r, g_t, g_f, g_bm));
        
if(mapIter == dicemap.end()) 
        
{
            updatemap(g_l,g_r,g_t,g_f,g_bm,g_bk,
++curidx);
            startpos[curidx] 
= i;
        }


        inputs[i] 
= dicemap[GETID(g_l, g_r, g_t, g_f, g_bm)];
    }


    
int start = 0;

    printf(
"%d ", curidx);

    
for(i=1;i<=curidx;++i)
    
{
        start 
= startpos[i];

        
while(start<N)
        
{
            
if(inputs[start] == i) printf("%d ",(start+1));

            
++start;
        }


        printf(
" ");
    }

}


void  updatemap( int  l,  int  r,  int  t,  int  f,  int  bm,  int  bk,  int  idx)
{
    
int i=0,j=0,k=0;
    
int temp =0;

    
for(i=0;i<4;++i)
    
{
        
for(j=0;j<4;++j)
        
{
            
for(k=0;k<4;++k)
            
{
                
if(dicemap.find(GETID(l, r, t, f, bm)) == dicemap.end())
                dicemap.insert(pair
<int,int>(GETID(l, r, t, f, bm),idx));
                
                
//rotate in direction 3 (left->top, top->right, right->bottom, bottom->left)
                temp = l;
                l 
= bm; bm = r; r = t; t = temp;
            }


            
//rotate in direction 2 (front->top, top->back, back->bottom, bottom->front)
            temp = f;
            f 
= bm; bm = bk; bk = t; t = temp;
        }


        temp 
= l;
        l 
= f; f = r; r = bk; bk = temp;
        
//rotate in direction 1 ( left->back, back->right, right->front, front->left)
    }

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用代码解决这个问题The program committee of the school programming contests, which are often held at the Ural State University, is a big, joyful, and united team. In fact, they are so united that the time spent together at the university is not enough for them, so they often visit each other at their homes. In addition, they are quite athletic and like walking. Once the guardian of the traditions of the sports programming at the Ural State University decided that the members of the program committee spent too much time walking from home to home. They could have spent that time inventing and preparing new problems instead. To prove that, he wanted to calculate the average distance that the members of the program committee walked when they visited each other. The guardian took a map of Yekaterinburg, marked the houses of all the members of the program committee there, and wrote down their coordinates. However, there were so many coordinates that he wasn't able to solve that problem and asked for your help. The city of Yekaterinburg is a rectangle with the sides parallel to the coordinate axes. All the streets stretch from east to west or from north to south through the whole city, from one end to the other. The house of each member of the program committee is located strictly at the intersection of two orthogonal streets. It is known that all the members of the program committee walk only along the streets, because it is more pleasant to walk on sidewalks than on small courtyard paths. Of course, when walking from one house to another, they always choose the shortest way. All the members of the program committee visit each other equally often. Input The first line contains the number n of members of the program committee (2 ≤ n ≤ 105). The i-th of the following n lines contains space-separated coordinates xi, yi of the house of the i-th member of the program committee (1 ≤ xi, yi ≤ 106). All coordinates are integers. Output Output the average distance, rounded down to an integer, that a member of the program committee walks from his house to the house of his colleague.
05-26

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值