Game Rooms
Time Limit: 4000/4000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Your company has just constructed a new skyscraper, but you just noticed a terrible problem: there is only space to put one game room on each floor! The game rooms have not been furnished yet, so you can still decide which ones should be for table tennis and which ones should be for pool. There must be at least one game room of each type in the building.
Luckily, you know who will work where in this building (everyone has picked out offices). You know that there will be Ti table tennis players and Pi pool players on each floor. Our goal is to minimize the sum of distances for each employee to their nearest game room. The distance is the difference in floor numbers: 0 if an employee is on the same floor as a game room of their desired type, 1 if the nearest game room of the 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(1leq Tleq 100)$. T test cases follow. Each test case begins with one line with an integer $N(2leq Nleq 4000)$, the number of floors in the building. N lines follow, each consists of 2 integers, $Ti and Pi(1leq T_i,P_ileq 10^9)$, the number of table tennis and pool players on the ith floor. The lines are 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
1 2 10 5 4 3
Sample Output
Case #1: 9
Hint
In the first case, you can build a table tennis game room on the first floor and a pool game room on the second floor. In this case, the 5 pool players on the first floor will need to go one floor up, and the 4 table tennis players on the second floor will need to go one floor down. So the total distance is 9.
Source
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL INF = ~0ULL>>2; 5 const int maxn = 4010; 6 LL a[maxn][2],sum[maxn][2],f[maxn][2],dp[maxn][2]; 7 LL S(int a,int b,int x){ 8 return sum[b][x] - sum[a-1][x]; 9 } 10 LL cost(int a,int b,int x){ 11 if(a == b) return 0; 12 if(a < b) return S(a,b,x)*b - f[b][x] + f[a-1][x];//up 13 return f[a][x] - f[b-1][x] - S(b,a,x)*b;//down 14 } 15 int main(){ 16 int kase,n,cs = 1; 17 scanf("%d",&kase); 18 while(kase--){ 19 scanf("%d",&n); 20 for(int i = 1; i <= n; ++i){ 21 scanf("%lld%lld",&a[i][0],&a[i][1]); 22 sum[i][0] = sum[i-1][0] + a[i][0]; 23 sum[i][1] = sum[i-1][1] + a[i][1]; 24 f[i][0] = f[i-1][0] + a[i][0]*i; 25 f[i][1] = f[i-1][1] + a[i][1]*i; 26 } 27 LL ret = INF; 28 for(int i = 1; i < n; ++i){ 29 dp[i][0] = cost(1,i + 1,1); 30 dp[i][1] = cost(1,i + 1,0); 31 for(int j = 1; j < i; ++j){ 32 int mid = (i + j + 1)>>1; 33 dp[i][0] = min(dp[i][0],dp[j][1] + cost(mid,j,1) + cost(mid + 1,i + 1,1)); 34 dp[i][1] = min(dp[i][1],dp[j][0] + cost(mid,j,0) + cost(mid + 1,i + 1,0)); 35 } 36 ret = min(ret,dp[i][0] + cost(n,i,0)); 37 ret = min(ret,dp[i][1] + cost(n,i,1)); 38 } 39 printf("Case #%d: %lld ",cs++,ret); 40 } 41 return 0; 42 }