Skip to main content

SRM 732 Round 1

I woke up early today for SRM ( 7 AM ) . I solved 2 problems DIV-2 Easy and Medium.

DIV-2 easy is just an implementation problem. 

Problem Statement of DIV-2 easy

     You found a deck of slightly non-traditional playing cards. Each card has a value and a suit. The value of each card is a positive integer, and the suit is a lowercase English letter ('a'-'z'). We will use (v, s) to denote a card with value v and suit s.

You want to know whether the deck is perfect. A perfect deck has two properties:
  • All cards in the deck are distinct. (I.e., no two cards share both value and suit.)
  • For any two cards (v1, s1) and (v2, s2) in the deck, the deck also contains the cards (v1, s2) and (v2, s1).

You are given the following data:
  • an int n: the number of cards in the deck
  • a vector <int> value with n elements: the values of the cards in the deck
  • a string suit with n elements: the suits of the cards in the deck
More precisely, for each valid i, (value[i], suit[i]) is one of the cards in the deck. Return "Perfect" if the deck is perfect and "Not perfect" otherwise. Note that the quotes are only for clarity and that the return value is case-sensitive.

Definition

    
Class: DeckOfCards
Method: IsValid
Parameters: int, vector <int>, string
Returns: string
Method signature: string IsValid(int n, vector <int> value, string suit)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256

Constraints

- n will be between 1 and 50, inclusive.
- value will contain exactly n elements.
- Each element of value will be between 1 and 1,000,000,000, inclusive.
- suit will be of length n exactly.
- Suit will only contain lower-case alphabets ('a'-'z').


My Solution : 
I used STL map to solve the problem. 




Problem Statement of DIV-2 Medium 

     You recently designed the Cube Stacking Game. This is a single-player puzzle. All you need to play the game is a collection of cubes, as described below.

You have n cubes. The cubes are numbered from 0 to n-1. Each face of each cube has a single color. There are n+1 colors. One of the colors is white and has number 0. The remaining colors have numbers 1 to n.

Each cube has exactly two white faces, and these are always two opposite faces. Let's number the remaining faces on each cube 1, 2, 3, 4 in adjacent order. (That is, face 1 is adjacent to 2, 2 to 3, 3 to 4, and 4 to 1.) For each cube, you are given the colors of these four faces: on cube i these faces have colors c1[i], c2[i], c3[i] and c4[i], in order. Remember that these faces are never white.

You want to build a single stack of cubes with cube 0 on the bottom, cube 1 on top of cube 0, and so on. (I.e., you have to use the cubes in the given order, starting with 0, and you are not allowed to skip any cube.)

The cubes have to be aligned to form a single stack with four vertical sides. You may rotate the cubes arbitrarily but you must place them onto the stack in such a way that the top and bottom face of each cube will be white. On each of the four sides of the stack each color can only appear at most once. Calculate and return the maximum height of a valid stack that can be built out of the given cubes.

Definition

    
Class: CubeStackingGame
Method: MaximumValue
Parameters: int, vector <int>, vector <int>, vector <int>, vector <int>
Returns: int
Method signature: int MaximumValue(int n, vector <int> c1, vector <int> c2, vector <int> c3, vector <int> c4)
(be sure your method is public)

Limits

    
Time limit (s): 2.000
Memory limit (MB): 256

Constraints

- n will be between 2 and 8, inclusive.
- Each of c1, c2, c3, c4 will contain exactly n elements.
- Each element of c1 will be between 1 and n, inclusive.
- Each element of c2 will be between 1 and n, inclusive.
- Each element of c3 will be between 1 and n, inclusive.
- Each element of c4 will be between 1 and n, inclusive.


My Solution:
n ( 2 <=n <= 8 ) is so small. So, I checked recursively all Five rotations.
4 rotations; where up and down side ( White color ) is constant. ( If we consider initial position also a rotation for easy to computation. ).
1 rotation; where up and down side is reverse.
In contest time, I had written code without checking the last rotation ( up and down side is reverse ) and got Wrong Answer for last test-case. Finally figured it by my

Rubik's Cube






Room Score Board 
DIV-2 Score Board 


Comments

Popular posts from this blog

Gray Code

A Gray code is a list of all 2^n  bit strings of length , where any two successive strings differ in exactly one bit (i.e., their Hamming distance is one).  source   The problem is, how to generate Gray Code for a given length n.  When n = 2 , Gray Code :  00  01  11  10 When n = 3, Gray Code : 000 001 011 010 110 111 101 100 000(0) position Gray code string 000  001(1) -> 001  010(2) -> 011  011(3) -> 010 .....  Key observation -  For the  K position, the Gray code string is S .   if i and (i+1)- th bit of K is equal then i- th char of S will be  ' 0 ', otherwise, it will be ' 1 '.   Code :               int n; cin >> n;     for(int i = 0; i < (1 << n); i++) {         int value =  ( i ^ (i >> 1) ) ;         string s = "";         for(int j = 0; j < n; j++) {             if( value & (1 << j) )s.push_back('1');             else s.push_back('0');         }         reverse( s.begin(), s.end() );      

Histogram - LOJ

It's a well-known problem and the well-known solution is using a stack. I tried to solve it by segment tree( iterative version for building tree ) and got TLE. Hope I can optimize the code.  TLE CODE