contest

1026. Questions and answers

http://acm.timus.ru/problem.aspx?space=1&num=1026
Time Limit: 2.0 second
Memory Limit: 16 MB

Background

The database of the Pentagon contains a top-secret information. We don’t know what the information is — you know, it’s top-secret, — but we know the format of its representation. It is extremely simple. We don’t know why, but all the data is coded by the natural numbers from 1 up to 5000. The size of the main base (we’ll denote it be N) is rather big — it may contain up to 100 000 those numbers. The database is to process quickly every query. The most often query is: "Which element is i-th by its value?"— with i being a natural number in a range from 1 to N.

Problem

Your program is to play a role of a controller of the database. In the other words, it should be able to process quickly queries like this.

Input

Input of the problem consists of two parts. At first, a database is written, and then there’s a sequence of queries. The format of database is very simple: in the first line there’s a number N, in the next N lines there are numbers of the database one in each line in an arbitrary order. A sequence of queries is written simply as well: in the first line of the sequence a number of queries K (1 ≤ K ≤ 100) is written, and in the next K lines there are queries one in each line. The query "Which element is i-th by its value?" is coded by the number i. A database is separated from a sequence of queries by the string of three symbols "#".

Output

The output should consist of K lines. In each line there should be an answer to the corresponding query. The answer to the query "i" is an element from the database, which is i-th by its value (in the order from the least up to the greatest element).

Sample

inputoutput
5
7
121
123
7
121
###
4
3
3
2
5

121
121
7
123
Problem Author: Leonid Volkov
Problem Source: Ural State University Internal Contest October'2000 Junior Session

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections;
using System.Globalization;
 
namespace acm.timus.ru_p1026
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList(100000);
            NumberFormatInfo nfi = NumberFormatInfo.InvariantInfo;
            string[] input = Console.In.ReadToEnd().Split(new char[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            int i = 0;
            int len1 = Int32.Parse(input[0]);
            for (i = 1; i <= len1; i++)
            {
                int n = Int32.Parse(input[i]);               
                list.Add(n);
            }
            int len2 = Int32.Parse(input[len1+2]) + len1 + 3;
            list.Sort();
            for (int j = len1 + 3; j < len2; j++)
            {
                int n = Int32.Parse(input[j])-1;
                Console.WriteLine(list[n]);
            }
        }
    }
}

Problem 100: UVA Online Judge

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=36

 The 3n + 1 problem 

 

Background

Problems in Computer Science are often classified as belonging to a certain class of problems (e.g., NP, Unsolvable, Recursive). In this problem you will be analyzing a property of an algorithm whose classification is not known for all possible inputs. 

The Problem

Consider the following algorithm:

 
1. input n
2. print n
3. if n = 1 then STOP
4. if n is odd then n <-- 3n + 1
5. else n <-- n / 2
6. GOTO 2

 

Given the input 22, the following sequence of numbers will be printed 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

It is conjectured that the algorithm above will terminate (when a 1 is printed) for any integral input value. Despite the simplicity of the algorithm, it is unknown whether this conjecture is true. It has been verified, however, for all integers n such that 0 < n < 1,000,000 (and, in fact, for many more numbers than this.)

Given an input n, it is possible to determine the number of numbers printed (including the 1). For a given n this is called the cycle-length of n. In the example above, the cycle length of 22 is 16.

For any two numbers i and j you are to determine the maximum cycle length over all numbers between i and j.  

The Input

The input will consist of a series of pairs of integers i and j, one pair of integers per line. All integers will be less than 1,000,000 and greater than 0.

You should process all pairs of integers and for each pair determine the maximum cycle length over all integers between and including i and j.

You can assume that no operation overflows a 32-bit integer. 

The Output

For each pair of input integers i and j you should output i, j, and the maximum cycle length for integers between and including i and j. These three numbers should be separated by at least one space with all three numbers on one line and with one line of output for each line of input. The integers i and j must appear in the output in the same order in which they appeared in the input and should be followed by the maximum cycle length (on the same line). 

Sample Input

1 10
100 200
201 210
900 1000

Sample Output

1 10 20
100 200 125
201 210 89
900 1000 174

 

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
 
unsigned long findMaxCycleLength(unsigned long min, unsigned long max);
unsigned long findCycleLength(unsigned long n);
     
int main(int argc, char** argv)
{
        unsigned long min, max, num = 0;
 
        while(scanf("%li %li", &min, &max) != EOF)
        {
                printf("%li %li ", min, max);
                if(min > max)
                {       
                        min^=max;
                        max^=min;
                        min^=max;
                }
                num = findMaxCycleLength(min, max);
                printf("%li\n", num);
        }
}
 
unsigned long findMaxCycleLength(unsigned long min, unsigned long max)
{
        unsigned long temp, i;
        unsigned long n = findCycleLength(max);
 
        for(i = min; i < max; i++)
        {
                temp = findCycleLength(i);
                if(temp > n)
                {       
                        n = temp;
                }
        }
 
        return n;
}
 
unsigned long findCycleLength(unsigned long n)
{
        if(n == 0) return 0;
        unsigned long count = 1;
        while(n != 1)
        {
                if(n%2 == 1)
                {
                        n = 3*n+1;
                }
                else
                {
                        n = n/2;
                }
                count++;
        }
        return count;
}