The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550

Game Rooms

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 305    Accepted Submission(s): 84


Problem Description
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(1T100)T test cases follow. Each test case begins with one line with an integer N(2N4000), the number of floors in the building. N lines follow, each consists of 2 integers, Ti and Pi(1Ti,Pi109), 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.
 
题意:有n层楼,每层楼有人想要打乒乓球或者羽毛球。
每层楼可以改造成一种场地(羽毛球场或者乒乓球场),容量无限大。
每个人可以上下楼。
问,这些人都进行自己想要进行的运动总共要走多少楼。
分析:显然,每个人都会选择离自己最近的场地。
有一个显然的dp
dp[i][0..1]表示前i层,第i层建造羽毛球场或者乒乓球场(0..1)的答案。
但是直接这样做的话会有后效性。
因为如果i与i+1种类不一样的话,i之前的某一段可能会选择向上走,而不是向下走。
为此,我们默认dp[i][0..1]得到答案是默认i与i+1不同的答案。
转移枚举一个j,表示j+1到i都建造一样的,而j和i+1都与它们不一样,如此,j+1到i这一段的人(需要转移场地的),必定一半向上一半向下。
这个代价是可通过预处理以O(1)算的的。
 
问题来了,这个dp还是O(n^2)的。
 
但是我不会优化了。。。比赛快要截止时,试了一发。。。居然AC了。。。
 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 const int N = 4010;
 6 typedef long long LL;
 7 const LL MLL = 1000000000000000001LL;
 8 int n, arr[N][2];
 9 LL distDown[N][2], distUp[N][2], sum[N][2], dp[N][2], ans;
10 
11 inline LL Up(int l, int r, bool s)
12 {
13     if(l > r) return 0;
14      return (distUp[l][s ^ 1] - distUp[r + 1][s ^ 1]) - 
15             (sum[r][s ^ 1] - sum[l - 1][s ^ 1]) * (n - r);
16 }
17 
18 inline LL Down(int l, int r, bool s)
19 {
20     if(l > r) return 0;
21     return (distDown[r][s ^ 1] - distDown[l - 1][s ^ 1]) - 
22            (sum[r][s ^ 1] - sum[l - 1][s ^ 1]) * (l - 1);
23 }
24 
25 inline LL Calc(int l, int r, bool state)
26 {
27     if(l == 1)
28     {
29         return Up(l, r, state);
30     }
31     int mid = (l + r) >> 1;
32     return Up(mid + 1, r, state) + Down(l, mid, state);
33 }
34 
35 inline void Solve()
36 {
37     scanf("%d", &n);
38     for(int i = 1; i <= n; i++)
39         for(int j = 0; j < 2; j++) scanf("%d", &arr[i][j]);
40     
41     for(int i = 0; i < 2; i++)
42     {
43         distDown[0][i] = 0;
44         for(int j = 1; j <= n; j++)
45         {
46             distDown[j][i] = distDown[j - 1][i] + 1LL * arr[j][i] * j;
47             sum[j][i] = sum[j - 1][i] + arr[j][i];
48         }
49         distUp[n + 1][i] = 0;
50         for(int j = n; j >= 1; j--)
51             distUp[j][i] = distUp[j + 1][i] + 1LL * arr[j][i] * (n + 1 - j);
52     }
53     
54     ans = MLL;
55     dp[0][0] = dp[0][1] = 0;
56     for(int i = 1; i < n; i++)
57     {
58         dp[i][0] = dp[i][1] = MLL;
59         for(int j = 0; j < i; j++)
60         {
61             dp[i][0] = min(dp[i][0], dp[j][1] + Calc(j + 1, i, 0));
62             dp[i][1] = min(dp[i][1], dp[j][0] + Calc(j + 1, i, 1));
63         }
64         
65         ans = min(ans, dp[i][0] + Down(i + 1, n, 1));
66         ans = min(ans, dp[i][1] + Down(i + 1, n, 0));
67     }
68     cout << ans << endl;
69 }
70 
71 int main()
72 {
73     int test;
74     scanf("%d", &test);
75     for(int testnumber = 1; testnumber <= test; testnumber++)
76     {
77         printf("Case #%d: ", testnumber);
78         Solve();
79     }
80     return 0;
81 }
View Code

 

 
posted @ 2015-12-22 21:18  yanzx6  阅读(341)  评论(0编辑  收藏  举报