The 2015 China Collegiate Programming Contest K Game Rooms hdu 5550

时间:2023-03-08 23:53:52
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(1≤T≤100). T test cases follow. Each test case begins with one line with an integer N(2≤N≤4000), the number of floors in the building. N lines follow, each consists of 2 integers, Ti and Pi(1≤Ti,Pi≤109), 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了。。。
 #include <cstdio>
#include <iostream>
using namespace std; const int N = ;
typedef long long LL;
const LL MLL = 1000000000000000001LL;
int n, arr[N][];
LL distDown[N][], distUp[N][], sum[N][], dp[N][], ans; inline LL Up(int l, int r, bool s)
{
if(l > r) return ;
return (distUp[l][s ^ ] - distUp[r + ][s ^ ]) -
(sum[r][s ^ ] - sum[l - ][s ^ ]) * (n - r);
} inline LL Down(int l, int r, bool s)
{
if(l > r) return ;
return (distDown[r][s ^ ] - distDown[l - ][s ^ ]) -
(sum[r][s ^ ] - sum[l - ][s ^ ]) * (l - );
} inline LL Calc(int l, int r, bool state)
{
if(l == )
{
return Up(l, r, state);
}
int mid = (l + r) >> ;
return Up(mid + , r, state) + Down(l, mid, state);
} inline void Solve()
{
scanf("%d", &n);
for(int i = ; i <= n; i++)
for(int j = ; j < ; j++) scanf("%d", &arr[i][j]); for(int i = ; i < ; i++)
{
distDown[][i] = ;
for(int j = ; j <= n; j++)
{
distDown[j][i] = distDown[j - ][i] + 1LL * arr[j][i] * j;
sum[j][i] = sum[j - ][i] + arr[j][i];
}
distUp[n + ][i] = ;
for(int j = n; j >= ; j--)
distUp[j][i] = distUp[j + ][i] + 1LL * arr[j][i] * (n + - j);
} ans = MLL;
dp[][] = dp[][] = ;
for(int i = ; i < n; i++)
{
dp[i][] = dp[i][] = MLL;
for(int j = ; j < i; j++)
{
dp[i][] = min(dp[i][], dp[j][] + Calc(j + , i, ));
dp[i][] = min(dp[i][], dp[j][] + Calc(j + , i, ));
} ans = min(ans, dp[i][] + Down(i + , n, ));
ans = min(ans, dp[i][] + Down(i + , n, ));
}
cout << ans << endl;
} int main()
{
int test;
scanf("%d", &test);
for(int testnumber = ; testnumber <= test; testnumber++)
{
printf("Case #%d: ", testnumber);
Solve();
}
return ;
}