Anda di halaman 1dari 2

IIITH Contest -I Solutions

Problem A The constraints for this problem are low enough that you can simulate the process straightforward. Have a 2D boolean array representing the board with 1 where there is a right turner and 0 where there is not. Start from the given point and move in the current direction till you hit a right turner or an edge of the board. If you hit a right turner, change your direction. We can show that the beam will not go into an infinite cycle. To show this, we can try tracing the beam backward and see that there is only a single place from which the beam could have come from at each point. Now if there was a cycle, then it should have come from outside the cycle the first time and then from inside. This is not possible, so there wont be any cycles. This observation is not required if you can take care of the cycle in some other way. Coding tip: Handling each direction using if cases will make the code very long. Instead, have an array which denotes difference in coordinates for the current direction. int dx[] = { 0,1,0,-1}, dy[] = {1, 0, -1, 0}; Lets denote the direction by dir. dir = 0 means up, 1 means right, 2 means down, 3 means left. When you have to move ahead in your current direction, you can do x += dx[dir], y += dy[dir] When you want to turn right, you can do dir = (dir + 1) % 4; Problem B Consider what happens when 2 people meet. If A is at position x with a box and B is at position x+1 without a box, after the transfer, A and B are still at the same positions but with the box exchanged. You can see that this is as good as A moving up and B moving down as though they never met each other. This means that you can assume that each person is working individually without caring about who else is there. Now, we have some people moving up and some others moving down. People moving up should first reach the top and then come down to start taking new ones, while the ones moving down can climb down straight and start the work. Calculate the first time they all reach the ground floor, say f[i]. Now after 2F*k+f[i] minutes, they would have each moved k boxes. We can find the minimum k which each person has to carry using B and N. Once this is done, you can sort the f[i]s and find out which people should carry the rest of the boxes. The time when the last of these people reach the top will be the answer. Problem C This was supposed to be the easiest after D, but it seems that most people didnt try it initially because the others didnt try :). Once we apply the permutation cipher on the original string, the position of the occurrence of each letter becomes irrelevant. We only need the count of each letter. When we apply the substitution cipher, the letters corresponding to the count also become irrelevant. You just need to verify that each count in the original string corresponds to a count in the new string. So count the number of occurrences of each letter, sort these counts and verify that the sorted counts of both the lists are equal. Problem D

By definition of Anagram the ordering of letters in the words can be ignored. Say we have two words A and B. Since the only operation allowed on a word is removal of letters we will have to remove the excessive occurrences of a given letter from the two words. For e.g if the letter a repeats x times in A and y times in B, max(x,y)-min(x,y) will contribute to the anagrammatic distance since that many occurrences of a will have to be removed. Problem E This is a mainly Parsing problem which requires to merely reformat the input. Since the input is very clearly defined we can use this to split the terms accordingly. The main fact to notice is that the terms are space separated. The first term (base string) can be simply stored in a string since it will repeat as such for every variable. The next term(s) can be processed one by one. The last character of such a term will be , or ; to indicate the continuation or end of further terms. Within such a term we need to split the variable name further. Since the variable name can contain only alphabets (capital or lower). This can also be easily split intro two strings. the second half has to be merely reversed and appended to the base string ( while reversing [] has to be retained as [] not as ][ ). Problem F: Problems such as this one scream for a binary search solution. Say we are given a quality q in addition to the input and we are to find the cheapest price for assembling a computer with all components with quality >= q. This is seems like a much simpler problem. After grouping the components by their type we have to choose from the set of components with quality >=q the one with cheapest price greedily. This would take O(n) time to calculate. Now for the actual problem we merely have to binary search for this value q . Clearly a computer with lesser q will be as cheap or cheaper than a computer with a higher q. This gives us our binary search criteria. So in this manner we can calculate the highest q which lies in our budget. The input contains strings as identifiers but this is not a problem since they can be easily mapped to integers by using map from C++ STL. once mapped. This way the component types can be easily handled since they are now integers.

Anda mungkin juga menyukai