http://acm.hdu.edu.cn/showproblem.php?pid=1529
Cashier Employment
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1489 Accepted Submission(s): 672
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.
If there is no solution for the test case, you should write No Solution for that case.
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
题目大意:一个超市,24小时营业,不同时间点所需的售货员数目不同,给出24小时各个时间的所需人数以及每个售货员的上班时间安排,每个售货员的工作时间是8小时,求最少需要多少售货员。
题目分析:由题目易知是道差分约束问题。则需要列出隐藏的不等式。
令S【I】表示【0,I】时间段以及工作过或者正在工作的人数【题目所求也就是S【24】】
令num【I】表示I时刻刚好开始工作的人数【也就是将题目中所说的每个售货员上班开始时间转化为了某一时刻刚好上班的人数】
令R【I】表示时刻I所需人数
则 S【I+1】-S【I】>= 0&&S【I+1】-S【I】<= num【I】【条件不等式一--->由每一时刻加入工作人数为num【I】来确定】
同时按照要求有 当 I >= 8 时 S【I】-S【I-8】>=R【I】
当 I < 8 时 S【24】- S【I+16】+ S【I】> R【I】 【条件不等式二、三---->由每个时刻所需人数R【I】确定】
而最容易被遗忘的不等式是 S【24】-S【0】== 枚举值 mid,这是一个等式,化成不等式形式是 S【24】-S【0】>= 0 &&S【24】-S【0】<= 0 【不等式四、五-->由等式转化】
【PS:记得初始化多个数组,不然WA到怀疑人生..】
二分S【24】的值,二分判断的条件就是连边之后能够得到最长路【即不存在正环,这一点可以通过SPFA来判断】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=;
struct edge{
int to;
int len;
int next;
}EDGE[];
queue<int>pq;
int edge_cnt=,dist[],stk[],head[],n,in[],r[],num[];
void add(int x,int y,int z)
{
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
EDGE[edge_cnt].len=z;
head[x]=edge_cnt++;
}
bool spfa()
{
while(!pq.empty())
{
pq.pop();
}
memset(dist,-,sizeof(dist));
memset(stk,,sizeof(stk));
memset(in,,sizeof(in));
dist[]=;
pq.push();
in[]=;
while(!pq.empty())
{
int qwq=pq.front();pq.pop();
// cout << qwq << endl;
in[qwq]++;
if(in[qwq]>){
return false;
}
stk[qwq]=;
for(int i = head[qwq] ; i != - ; i = EDGE[i].next)
{
int v=EDGE[i].to;
// cout << dist[v]<<v<<endl;
if(dist[v]<dist[qwq]+EDGE[i].len)
{
dist[v]=dist[qwq]+EDGE[i].len;
if(!stk[v]){
stk[v]=;
pq.push(v);
}
}
}
}
return true;
}
bool check(int x)
{
memset(head,-,sizeof(head));
for(int i = ; i <= ; i++)
{
add(i-,i,);
add(i,i-,-num[i]);
if(i>=)
add(i-,i,r[i]);
else
add(i+,i,r[i]-x);
}
add(,,-x);
add(,,x);
return spfa();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i = ; i <= ; i++)
{
scanf("%d",&r[i]);
}
scanf("%d",&n);
memset(num,,sizeof(num));
for(int i = ; i < n ; i++)
{
int x;
scanf("%d",&x);
num[x+]++;
}
int l=;int r=n;
while(l<=r)
{
int mid=(l+r)/;
if(check(mid))
{
r=mid-;
}
else
{
l=mid+;
}
}
if(l>n)printf("No Solution\n");
else printf("%d\n",l);
}
return ;
}