Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 130    Accepted Submission(s): 39


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.

 

这题是一道dp题,思路很难想,看了被人的博客后才做了出来,具体看代码中的解释。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x7fffffff
#define maxn 4050
ll a[maxn],b[maxn];
ll sum[maxn][2];//sum[i][u]表示前i层楼,性别为u的总人数
ll dsum[maxn][2];//dsum[i][u]表示前i层楼,性别为u者距离0层的距离之和
ll dp[maxn][2];//dp[i][u]表示第i层为u属性,第i+1层为另一属性,前i层不同性别到达自己的最近属性的寝室的最近距离和 ll goup(int l,int r,int sex){ //表示[l+1,r]区间sex性别要去r+1的总距离
return (sum[r][sex]-sum[l][sex])*(r+1)-(dsum[r][sex]-dsum[l][sex]);
} ll godown(int l,int r,int sex){ //表示[l+1,r]区间sex性别要去l的总距离
return dsum[r][sex]-dsum[l][sex]-(sum[r][sex]-sum[l][sex])*l;
} ll cnt(int l,int r,int sex){ //在[l,r]都是sex属性,且l-1与r+1都为非sex属性的条件下。 [l,r]这些楼层非sex属性的人,去自己属性寝室的最小距离。
int mid=(l+r)>>1;
return godown(l-1,mid,sex)+goup(mid,r,sex);
}
int main()
{
int n,m,i,j,T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum[0][0]=sum[0][1]=0;
dsum[0][0]=dsum[0][1]=0;
for(i=1;i<=n;i++){
scanf("%lld%lld",&a[i],&b[i]);
sum[i][0]=sum[i-1][0]+a[i];
sum[i][1]=sum[i-1][1]+b[i];
dsum[i][0]=dsum[i-1][0]+a[i]*i;
dsum[i][1]=dsum[i-1][1]+b[i]*i;
}
memset(dp,0,sizeof(dp));
ll ans=1e18;
for(i=1;i<n;i++){
dp[i][0]=goup(0,i,1); //这里先假设前i层都是性别0,i+1层是性别1所要的总距离
dp[i][1]=goup(0,i,0);
for(j=1;j<i;j++){
dp[i][0]=min(dp[i][0],dp[j][1]+cnt(j+1,i,1) ); //依次使得前j层是1,使得第j,i+1都是1,这样就好状态转移了
dp[i][1]=min(dp[i][1],dp[j][0]+cnt(j+1,i,0) );
}
ans=min(ans,dp[i][0]+godown(i,n,0) ); //这里每一层都要更新一下ans,而不能最后才更新,因为最后才更新的话就不能使得后面几层都相同了
ans=min(ans,dp[i][1]+godown(i,n,1) );
}
cas++;
printf("Case #%d: %lld\n",cas,ans);
}
return 0;
}

hdu5550 Game Rooms的更多相关文章

  1. [LeetCode] Meeting Rooms II 会议室之二

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  2. [LeetCode] Meeting Rooms 会议室

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  3. codeforces 519E A and B and Lecture Rooms LCA倍增

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  4. 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 ...

  5. [SmartFoxServer概述]Zones和Rooms结构

    Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...

  6. LeetCode Meeting Rooms II

    原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...

  7. LeetCode Meeting Rooms

    原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...

  8. socket.io问题,io.sockets.manager.rooms和io.sockets.clients('particular room')这两个函数怎么用?

    为什么我用nodejs用这个两个函数获取都会出错呢?是不是socket的api改了?请问现在获取房间数和当前房间的客户怎么获取?什么函数?谢谢!!急求!     网友采纳 版本问题.io.socket ...

  9. 253. Meeting Rooms II

    题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...

随机推荐

  1. 有关Servlet的面试题

    CGI(Common Gateway Interface),通用网管接口 通用网管接口,简称CGI,是一种根据请求信息动态产生回应内容的技术.通过CGI,web服务器可以根据请求的不同,启动不同的外部 ...

  2. mysql .sock丢时候如何链接数据库

    在mysql服务器本机上链接mysql数据库时,经常会噢出现mysql.sock不存在,导致无法链接的问题,这是因为如果指定localhost作为一个主机名,则mysqladmin默认使用unix套接 ...

  3. 怎么判断innodb 日志缓冲区该设置为多大呢

    怎么判断innodb 日志缓冲区该设置为多大呢

  4. webpack知识点整理

    作用域 es6里模块化的写法 会存在的问题,变量.方法名字雷同,外部文件调用的时候出现问题 如 a.js里 var a='susan' b.js里 var a='jack' 问题解决方案,添加包裹 a ...

  5. SwiftUI 官方画图实例详细解析

    前言 在前面几篇关于SwiftUI的文章中,我们用一个具体的基本项目Demo来学习了下SwiftUI,里面包含了常见的一些控件使用以及数据处理和地图等等,有兴趣的小伙伴可以去翻翻以前的文章,在前面总结 ...

  6. 在Jetbrain IDE中自定义TODO功能

    好的IDE能为开发以及学习源码带来效率的提升,今天要介绍的就是Jetbrain家族中IDE自带的TODO功能,我认为利用好它,能够大大的提升阅读源码的效率. 假设我现在需要去阅读源代码,看了半天我终于 ...

  7. 使用ogg实现oracle到postgresql表的实时同步

    参考:https://docs.oracle.com/goldengate/c1221/gg-winux/index.html https://blog.51cto.com/hbxztc/188071 ...

  8. pandas的级联操作

    级联操作 pd.concat, pd.append import pandas as pd from pandas import DataFrame import numpy as np pandas ...

  9. 部署自动初始化Schema的数据库

    我们使用容器的方式部署数据库组件,特别是企业有大量的项目开发业务的,部署的开发.测试数据库组件较多时.经常会遇到以下问题: 业务需要使用数据库,但部署完数据库后,需要在数据库中执行创建schema的操 ...

  10. C++ /Python 将视频中的片段转为图片

      配置OpenCV :项目名称->右击->属性 VC++目录 包含目录 放 ...\build\include ...\build\include\opencv   ...\build\ ...