题干:
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
题目大意:
一句话题意:
在一家超市里,每个时刻都需要有营业员看管,R(i) (0 <= i < 24)表示从i时刻开始到i+1时刻结束需要的营业员的数目,现在有N(N <= 1000)个申请人申请这项工作,并且每个申请者都有一个起始工作时间 ti,如果第i个申请者被录用,那么他会从ti时刻开始连续工作8小时。现在要求选择一些申请者进行录用,使得任何一个时刻i,营业员数目都能大于等于R(i)。求出至少需要录用多少营业员。
解题报告:
考虑差分约束。
设 代表第(i-1)时这个时刻来应聘工作的人数,为这个时间段至少需要的人数, 为在第(i-1)时这个时刻招到的人数。
根据题意需要满足:
0 <= has[i] <= num[i]
has[i] + has[i-1] + …+ has[i-7] >= r[i] (当然遇到i-7小于0的要处理一下)(题目中的连续工作8小时)
但是考虑到不方便建图,重新定义关系:s[i] = has[1] + has[2] + … + has[i]
然后创建出满足题目关系的图:
s[i] - s[i-1] >= 0
s[i] - s[i-1] <= num[i]
s[i] - s[i-8] >= r[i] (其中8 <= i <= 24)
s[i] + s[24] - s[i+16] >= r[i] (其中1 <= i <= 7)
然后发现最后一个式子有三个参数了,但是还好s[24]是个常量,虽然不是常量(因为也属于约束系统的)但是最起码可以是个常量(大概因为他不带 i ?)所以我们可以钦点这个常数的大小。然后看能否构造出满足条件的解就可以了。
当然指定s[24]了之后还要加上s[24] = ans,又因为指定了s[0]=0,所以可以构造s[24]-s[0]=ans。然后题目是求最小值,即求最长路,以0为源点就可以了。又因为这题显然是符合单调性的,所以对这个ans可以二分。
注意一个地方,那就是不能直接不加 s[24] = ans 这个条件,然后直接检测spfa之后的dis[24]<=ans,如果成立则移动r,不成立则移动l。这样是不对的。
因为这样相当于你先假设了s[24]来约束了s[1]~s[7],然后构造出了一个有解的系统,然后检查dis[24],这时候如果dis[24]<ans,则说明说明你在假设了一个综合的前提下得到了更小的解,但是这个解是在假设s[24]的前提下的,你此时因为ans就是s[24],你此时s[24]>dis[24]说明,你在求解的过程中扩大了求解范围(也就是削弱了约束),因为你输入的条件是ans的,结果你现在得到的是小于ans的数,所以你把当前dis[24]带入的虽然是你输入的约束系统的合法解,但未必是符合题意的合法解。也就是dis[24]<ans成立,但是未必dis[24]人为放大到=ans也是成立的,但你输入系统的时候要求dis[24]是ans,所以这样是不可行的。
那有同学就会说,那spfa完后检查dis[24]==ans不就好了?这样也有个问题:就是dis[24]代表的是24号点取最小的时候构成的解,因为你差分约束系统只能求出一组解来不能求出全部解,也不能求出所有最优解,只能求出对于某个点来说的最优解,而此时构造出的s数组对于其他点来说未必是最优解。所以你dis[24]==24成立则肯定没问题,但是dis[24]!=24也不代表就一定不成立。以为可能通过dis[24]变小一丢丢,就可能存在了合法解。我也不知道我上面在说什么,反正大概就是,假设你枚举到一个正确答案ans,输入到差分约束系统,但是你spfa出来可能dis[24]<ans,因为你ans输入了一个比较大的范围,我在这里面可能dis[24]可以取到一个比较小的值,但是dis[24]真取到这个小的值的时候虽然是约束系统的合法解,但是不是题意的合法解(其实我是怎么发现这一点的呢?就是你按照正确的方式建图,然后找到答案了之后,对这个答案用错误的建图方式但是用dis[24]<=ans去检验,也可以AC)。 emmm怎么说呢就是可以理解成:虽然分开看都可以成立,但是不代表他俩可以同时成立。所以解决办法就是加上一个 s[24]=ans 这个约束,然后直接看有无解就可以了(这里就是有无正环)
总的来说:如果<ans来判断会使得求得的答案<正确答案(即求出了看似正确的解但是不是正确的解)
如果用=ans来判断,会使得求得的答案>正确答案(即本身可以是正确的解,但是通过你的判断方法得出这个解是不成立的)
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {int ne,v,w;
} e[MAX];
int tot,n;
int head[MAX];
void add(int u,int v,int w) {e[++tot].v = v;e[tot].w = w;e[tot].ne = head[u];head[u] = tot;
}
int dis[MAX],cnt[MAX],vis[MAX];int spfa(int st) {for(int i = 0; i<=1231; i++) dis[i] = -INF; memset(vis,0,sizeof vis);memset(cnt,0,sizeof cnt);queue<int> q;dis[st]=0;q.push(st);vis[st]=1;cnt[st]=1;while(q.size()) {int cur = q.front(); q.pop();vis[cur]=0;for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].v;if(dis[v] >= dis[cur] + e[i].w) continue;dis[v] = dis[cur] + e[i].w;if(!vis[v]) {cnt[v]++;vis[v]=1;q.push(v);if(cnt[v] > n) return 99999999;}}}return dis[24];
}
int R[26],num[26];
int main()
{int t;cin>>t;while(t--) {tot=0;memset(head,-1,sizeof head);for(int i = 1; i<=24; i++) scanf("%d",R+i),num[i]=0;scanf("%d",&n);for(int x,i = 1; i<=n; i++) scanf("%d",&x),num[x+1]++;int l = 0,r = n,mid,ans=-1;while(l<=r) {mid=(l+r)>>1;tot=0;memset(head,-1,sizeof head);for(int i = 1; i<=24; i++) add(i-1,i,0),add(i,i-1,-num[i]);for(int i = 9; i<=24; i++) add(i-8,i,R[i]);for(int i = 1; i<=8; i++) add(i+16,i,R[i] - mid);add(0,24,mid); add(24,0,-mid);if(spfa(0) == mid) ans = mid,r = mid - 1;else l = mid + 1; }if(ans == -1) printf("No Solution\n");else printf("%d\n",ans);}return 0 ;
}