POJ 3253 Fence Repair

Source: Internet
Author: User

Fence RepairTime Limit: 2000 MS Memory Limit: 65536 KTotal Submissions: 17270 Accepted: 5483 Description Farmer John wants to repair a small length of the fence around the pasture. he measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. he then purchases a single long board just long enough to saw into the N planks (I. E ., whose length is the sum of the lengths Li ). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you shoshould ignore it, too. FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw. farmer Don, a closet capitalist, doesn't lend FJ a saw but instead Offers to charge Farmer John for each of the N-1 cuts in the plank. the charge to cut a piece of wood is exactly equal to its length. cutting a plank of length 21 costs 21 cents. farmer Don then lets Farmer John decide the order and locations to cut the plank. help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various d Ifferent orders which will result in different charges since the resulting intermediate planks are of different lengths. input Line 1: One integer N, the number of planks Lines 2 .. N + 1: Each line contains a single integer describing the length of a needed plankOutput Line 1: One integer: the minimum amount of money he must spend to make N-1 cutsSample Input 3858 Sample Output 34 Hint He wants to cut A board of length 21 into pieces of lengths 8, 5, and 8. the original board measures 8 + 5 + 8 = 21. the first cut will cost 21, and shoshould be used to cut the board into pieces measuring 13 and 8. the second cut will cost 13, and shoshould be used to cut the 13 into 8 and 5. this wocould cost 21 + 13 = 34. if the 21 was cut into 16 and 5 instead, the second cut wocould cost 16 for a total of 37 (which is more 34 ). source USACO 2006 November Gold: when I started to do this, I thought of greed, but I was greedy and wrong. My strategy was to find a maximum value every time. After several wrong times, I looked at discuss and found that it was not so greedy. When it was correct, the greedy principle was to find two minimum values each time, and then add them to the array to find two minimum values. This idea is the same as that of the Harman tree. Because no template is used, I checked the time when I handed in the question, running more than 300 [cpp] # include <iostream> # include <cstring> # include <stdlib. h> # include <stdio. h> using namespace std; int a [20010]; int cmp (const void * e, const void * f) {return (* (int *) e-* (int *) f);} int main () {int I, j, n, m, t; _ int64 sum, s; scanf ("% d", & n ); for (I = 0; I <= n-1; I ++) {scanf ("% d", & a [I]) ;} qsort (a, n, sizeof (a [0]), cmp); sum = 0; for (I = 1; I <= n-1; I ++) {s = (a [I-1] + a [I]); sum + = s; for (j = I + 1; j <= n-1; j ++) {if (a [j] <s) {a [J-1] = a [j];} else {break;} a [J-1] = s ;} printf ("% I64d \ n", sum); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.