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],...] ...
随机推荐
- Thread线程源码解析,Java线程的状态,线程之间的通信
线程的基本概念 什么是线程 现代操作系统在运行一个程序的时候,会为其创建一个进程.例如,启动一个Java程序,操作系统就会创建一个Java进程.线代操作系统调度的最小单位是线程.也叫做轻量级进程.在一 ...
- .NET 5 程序高级调试-WinDbg
上周和大家分享了.NET 5开源工作流框架elsa,程序跑起来后,想看一下后台线程的执行情况.抓了个进程Dump后,使用WinDbg调试,加载SOS调试器扩展,结果无法正常使用了: 0:000> ...
- mysql过滤复制
- IE浏览器直接在页面中显示7z文件而不是下载问题解决
IE浏览器中输入7z文件的完整下载URL后,不是保存文件,而是直接在页面中显示(当然是乱码) 这是因为浏览器对不同的资源文件处理的方式不同,例如图片文件,一般会直接打开,所以我们可以不用7z,使用zi ...
- IDEA安装codota插件和使用,开发人员的知心伙伴
打开IDEA 点击左上角的File之后,如下图 成功后如图所示
- 二十七:XSS跨站之代码及httponly绕过
httponly:如果给某个 cookie 设置了 httpOnly 属性,则无法通过 JS 脚本 读取到该 cookie 的信息,但还Application 中手动修改 cookie,所以只是在一定 ...
- JAVA中@Override的含义
@Override是伪代码,表示重写(当然不写也可以),不过写上有如下好处: 1.可以当注释用,方便阅读: 2.编译器可以给你验证@Override下面的方法名是否是你父类中所有的,如果没有则报错.例 ...
- LSM(Log Structured Merge Trees ) 笔记
目录 一.大幅度制约存储介质吞吐量的原因 二.传统数据库的实现机制 三.LSM Tree的历史由来 四.提高写吞吐量的思路 4.1 一种方式是数据来后,直接顺序落盘 4.2 另一种方式,是保证落盘的数 ...
- 面试常问的ArrayQueue底层实现
public class ArrayQueue<T> extends AbstractList<T>{ //定义必要的属性,容量.数组.头指针.尾指针 private int ...
- SQL Server和Oracle数据类型对应关系
在工作中,有时会遇到跨库传输数据的情况,其中 SQL Server 和 Oracle 之间的数据传输是比较常见的情况. 因为 SQL Server 和 Oracle 的数据类型有些差异,这就要求我们在 ...