zoukankan      html  css  js  c++  java
  • CDOJ 1225 Game Rooms

    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

    The 2015 China Collegiate Programming Contest
     
    解题:动态规划大法真是无敌
    dp[i][0]表示第i层放0,第i+1层放1,所以前面放0的层数那些玩1的人要跑到i+1去玩1了,这就是dp的初始化
    那么转移 dp[i][0] 可以由 dp[j][1]转移到来,因为j层放1,i+1层放1,故j+1到i层玩1的人有两个去处,均摊给j和i+1两个楼层较优
    这就是转移
    那么最后由于dp[i][0],i层放的是0并且i+1层放的是1,所以i+1层的0要到i来玩0,取最小的
     
     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 }
    View Code
  • 相关阅读:
    HDU 3709 数位dp
    Educational Codeforces Round 64 (Rated for Div. 2)-C. Match Points
    POJ 1845乘法逆元+约数和
    POJ3696 欧拉定理
    NC24953 树形dp(最小支配集)
    Codeforces 1173 C 思维+模拟
    Codeforces 1324F 树形dp+换根
    codeforces-1285D(字典树)
    面向纯小白的CLion(C++)基于Windows的安装配置教程
    HDU-2825Wireless Password(AC自动机+状压DP)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4934228.html
Copyright © 2011-2022 走看看