hdu5550 Game Rooms
Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 130    Accepted Submission(s): 39
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.
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.
the test case number (starting from 1) and y is
the minimal sum of distances.
2
10 5
4 3
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的更多相关文章
- [LeetCode] Meeting Rooms II 会议室之二
		
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
 - [LeetCode] Meeting Rooms 会议室
		
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
 - codeforces 519E A and B and Lecture Rooms LCA倍增
		
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Prac ...
 - 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 ...
 - [SmartFoxServer概述]Zones和Rooms结构
		
Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...
 - LeetCode Meeting Rooms II
		
原题链接在这里:https://leetcode.com/problems/meeting-rooms-ii/ Given an array of meeting time intervals con ...
 - LeetCode Meeting Rooms
		
原题链接在这里:https://leetcode.com/problems/meeting-rooms/ Given an array of meeting time intervals consis ...
 - socket.io问题,io.sockets.manager.rooms和io.sockets.clients('particular room')这两个函数怎么用?
		
为什么我用nodejs用这个两个函数获取都会出错呢?是不是socket的api改了?请问现在获取房间数和当前房间的客户怎么获取?什么函数?谢谢!!急求! 网友采纳 版本问题.io.socket ...
 - 253. Meeting Rooms II
		
题目: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] ...
 
随机推荐
- 谈谈你不知道的gist
			
1.Gist是什么关于Gist的详细介绍,请阅读官方文档About gists,下面只简略介绍部分功能: Gist是一种与其他人共享代码片段和粘贴的简单方法. 当您需要与同事或朋友共享示例代码或技术时 ...
 - Linux学习笔记 | docker基本命令
			
Docker的三大核心概念:镜像.容器.仓库 镜像:类似虚拟机的镜像.用俗话说就是安装文件. 容器:类似一个轻量级的沙箱,容器是从镜像创建应用运行实例,可以将其启动.开始.停止.删除.而这些容器都是相 ...
 - 修改hosts文件后不生效,该怎么办
			
对于web开发来说,经常需要修改hosts文件,用来将域名与ip对应匹配.但是有时候发现hosts文件明明已经改了,但就是不生效,页面还会跳到某个丧心病狂的私人小站.hosts文件不生效有很多种原因, ...
 - 【Java】单例模式(Singleton)
			
重新搞一波 复习巩固 简单记录 慕课网 Java工程师 文章目录 单例概述 设计模式 单例模式(Singleton) 参考资料 单例概述 Singleton Pattern 单例模式是Java中最简单 ...
 - 【ORA】ORA-19602: cannot backup or copy active file in NOARCHIVELOG mode
			
ORA-19602: cannot backup or copy active file in NOARCHIVELOG mode 这个问题是rman备份的时候,发现有问题 原因: 数据库没有开启归档 ...
 - Jmeter(三十六) - 从入门到精通进阶篇 - 设置负载阶梯式压测场景(详解教程)
			
1.简介 在性能测试中,有时需要模拟一种实际生产中经常出现的情况,即:从某个值开始不断增加压力,直至达到某个值,然后持续运行一段时间,然后继续加压达到某个值持续运行,如此循环直到达到预期的峰值,运行一 ...
 - 《Go 语言并发之道》读后感 - 第四章
			
<Go 语言并发之道>读后感-第四章 约束 约束可以减轻开发者的认知负担以便写出有更小临界区的并发代码.确保某一信息再并发过程中仅能被其中之一的进程进行访问.程序中通常存在两种可能的约束: ...
 - Sentry(v20.12.1) K8S 云原生架构探索,JavaScript Enriching Events(丰富事件信息)
			
系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...
 - linux串口编程
			
按照对linux系统的理解,串口编程的顺序无非就是open,read,write,close,而串口有波特率.数据位等重要参数需要设置,因此还应该用到设置函数,那么接下来就带着这几个问题去学习linu ...
 - 糊糊的学习笔记--Fiddle抓包
			
Fiddle简述 Fiddler是一个http调试代理,它能 够记录所有的你电脑和互联网之间的http通讯,Fiddler 可以也可以让你检查所有的http通讯,设置断点,以及Fiddle 所有的&q ...