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],...] ...
随机推荐
- Jmeter(三十五) - 从入门到精通进阶篇 - 关联(详解教程)
1.简介 上一篇中介绍了如果想要同时发送多条请求,那么怎样才能让每条数据某些请求参数改变呢.这就用到了jMeter参数化.在实际测试场景中,我们往往还有这样的需求,登录后服务器响应的token作为下次 ...
- Nacos使用和注册部分源码介绍
Nacos简单介绍 Nacos致力于帮助您发现.配置和管理微服务.Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置.服务元数据及流量管理.Nacos帮助您更敏捷和容易地构建. ...
- DataGridView控件使用Demo
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- postgresql数据库升级
pg_upgrade官网介绍:https://www.postgresql.org/docs/10/pgupgrade.html 1.查看老版本数据库编译参数值并记录 select name,sett ...
- BDC应用
第一步:SHDB或者是SM35进入BDC录制事务.开始录制. 第二部:保存录制的记录. 第三步:在你自己的程序中定义一个内表如:ITAB TYPE TABLE OF BDCDATA. 再定义一个工作空 ...
- STM32驱动LCD原理
TFTLCD即薄膜晶体管液晶显示器.它与无源TN-LCD.STN-LCD的简单矩阵不同,它在液晶显示屏的每一个像素上都设置有一个薄膜晶体管(TFT),可有效地克服非选通时的串扰,使显示液晶屏的静态特性 ...
- Sklearn 与 TensorFlow 机器学习实战—一个完整的机器学习项目
本章中,你会假装作为被一家地产公司刚刚雇佣的数据科学家,完整地学习一个案例项目.下面是主要步骤: 项目概述. 获取数据. 发现并可视化数据,发现规律. 为机器学习算法准备数据. 选择模型,进行训练. ...
- ichartjs插件的使用
项目中可能会用到饼状图.柱状图.环形图等,ichartjs是一个很不错的插件,体量小,只需引入ichart.1.2.1.min.js即可满足基础需求,github下载地址是:https://githu ...
- 使用cacti监控linux主机
介绍:使用cacti监控linux主机,需要在linux主机上面安装snmp服务,并修改snmpd.conf文件,指定cacti服务器的地址,然后在cacti的前台界面添加此主机即可,此处以监控cen ...
- JMETER-正则表达式提取与查看变量是否提取正确
一.应用场景说明: 在一个线程组中,B请求需要使用A请求返回的数据,也就是常说的关联,将上一个请求的响应结果作为下一个请求的参数,则需要对A请求的响应报文使用后置处理器,其中最方便最常用的就是正则表达 ...