POJ1275 Cashier Employment(差分约束)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9078 | Accepted: 3515 |
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.
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.
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 Solution\n"); else printf("%d\n",ans); } return 0; }
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。