shengdebao's blog

By shengdebao, history, 8 years ago, In English

Problem 1: Land Acquisition [Paul Christiano, 2007]

Farmer John is considering buying more land for the farm and has his eye on N (1 <= N <= 50,000) additional rectangular plots, each with integer dimensions (1 <= width_i <= 1,000,000; 1 <= length_i <= 1,000,000).

If FJ wants to buy a single piece of land, the cost is $1/square unit, but savings are available for large purchases. He can buy any number of plots of land for a price in dollars that is the width of the widest plot times the length of the longest plot. Of course, land plots cannot be rotated, i.e., if Farmer John buys a 3x5 plot and a 5x3 plot in a group, he will pay 5x5=25.

FJ wants to grow his farm as much as possible and desires all the plots of land. Being both clever and frugal, it dawns on him that he can purchase the land in successive groups, cleverly minimizing the total cost by grouping various plots that have advantageous width or length values.

Given the number of plots for sale and the dimensions of each, determine the minimum amount for which Farmer John can purchase all

PROBLEM NAME: acquire

INPUT FORMAT:

  • Line 1: A single integer: N

  • Lines 2..N+1: Line i+1 describes plot i with two space-separated integers: width_i and length_i

SAMPLE INPUT:

4 100 1 15 15 20 5 1 100

INPUT DETAILS:

There are four plots for sale with dimensions as shown.

OUTPUT FORMAT:

  • Line 1: The minimum amount necessary to buy all the plots.

SAMPLE OUTPUT:

500

OUTPUT DETAILS:

The first group contains a 100x1 plot and costs 100. The next group contains a 1x100 plot and costs 100. The last group contains both the 20x5 plot and the 15x15 plot and costs 300. The total cost is 500, which is minimal.


my solution gives a O(n^2) algorithm, and it's not good since n<=5*10^4. my solution sketch:

first we sort the pairs(from their width) consider 2 pairs the first with width w1 and length l1 and another with width w2 and l2, if w1 <= w2 & l1 <= l2 then we can buy these two lands together without considering the first land and paying w2*l2 so we can simply discard these situations as below

after we sort the lands we change it so that their width would be increasing and their length would be decreasing(this can be easily done with a stack...), after that we can conclude that any form of buying the lands is decomposing the lands into sequences and paying every sequence, for example if w1 < w2 < w3 < w4 and l1 > l2 > l3 > l4 one form of paying is joining 1 and 2 and then paying for 3 and then paying for 4, the sequnces are: [1..2] , [3..3] , [4..4] we can easily conclude if a form of buying is such that the lands joint, dont form a sequence by including the lands so that we can form a sequence results in decreasing the price,

now we can easily put a dp and update the dp with(O(n)) (how?) giving an algorithm of O(n^2),

the real dp algorithm should be less , plz help :)

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By shengdebao, history, 8 years ago, In English

The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges that tastes like a mixture between both of them.

A big topic of discussion inside the company is "How should the new creations be called?" A mixture between an apple and a pear could be called an apple-pear, of course, but this doesn't sound very interesting. The boss finally decides to use the shortest string that contains both names of the original fruits as sub-strings as the new name. For instance, "applear" contains "apple" and "pear" (APPLEar and apPlEAR), and there is no shorter string that has the same property. A combination of a cranberry and a boysenberry would therefore be called a "boysecranberry" or a "craboysenberry", for example.

Your job is to write a program that computes such a shortest name for a combination of two given fruits. Your algorithm should be efficient, otherwise it is unlikely that it will execute in the alloted time for long fruit names. Input Specification

Each line of the input file contains two strings that represent the names of the fruits that should be combined. All names have a maximum length of 100 and only consist of alphabetic characters. Input is terminated by end of file. Output Specification

For each test case, output the shortest name of the resulting fruit on one line. If more than one shortest name is possible, any one is acceptable. Sample Input

apple peach ananas banana pear peach

Sample Output

appleach bananas pearch

----------------- This is my solution but unfortunately I cant get ACCEPT ,

I simply make two vectors v and u indicating the index of the lcs in strings s and t (first string and the second string). I make these vectors by running a dp LCS algorithm that I think its correct at least I have not found any test case that proves sth else, after I've made these two vector I simply use three pointers one on the first char of s , another on the first char of string t, and another indicating the first index of LCS we will meet(on the vectors) while any of the pointers 1 or 2 point a character not on the LCS I simply add those characters to my result, else I add all the pointers and add that character of the LCS to my result

heres my code I'd be thankful if you could help me:

http://pastebin.com/raw/ZgJt6hak

P.S The reason why I'm not getting accept might be because of my input method :)

Full text and comments »

  • Vote: I like it
  • -8
  • Vote: I do not like it