Cdoj 1225 Game Rooms

Source: Internet
Author: User

Game RoomsTime limit:4000/4000ms (java/others) Memory limit:65535/65535kb (java/others)

Your company have just constructed a new skyscraper, but you just noticed a terrible problem:there are only space to put on E Game floor! The game rooms has not been furnished yet, so can still decide which ones should is for table tennis and which ones s Hould is for pool. There must is at least one game, each of the type in the building.

Luckily, you know the WHO would work where the this building (everyone have picked out offices). You know that there'll be Ti table tennis players and Pi pool players on each floor. Our goal are to minimize the sum of distances for each employee to their nearest game. The distance is the difference-in floor numbers:0 if an employee are on the same floor as a game-class of their desired Typ E, 1 if the nearest game desired type is exactly one floor above or below the employee, and so on.

Input
The first line of the input gives the number of test cases, $T (1\leq T\leq 100) $. T test Cases follow. Each test case is begins with a line with a integer $N (2\leq n\leq 4000) $, the number of floors in the building. N lines Follow, each consists of 2 integers, $Ti and Pi (1\leq T_i,p_i\leq 10^9) $, the number of table tennis and pool play ERs on the ith floor. The lines is given in increasing order of floor number, starting with floor 1 and going upward.

Output
For each test case, output one line containing case #x: Y, where x is the test Case number (starting from 1) and Y is the Minimal sum of distances.

Sample Input

1210 54 3

Sample Output

Case #1:9

Hint
In the first case, you can build a table tennis game class on the first floor and a pool game class on the second floor. The 5 pool players on the first floor would need to go one floor up, and the 4 table tennis players on the SE Cond floor would need to go. So the total distance is 9.

SourceThe China collegiate Programming Contest Problem Solving: Dynamic programming Dafa really invincible dp[i][0] The first layer I put 0, the first i+1 layer put 1, so the front of 0 layer of those who play 1 to go to i+1 to play 1, This is the DP initialization then transfer DP[I][0] can be transferred by dp[j][1], because the J layer 1,i+1 layer put 1, so j+1 to I play 1 people have two places, averaging to J and i+1 two floors better this is the transfer so finally due to dp[i][0], The I layer is 0 and the i+1 layer is 1, so the i+1 layer 0 to I to play 0, take the smallest
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 ConstLL INF = ~0ull>>2;5 Const intMAXN =4010;6LL a[maxn][2],sum[maxn][2],f[maxn][2],dp[maxn][2];7LL S (intAintBintx) {8     returnSUM[B][X]-sum[a-1][x];9 }TenLL Cost (intAintBintx) { One     if(A = = b)return 0; A     if(A < b)returnS (a,b,x) *b-f[b][x] + f[a-1][X];// up -     returnF[A][X]-f[b-1][X]-S (b,a,x) *b;// Down - } the intMain () { -     intKase,n,cs =1; -scanf"%d",&Kase); -      while(kase--){ +scanf"%d",&n); -          for(inti =1; I <= N; ++i) { +scanf"%lld%lld", &a[i][0],&a[i][1]); Asum[i][0] = sum[i-1][0] + a[i][0]; atsum[i][1] = sum[i-1][1] + a[i][1]; -f[i][0] = f[i-1][0] + a[i][0]*i; -f[i][1] = f[i-1][1] + a[i][1]*i; -         } -LL ret =INF; -          for(inti =1; I < n; ++i) { indp[i][0] = Cost (1, i +1,1); -dp[i][1] = Cost (1, i +1,0); to              for(intj =1; J < I; ++j) { +                 intMid = (i + j +1) >>1; -dp[i][0] = min (dp[i][0],dp[j][1] + cost (mid,j,1) + Cost (Mid +1, i +1,1)); thedp[i][1] = min (dp[i][1],dp[j][0] + cost (mid,j,0) + Cost (Mid +1, i +1,0)); *             } $ret = min (ret,dp[i][0] + cost (n,i,0));Panax Notoginsengret = min (ret,dp[i][1] + cost (n,i,1)); -         } theprintf"Case #%d:%lld\n", cs++, ret); +     } A     return 0; the}
View Code

Cdoj 1225 Game Rooms

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.