HDU1529 Cashier Employment 题解 【差分约束】【二分答案】
2017-07-10 17:09
405 查看
【题目描述】
Problem 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.
大意:
德黑兰的某国营商店需要聘用一批员工,不同的时间有不同的人员需求,能够雇佣到得人数也不同。然而你一旦聘用了一个人,他就会跟着你干8个小时。输入需求与劳动力人数,输出满足需求最少需要的人数,不行就输出no solution。
【题解】
我们先申明三个数组:
sum[i]:表示第i时雇佣人得最小值
num[i]:i时可雇用得人数
r[i]:i时需要雇佣的人数
备注:i的范围是[1,24]而非[0,23],输入的时候要动脑筋
随后我们可以推出的不等式们:
之后我们以sum数组为中心建图,然而用于最后一个不等式里头有一个sum[24],这个东西是一个变量,三个变量是怎么都不能用差分约束解得。由此我们在外边二分一个sum[24],之后在check()里面搞差分约束就行了。
我们来看代码:
Problem 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.
大意:
德黑兰的某国营商店需要聘用一批员工,不同的时间有不同的人员需求,能够雇佣到得人数也不同。然而你一旦聘用了一个人,他就会跟着你干8个小时。输入需求与劳动力人数,输出满足需求最少需要的人数,不行就输出no solution。
【题解】
我们先申明三个数组:
sum[i]:表示第i时雇佣人得最小值
num[i]:i时可雇用得人数
r[i]:i时需要雇佣的人数
备注:i的范围是[1,24]而非[0,23],输入的时候要动脑筋
随后我们可以推出的不等式们:
sum[i]-sum[i-1]>=0;//i时雇佣的人数大于等于0 sum[i]-sum[i-1]<=num[i]==>sum[i-1]-sum[i]>=num[i];//i时雇佣的人数不能超出市场能力 sum[i]-sum[i-8]>=r[i];(r>=8&&r<=24)//i时雇佣的人数不能小于需求 sum[24]+sum[i]-sum[i+16]>=r[i];(r<=7) sum[24]-sum[0]=0==>sum[24]-sum[0]>=0,sum[0]-sum[24]<=0;
之后我们以sum数组为中心建图,然而用于最后一个不等式里头有一个sum[24],这个东西是一个变量,三个变量是怎么都不能用差分约束解得。由此我们在外边二分一个sum[24],之后在check()里面搞差分约束就行了。
我们来看代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int N=10000; const int M=30; const int Q=1000; const int INF=1e9; struct edge { int u,v,w; int next; edge(){next=-1;} }ed[2*N+5]; int head[M+5],tot; int dis[M+5],flag[M+5],mark[N+5]; queue<int> q; int n,t; int r[M+5],num[Q+5];//num[i]表示市场上i时可用的劳动力,r[i]表示第i时的需求,i属于[1,24] void build(int u,int v,int w) { ed[++tot].v=v; ed[tot].w=w; ed[tot].next=head[u]; head[u]=tot; } bool SPFA(int s,int e) { while(!q.empty())q.pop(); for(int i=0;i<=e;i++) dis[i]=-INF;//最长路 memset(flag,0,sizeof(flag)); memset(mark,0,sizeof(mark)); dis[s]=0,mark[s]=1,flag[s]=1; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); flag[u]=0; for(int i=head[u];i!=-1;i=ed[i].next) { int v=ed[i].v; if(dis[v]<dis[u]+ed[i].w)//最长路 { dis[v]=dis[u]+ed[i].w; if(!flag[v]) { flag[v]=1; if(++mark[v]>e)return false;//负环,显然不行 q.push(v); } } } } return true; } bool check(int x) { memset(head,-1,sizeof(head)); tot=0; for(int i=1;i<=24;i++) { build(i-1,i,0); build(i,i-1,-num[i]); if(i>=8)build(i-8,i,r[i]); else build(i+16,i,r[i]-x); } build(0,24,x); build(24,0,-x); return SPFA(0,24); } int main() { scanf("%d",&t); while(t--) { for(int i=1;i<=24;i++) scanf("%d",&r[i]); scanf("%d",&n); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); num[x+1]++;//i属于[1,24] } int lf=0,rg=n,ans=-1; while(rg>=lf) { int mid=(lf+rg)>>1; if(check(mid)){ans=mid;rg=mid-1;} else lf=mid+1; } if(ans==-1)printf("No Solution\n"); else printf("%d\n",ans); } return 0; }
相关文章推荐
- [NOIP2012提高]借教室 题解(二分答案+差分)
- Luogu4926 倍杀测量者(二分答案+差分约束)
- HDU1529:Cashier Employment(差分约束 & 二分)
- poj 1275 Cashier Employment - 差分约束 - 二分答案
- [UVA]11478 二分答案+差分约束
- UVA 11478 Halum(二分 + 差分约束)
- UVA 11478(差分约束 + 二分)
- POJ 1275 Cashier Employment (差分约束 二分)
- NOIP 2015 Day 2 transport 运输计划 (树链剖分 序列差分 二分答案)
- uva11478 Halum【二分+差分约束】
- 图论小结(一)包括一些最短路,最小生成树,差分约束,欧拉回路,的经典题和变种题。强连通,双连通,割点割桥的应用。二分匹配,KM,支配集,独立集,还有2-SAT。
- Luogu P1083 借教室【二分答案/差分】By cellur925
- NOIP2015 运输计划 二分答案+Tarjan LCA+树上差分
- vijos 运输计划 - 二分答案 - 差分 - Tarjan
- BZOJ2330[SCOI2011] 糖果 题解【差分约束】
- bzoj4326【2015提高】运输计划(二分答案+lca+树上差分前缀和)
- LuoguP2680/UOJ150[NOIP2015] 运输计划 解题报告【二分答案+树上操作(LCA)+树上差分】
- [NOIP提高&洛谷P1024]一元三次方程求解 题解(二分答案)
- Layout POJ - 3169(差分约束) 题解
- Noip2012 Day2 T2 借教室 (二分答案+差分)