algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

problem.txt (3079B)


      1 C. Looking for Order
      2 time limit per test
      3 4 seconds
      4 memory limit per test
      5 512 megabytes
      6 
      7 Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess — all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag — her inherent sense of order does not let her do so.
      8 
      9 You are given the coordinates of the handbag and the coordinates of the objects in some Сartesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period.
     10 Input
     11 
     12 The first line of the input file contains the handbag's coordinates xs, ys. The second line contains number n (1 ≤ n ≤ 24) — the amount of objects the girl has. The following n lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer.
     13 Output
     14 
     15 In the first line output the only number — the minimum time the girl needs to put the objects into her handbag.
     16 
     17 In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to n), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them. 
     18 
     19 ---
     20 
     21 - thoughts:
     22     - there are objects in a room that have to be placed in a container
     23     - the girl can only carry at most two objects at a time
     24     - container can't move
     25     - if an object is picked up, it must be moved directly to the handbag, not pickup / set down
     26     - cover distance between two objects in time equal to squared length of the segment connecting the objects
     27         - euclidean distance squared
     28     - girl starts at the handbag
     29     - find operations that minimizes time to get all objects in container
     30 
     31 ---
     32 
     33 examples
     34 
     35 0 0
     36 2
     37 1 1
     38 -1 1
     39 
     40 handbag location = 0,0
     41 n = 2
     42 object 1 location =  1,1
     43 object 2 location = -1,1
     44 
     45 0,0 -> 1,1 -> -1,1 -> 0,0
     46 
     47 distances at each step:
     48 
     49 (1**2 + 1 ** 2) + (2**2 + 0) + (1**2 + 1 ** 2)
     50 
     51 2 + 4 + 2 = 8
     52 
     53 sum = 8
     54 0 1 2 0
     55 
     56 (we start at zero, get 1, get 2, end at 0)
     57 
     58 ---
     59 
     60 1 1
     61 3
     62 4 3
     63 3 4
     64 0 0
     65 
     66 hb location = 1,1
     67 o1 = 4,3
     68 o1 = 3,4
     69 o2 = 0,0
     70 
     71 do we always want to pickup as many as we can? not necessarily because in some cases where there are > 3 elements we want to get one and then the other two in another swoop
     72 
     73 1,1 -> 3,4 -> 4,3 -> 1,1 -> 0,0 -> 1,1
     74 
     75 2, 3
     76 1, 1
     77 3, 2
     78 1, 1
     79 1, 1
     80 
     81 4 + 9 + 1 + 1 + 9 + 4 + 1 + 1 + 1 + 1 = 32
     82 
     83 0 2 1 0 3 0