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. wpf 在不同DPI下如何在DrawingVisual中画出清晰的图形

    环境Win10 VS2017 .Net Framework4.7.1   本文仅讨论在DrawingVisual中进行的画图.   WPF单位,系统DPI,显示器DPI三者的定义及关系 WPF单位:一 ...

  2. 【MySQL】CentOS7中使用systemctl工具管理启动和停止MySQL

    centos7以前版本,可以使用这个/etc/init.d/mysqld start 来启动mysql 但是centos7之后,通过systemctl start mysqld.service 这个要 ...

  3. 【EXPDP】Oracle expdp中并行问题

    $ expdp hr/hr tables=test1 dumpfile=test2.dmp directory=pump parallel=4 Export: Release 11.2.0.4.0 - ...

  4. 【ASM】介绍Oracle自带的一些ASM维护工具 (kfod/kfed/amdu)

    转自:http://blog.csdn.net/wenzhongyan/article/details/47043253 非常感谢作者的文章,很有价值!至此转载,非常感谢 1.前言 ASM(Autom ...

  5. ORACLE 归档日志打开关闭方法(转载)

    一 设置为归档方式 1 sql> archive log list; #查看是不是归档方式 2 sql> alter system set log_archive_start=true s ...

  6. Android事件分发机制四:学了事件分发有什么用?

    " 学了事件分发,影响我CV大法吗?" " 影响我陪女朋友的时间" " ..... " 前言 Android事件分发机制已经来到第四篇了,在 ...

  7. Redis 实战 —— 08. 实现自动补全、分布式锁和计数信号量

    自动补全 P109 自动补全在日常业务中随处可见,应该算一种最常见最通用的功能.实际业务场景肯定要包括包含子串的情况,其实这在一定程度上转换成了搜索功能,即包含某个子串的串,且优先展示前缀匹配的串.如 ...

  8. .net core 和 WPF 开发升讯威在线客服与营销系统:使用 TCP协议 实现稳定的客服端

    本系列文章详细介绍使用 .net core 和 WPF 开发 升讯威在线客服与营销系统 的过程.本产品已经成熟稳定并投入商用. 在线演示环境:https://kf.shengxunwei.com 注意 ...

  9. 【Soul网关探秘】http数据同步-Admin通知前处理

    引言 本篇开始研究 Soul 网关 http 数据同步,将分为三篇进行分析: <Admin通知前处理> <变更通知机制> <Bootstrap处理变更通知> 希望三 ...

  10. Server Tracking of Client Session State Changes Connection Management

    MySQL :: MySQL 8.0 Reference Manual :: 5.1.12 Connection Management https://dev.mysql.com/doc/refman ...