CS
: 50583553
CsWiki :: LogIn
This page contains information I used to solve UVA problems.

 Table of content 

1. UVA 101 The Blocks Problem

http://acm.uva.es/p/v1/101.html

Key to solve:
I'd say "Understand the English" is the key. The movement is as follows:

Here is the code to simulate the movement
if (locations[a] != locations[b]) { // pop out top of b // a and b are on different stack isMove = strcmp ("move", action) == 0; isOnto = strcmp ("onto", param) == 0; if (isMove) { if (isOnto) { // move a onto b //pop out top of b stackno = locations[b]; //select the stack contains a top = size[stackno] - 1;// index of the topmost block on that stack // pop out a block from the top until there is no more block or a is met while (top >= 0 && blocks[stackno][top] != b) { box = blocks[stackno][top--]; // take out the block --size[stackno];// pop out the topmost box blocks[box][size[box]] = box; // puts on top of its initial positions ++size[box]; locations[box] = box; } //pop out top of a stackno = locations[a]; //select the stack contains a top = size[stackno] - 1;// index of the topmost block on that stack // pop out a block from the top until there is no more block or a is met while (top >= 0 && blocks[stackno][top] != a) { box = blocks[stackno][top--]; // take out the block --size[stackno];// pop out the topmost box blocks[box][size[box]] = box; // puts on top of its initial positions ++size[box]; locations[box] = box; } // assert blocks[stackno][top] is a --size[locations[a]]; blocks[locations[b]][size[locations[b]]++] = a; locations[a] = locations[b]; } else { //pop out top of a stackno = locations[a]; //select the stack contains a top = size[stackno] - 1;// index of the topmost block on that stack // pop out a block from the top until there is no more block or a is met while (top >= 0 && blocks[stackno][top] != a) { box = blocks[stackno][top--]; // take out the block --size[stackno];// pop out the topmost box blocks[box][size[box]] = box; // puts on top of its initial positions ++size[box]; locations[box] = box; } --size[locations[a]]; // put a on top of the top of b blocks[locations[b]][size[locations[b]]] = a; ++size[locations[b]]; locations[a] = locations[b]; } } else { if(isOnto) { //pop out top of b stackno = locations[b]; //select the stack contains a top = size[stackno] - 1;// index of the topmost block on that stack // pop out a block from the top until there is no more block or a is met while (top >= 0 && blocks[stackno][top] != b) { box = blocks[stackno][top--]; // take out the block --size[stackno];// pop out the topmost box blocks[box][size[box]] = box; // puts on top of its initial positions ++size[box]; locations[box] = box; } } // pile a onto b stackno = locations[a]; //select the stack contains a top = 0; while (blocks[stackno][top++] != a); //top is the index of a; // cop from a to its top to b stack for (i = top - 1; i < size[stackno]; ++i) { blocks[locations[b]][size[locations[b]]++] = blocks[stackno][i]; locations[blocks[stackno][i]] = locations[b]; } size[stackno] = --top; }


2. UVA 299 Train Swapping

http://acm.uva.es/p/v2/299.html

Key to solve:
I see that question is about sorting a list of numbers in accending order. I get accepted using Bubble sort and insertion sort.

Bubble sort
void bubbleSort(int numbers[], int array_size) { int i, j, temp; for (i = (array_size - 1); i >= 0; i--) { for (j = 1; j <= i; j++) { if (numbers[j-1] > numbers[j]) { temp = numbers[j-1]; numbers[j-1] = numbers[j]; numbers[j] = temp; ++swap; } } } }


Insertion sort
swap = 0; for (i = 1; i < n; i++) { index = a[i]; j = i; while ((j > 0) && (a[j-1] > index)) { a[j] = a[j-1]; j = j - 1; ++swap; } a[j] = index; }


3. UVA 401 Palindromes

http://acm.uva.es/p/v4/401.html

Key to solve:
The key is to understand what a palindrome string and a mirrored string is.

Check palindrome
left = 0; right = length - 1; while (str[left] == str[right]) { ++left; --right; } isPalindrome = left > right;


Check mirrored string
left = 0; right = length - 1; while (right >= 0 && str[left] == reverse[str[right]]) { --right; ++left; } isMirrored = right < 0;


4. UVA 414 Machined Surfaces

http://acm.uva.es/p/v4/414.html

Key to solve:
What will happen when two surfaces contact each other? The spacing between them is 0. In other words, the spacing is all consumed. The question is changed to ask what is the minimum spacing between each row of the two surfaces. When you get that minimum, all you have to do is to subtract it by the spacing of each rows. That is your anwser.

5. UVA 621 Secret Research

http://acm.uva.es/p/v6/621.html

Key to solve:
The input is all valid code, so checking the length and the first and last character or last two charaters is enough to solve.

Decode
if (length == 1 || length == 2) { puts ("+"); } else if (str[length - 2] == '3' && str[length - 1] == '5') { puts ("-"); } else if (str[0] == '9' && str[length - 1] == '4') { puts ("*"); } else if (str[0] == '1' && str[1] == '9' && str[2] == '0') { puts ("?"); }


6. UVA 10127 Ones

http://acm.uva.es/v101/10127.html

Key to solve:
How to do division by hands? For example, 111 / 3, you will do 1/3 first, then 11/3. Another example, 1111/56, you will do 1/56, 11/56, 111/56. Get it yet?

one = 1; dividen = 1; while (1) { remainder = dividen % n; if (!remainder) { break; } ++one; dividen = remainder * 10 + 1; }


7. UVA 10700 Camel trading

http://acm.uva.es/p/v107/10700.html

Key to solve:
max: perform all additoins, then do multilpication
min: perform all multiplication, then do addition

count = vsize = nsize1 = nsize2 = 0; doMul = doAdd = false; while (count < length) { c = str[count++]; if (c == '+' || c == '*') { number1[nsize1++] = atoi (value); number2[nsize2++] = atoi (value); if (doAdd) { number1[nsize1 - 2] += number1[nsize1 - 1]; --nsize1; } if (doMul) { number2[nsize2 - 2] *= number2[nsize2 - 1]; --nsize2; } doAdd = c == '+'; doMul = c == '*'; // clear value for (j = 0; j < vsize; ++j) { value[j] = 0; } vsize = 0; } else { // extract numbers value[vsize++] = c; } } number1[nsize1++] = atoi (value); number2[nsize2++] = atoi (value); if (doAdd) { number1[nsize1 - 2] += number1[nsize1 - 1]; --nsize1; } else if (doMul) { number2[nsize2 - 2] *= number2[nsize2 - 1]; --nsize2; } max = 1; min = 0; for (j = 0; j < nsize1; ++j) { max *= number1[j]; } for (j = 0; j < nsize2; ++j) { min += number2[j]; }


8. UVA 10812 Beat the Spread!

http://acm.uva.es/p/v108/10812.html

Remember score is non-negative integer. That is it won't have a decimal places.
Let,

a + b = s;
a - b = d;

a = (s + d) / 2
b = (s - d) / 2

My implementation is here
sum = s + d; if (sum & 1 || d > s || s < 0 || d < 0) { puts ("impossible"); } else { printf ("%d %d\n", sum >> 1, (s - d) >> 1); }


9. UVA 10919 Prerequisites

http://acm.uva.es/p/v109/10919.html

Key to solve:
Treat every single category as a large array of course codes. To see wheather the minumum number of course has taken or not, just go through every selected courses, count the number of fullfilment. Then repeat this process for every category.

Note: Without early break of checking
for (j = 0; j < ncourse; ++j) { if (table[mychoice[j]] == 1) { --min; } } fulfil = min <= 0;