POJ1275 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
题意:
在一家超市里,每个时刻都需要有营业员看管,R(i) (0 <= i < 24)表示从i时刻开始到i+1时刻结束需要的营业员的数目,现在有N(N <= 1000)个申请人申请这项工作,并且每个申请者都有一个起始工作时间 ti,如果第i个申请者被录用,那么他会从ti时刻开始连续工作8小时。现在要求选择一些申请者进行录用,使得任何一个时刻i,营业员数目都能大于等于R(i)。求出至少需要录用多少营业员。
我们用S[i]表示一天内前i+1个小时录用的人员,
当i>=7时,我们需要满足s[i]-s[i-8]>=R[i]
当0<=i<7,经过推倒不难发现,我们需要满足s[i]+s[23]-s[i+16]>=R[i]
同时,因为题目中人数的限制,我们还需要满足0<=s[i]-s[i-1]<=b[i]
这样这道题就看起来可做了
但是我们的第二个式子左边有三项,不过还好s[23]是个常数项,我们可以二分解决
因为是求最小,所以我们按照套路连边,求最长路
#include<cstdio>
#include<queue>
#include<cstring>
#define INF 1e8+10
using namespace std;
const int MAXN=1e5+10;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
char buf[MAXN],*p1=buf,*p2=buf;
inline int read()
{
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int R[MAXN],N,pep[MAXN],dis[MAXN],vis[MAXN];
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN],num=1;
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;
edge[num].nxt=head[x];
head[x]=num++;
}
void PRE()
{
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
num=1;
}
int SPFA(int val)
{
memset(dis,-0x7f,sizeof(dis));
queue<int>q;
dis[0]=0;
q.push(0);
while(q.size()!=0)
{
int p=q.front();q.pop();
vis[p]=0;
if(p==24&&dis[p]>val) return 0;
for(int i=head[p];i!=-1;i=edge[i].nxt)
{
if(dis[edge[i].v]<dis[p]+edge[i].w)
{
dis[edge[i].v]=dis[p]+edge[i].w;
if(!vis[edge[i].v]) vis[edge[i].v]=1,q.push(edge[i].v);
}
}
}
return dis[24]<=val?1:0;
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int QWQ=read();
while(QWQ--)
{
memset(pep,0,sizeof(pep));
for(int i=0;i<=23;i++) R[i]=read();
N=read();
for(int i=1;i<=N;i++)
pep[read()]++;
int r=N+1,l=0;
int ans=INF;
while(l<=r)
{
PRE();
int mid=l+r>>1;
for(int i=0;i<=23;i++) AddEdge(i,i+1,0),AddEdge(i+1,i,-pep[i]);
for(int i=7;i<=23;i++) AddEdge(i-7,i+1,R[i]);
AddEdge(0,24,mid);AddEdge(24,0,-mid);
for(int i=0;i<7;i++) AddEdge(i+17,i+1,R[i]-mid);
if(SPFA(mid)) ans=mid,r=mid-1;
else l=mid+1;
}
if(ans>N) printf("No Solutionn");
else printf("%dn",ans);
}
return 0;
}
- 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 数组属性和方法
- Java并发编程的艺术[2]
- Spark UDF实现demo
- Spring boot 开发中热部署配置(只需两步)
- 纯Java搭建SpringMVC项目
- 整理一下Spring整合Junit4单元测试方法
- spring 到底注入接口还是实现类?
- 韩松团队新作 | MCUNet | IoT设备+微型机器学习时代已经到来了
- SpringMVC在web.xml中的配置(引入springmvc)
- springmvc使用DELETE|PUT请求
- Mybatis缓存相关的知识
- 「2020最新」Spring最易学习教程—第一个Spring程序
- idea背景图片及简化插件
- 「2020最新」Spring最易学习教程—IOC 以及 整合Struts2
- SpringBoot整合Mybatis增删改查
- JavaWeb之Maven