POJ1275 Cashier Employment
时间:2019-06-18
本文章向大家介绍POJ1275 Cashier Employment,主要包括POJ1275 Cashier Employment使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Cashier Employment
Language: Cashier Employment
Description A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its need. The supermarket manager has hired you to help him, solve his problem. The problem is that the supermarket needs different number of cashiers at different times of each day (for example, a few cashiers after midnight, and many in the afternoon) to provide good service to its customers, and he wants to hire the least number of cashiers for this job.
The manager has provided you with the least number of cashiers needed for every one-hour slot of the day. This data is given as R(0), R(1), ..., R(23): R(0) represents the least number of cashiers needed from midnight to 1:00 A.M., R(1) shows this number for duration of 1:00 A.M. to 2:00 A.M., and so on. Note that these numbers are the same every day. There are N qualified applicants for this job. Each applicant i works non-stop once each 24 hours in a shift of exactly 8 hours starting from a specified hour, say ti (0 <= ti <= 23), exactly from the start of the hour mentioned. That is, if the ith applicant is hired, he/she will work starting from ti o'clock sharp for 8 hours. Cashiers do not replace one another and work exactly as scheduled, and there are enough cash registers and counters for those who are hired. You are to write a program to read the R(i) 's for i=0..23 and ti 's for i=1..N that are all, non-negative integer numbers and compute the least number of cashiers needed to be employed to meet the mentioned constraints. Note that there can be more cashiers than the least number needed for a specific slot. Input The first line of input is the number of test cases for this problem (at most 20). Each test case starts with 24 integer numbers representing the R(0), R(1), ..., R(23) in one line (R(i) can be at most 1000). Then there is N, number of applicants in another line (0 <= N <= 1000), after which come N lines each containing one ti (0 <= ti <= 23). There are no blank lines between test cases. Output For each test case, the output should be written in one line, which is the least number of cashiers needed.
If there is no solution for the test case, you should write No Solution for that case. Sample Input 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 5 0 23 22 1 10 Sample Output 1 Source |
给出24个数,第i个数表示在(i,i+1)小时内需要的人数,之后m个数,代表m个人前来应聘,其中每个人工作连续的8小时,给出应聘的人开始工作的时间,问最少需要雇佣的人数(可以在某个时间段中人数多于需要的人数)
题解
参照刀刀狗0102的题解。
问题分析:因为二十四小时是循环的,所以这是一个环,这题不能只用前缀和表示关系。解决办法是枚举(二分)答案,然后连特殊边(边权根据枚举的答案变化)解决问题。
差分约束:
- 题目需要求什么,就找什么之间的关系(二项式),比如,题目求雇佣的人数,就找出雇佣人数之间的关系,s[i]代表从0到i一共雇佣的人数
- 注意0的问题,和总和的问题。
- 判断的问题,不能存在环,和不能违背要求的值
那么归结关系有
s[i]:1到i时刻雇佣的总人数。
c[i]:i时刻;来应聘的人数。
r[i]:i时刻需要的人数。
设需要的人数为sum,即枚举的答案。
- 实际问题
0 <= s[i] - s[i-1] <= h[i] ; - 总数
s[24] - s[0] >= sum - 分类讨论约束
s[i] - s[i-8] >= r[i] (i >= 8)
sum - s[i+16] + s[i] >= r[i] (i < 8)
对上面的条件整理,然后求最短路,如果没有负环那么就有解。
#include<iostream>
#include<cstring>
#include<queue>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=1e3+1;
int n,r[N],c[N],d[N],s[N];
int head[N],edge[N],next[N],leng[N],tot;
bool v[N];
il void add(int x,int y,int z){
edge[++tot]=y,leng[tot]=z,next[tot]=head[x],head[x]=tot;
}
int spfa(int now){
for(int i=1;i<=7;++i) leng[head[i]]=now-r[i];
leng[head[24]]=-now;
memset(d,0x3f,sizeof d);
memset(s,0,sizeof s);
queue<int> q;
for(int i=0;i<=24;++i) q.push(i),d[i]=0,v[i]=1;
while(q.size()){
int x=q.front();q.pop();
v[x]=0;
for(int i=head[x];i;i=next[i]){
int y=edge[i],z=leng[i];
if(d[y]>d[x]+z){
d[y]=d[x]+z,s[y]=s[x]+1;
if(s[y]>25) return 0;
if(!v[y]) q.push(y),v[y]=1;
}
}
}
return 1;
}
void Cashier_Employment(){
for(int i=1;i<=24;++i) read(r[i]);
read(n),memset(c,0,sizeof c);
for(int i=1;i<=n;++i) ++c[read<int>()+1];
tot=0,memset(head,0,sizeof head);
for(int i=1;i<=24;++i){
add(i-1,i,c[i]);
add(i,i-1,0);
}
for(int i=8;i<=24;++i) add(i,i-8,-r[i]);
for(int i=1;i<=7;++i) add(i,i+16,0); // changeable length
add(24,0,0);
for(int i=1;i<=n;++i)
if(spfa(i)) return printf("%d\n",i),void();
puts("No Solution");
}
int main(){
for(int t=read<int>();t--;) Cashier_Employment();
return 0;
}
原文地址:https://www.cnblogs.com/autoint/p/11045256.html
- 55. 上传文件(Web版) | 厚土Go学习笔记
- ASM无法启动的问题分析(一)(r7笔记第87天)
- 54. 心跳的实现 | 厚土Go学习笔记
- 53. Socket服务三次握手的示例 | 厚土Go学习笔记
- 使用Beego+Swagger构建更好的API服务
- ASM无法启动的问题分析(二)(r7笔记第88天)
- 43. 等价二叉树 | 厚土Go学习笔记
- 51. Socket服务端和客户端使用TCP协议通讯 | 厚土Go学习笔记
- 50. RESTful API的简单实现 | 厚土Go学习笔记
- go实现西瓜视频花椒直播等平台智能答题
- 主备切换的准备工作(二) (r7笔记第85天)
- 49. 访问PostgreSQL数据库增删改查 | 厚土Go学习笔记
- Golang中Interface类型详解
- Go语言的网络编程简介
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 基于TypeScript封装Axios笔记(二)
- redis高性能数据结构之有序集
- spring之通过静态工厂方法配置Bean
- 【python-leetcode107-树的宽度遍历】二叉树的层次遍历Ⅱ
- spring之通过注解方式配置Bean(一)
- React17新特性:启发式更新算法
- spring之通过FactoryBean配置Bean
- 谱聚类的python实现
- 基于TypeScript封装Axios笔记(三)
- hadoop伪分布式之启动HDFS并运行MR程序(WordCount)
- 细品Reids的HyperLogLog数据结构
- 布隆过滤器与缓存击穿
- spring之通过注解方式配置Bean(二)
- 【python-leetcode103-树的宽度遍历】二叉树的锯齿形层次遍历
- 图卷积网络(GCN)python实现