SRM 451 Div 2 1000

Source: Internet
Author: User

Question: http://www.topcoder.com/stat? C = problem_statement & PM = 10634.

Solution: let assume we have only one boy and he delivers a set S of pizza from the given pizzas. let M be the size of the Set S. let Ts be the time needed to deliver all the pizzas from set S. ti = shortest time from the restaurant to I-th pizza which he delivers at I-th step during delivering. now we know Ts = 2 * t1 + 2 * t2 + .... + 2 * Tm-1 + TM. we can minimize ts by delivering in a order such that t1 <= t2 <=... <= Tm-1 <= TM. now, consider we have 2 boys namely A and B and SA = set of pizza boy a delivers and SB = set of pizza boy B delivers. to deliver all pizzas we need time equals to maximum of TA and TB. we can apply bruteforce on given set of pizzas to partition it into two sets and get the required answer. code: # include <cstdlib> # include <cctype> # include <cstring> # include <cstdio> # include <cmath> # include <Algorithm> # include <vector> # include <string> # include <iostream> # include <sstream> # include <map> # include <set> # include <queue> # include <stack> # include <fstream> # include <numeric> # include <iomanip> # include <bitset> # include <list> # include <sstream> # include <stdexcept> # include <functional> # include <utility> # include <ctime> using namespace STD; const int DX [] = {1,-1, 0}; const int Dy [] = {0, 0, 1,-1}; # define Pb push_back # define MP make_pair # define rep (I, n) for (I = 0; I <(n); ++ I) # define for (I, L, H) for (I = (l); I <= (h); ++ I) # define Ford (I, H, L) for (I = (h); I >= (l); -- I) typedef vector <int> VI; typedef vector <string> vs; typedef vector <double> VD; typedef long ll; typedef pair <int, int> PII; char map [55] [55]; int dis [55] [55]; int Dist [30]; int totcost [(1 <20) + 5]; int e [30]; bool used [55] [55]; int n, m, TOT; bool valid (int x, int y) {return x> = 0 & x <n & Y> = 0 & Y <m;} bool passable (INT ax, int ay, int BX, int) {If (Map [ax] [AY] = '$' | map [BX] [by] = '$') return true; if (Map [ax] [AY] = 'X' | map [BX] [by] = 'X') return true; if (Map [ax] [AY] + 1 = map [BX] [by] | map [ax] [AY] = map [BX] [by] + 1) return true; return map [ax] [AY] = map [BX] [by];} int cost (INT ax, int ay, int BX, int) {If (Map [Ax] [AY] = '$' | map [BX] [by] = '$') return 2; if (Map [ax] [AY] = 'X' | map [BX] [by] = 'X') return 2; if (Map [ax] [AY] + 1 = map [BX] [by] | map [ax] [AY] = map [BX] [by] + 1) return 3; return 1;} class pizzadelivery {public: int deliverall (vector <string> terrain) {n = (INT) terrain. size (); M = (INT) terrain [0]. size (); For (INT I = 0; I <n; ++ I) for (Int J = 0; j <m; ++ J) map [I] [J] = (char) terrain [I] [J]; memset (used, false, sizeof (US Ed); memset (DIS, 0xff, sizeof (DIS); For (INT I = 0; I <n; ++ I) for (Int J = 0; j <m; ++ J) if (Map [I] [J] = 'X') dis [I] [J] = 0; For (INT I = 0; I <n * m; ++ I) {int X, Y, min =-1; for (Int J = 0; j <n; ++ J) for (int K = 0; k <m; ++ K) if (! Used [J] [k] & dis [J] [k]> = 0 & (min =-1 | dis [J] [k] <min )) {min = dis [J] [k]; X = J; y = K;} If (min =-1) break; used [x] [Y] = true; for (int K = 0; k <4; ++ K) if (valid (x + dx [K], Y + dy [k]) & passable (X, y, x + dx [K], Y + dy [k]) if (DIS [x + dx [k] [Y + dy [k] <0) dis [x + dx [k] [Y + dy [k] = dis [x] [Y] + cost (X, Y, x + dx [K], Y + dy [k]); else dis [x + dx [k] [Y + dy [k] = min (DIS [x + dx [k] [Y + dy [k], dis [x] [Y] + cost (X, Y, x + dx [K], Y + dy [k]);} tot = 0; for (INT I = 0; I <n; ++ I) for (Int J = 0; j <m; ++ J) if (Map [I] [J] = '$ ') dist [tot ++] = dis [I] [J]; for (INT I = 0; I <tot; ++ I) if (Dist [I] <0) return-1; E [0] = 1; for (INT I = 1; I <tot; ++ I) E [I] = E [I-1] * 2; sort (Dist, DIST + ToT); For (INT I = 0; I <(1 <ToT); ++ I) {totcost [I] = 0; for (Int J = 0; j <tot; ++ J) if (I & E [J]) totcost [I] + = DIST [J] * 2; for (Int J = tot-1; j> = 0; -- j) if (I & E [J]) {totcost [I]-= DIST [J]; break ;}} int ans = totcost [(1 <ToT)-1], stat = (1 <tot )-1; for (INT I = 0; I <(1 <ToT); ++ I) ans = min (ANS, max (totcost [I], totcost [stat-I]); Return ans ;}}; question: I think thers are always DP for these kind of problems, for this one, suppose there are n jobs for 2 worker to do, contained in T [N], and the max time cost will not exceed M, so we can define the State: time [index, _ 1 time, _ 1 maxindex, _ 2 maxindex]: = by doing the 0 ~ Index works, the first worker used _ 1 time to do some, and his Max time index is _ 1 maxindex in T; The _ 2 maxindex refer to the second worker, the value of the time means the time that the second worker to do. so there goes: int secondtime = time [index, _ 1 time, _ 1 maxindex, _ 2 maxindex]; If (T [index + 1] is for first worker) if (T [index + 1]> T [_ 1 maxindex]) time [index + 1, _ 1 time + T [_ 1 maxindex] + T [index + 1], index + 1, _ 2 maxindex] = Secondtime; elsetime [index + 1, _ 1 time + 2 * t [index + 1], _ 1 maxindex, _ 2 maxindex] = secondtime; if (T [index + 1] is for second worker) if (T [index + 1]> T [_ 2 maxindex]) time [index + 1, _ 1 time, _ 1 maxindex, index + 1] = secondtime + T [_ 2 maxindex] + T [index + 1]; elsetime [index + 1, _ 1 time, _ 1 maxindex, _ 2 maxindex] = secondtime + 2 * t [index + 1]; by using 0 ~ 1 index flip, the total space useage is 2 * n * m, n refers to the count of the works to do (the count of the '$' here) M refers to the time cost for one worker to do all the job. yeah. another DP approach was knackpack problem in yje following way: Last Delivery for first boy is the largest one. we loop over all other deliveries checking the answer if chosen delivery is the last one. how we do it: 1 ). we f Ill up the "knackpack" with all delivery times before t chosen one, And We shoshould use the biggest one. we check two times we can get which are closest to total_delivery_times/2 and suppose its total time needed for the first boy to deliver pizzas. knowing it we can find times the second boy needs. maximum of these 2 values is the answer for this chosen last delivery. for more clarification you can s Ee my solution in practise room. Is there a dynamic planning solution for this problem ?!!! Waiting for thinking!

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.