# poj 1275 Cashier Employment - 差分约束 - 二分答案

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

一个超市在第$i$小时中工作的员工数目不能少于$req[i]$个。有$n$个应聘的人，第$i$个人愿意从$t_{i}$开始工作8小时，问最少需要聘请多少人才能使得达到要求。

设$x_{i}$表示第$i$个小时中开始工作的员工数目。为了表示八个小时内的员工数目，定义$s_{i} = x_{0} + \cdots + x_{i - 1}$。用$own[i]$表示愿意从时刻$i$开始工作的人数

于是便有如下一些不等式：

• $0 \leqslant s_{i} - s_{i - 1} \leqslant own[i - 1]$
• $\left\{\begin{matrix}s_{i} - s_{i - 8}\geqslant req[i - 1]\ \ \ \ \ \ \ \ \ \ \left ( i \geqslant 8 \right ) \\ s_{16 + i} - s_{i}\leqslant s_{24} - req[i - 1]\ \left ( i \leqslant 8 \right )\end{matrix}\right.$

但是发现第二个不等式组中的第二个不等式含有3个未知量，即$s_{24}$，但是总共就只有这么一个，可以考虑枚举它。

显然答案满足二分性质，所以二分它，增加限制$s_{24} = mid$。

### Code

 1 /**
2  * poj
3  * Problem#1275
4  * Accepted
5  * Time: 16ms
6  * Memory: 672k
7  */
8 #include <iostream>
9 #include <cstring>
10 #include <cstdio>
11 #include <queue>
12 using namespace std;
13 typedef bool boolean;
14
15 const int N = 26;
16
17 int T;
18 int n;
19 int req[N], own[N];
20 int g[N][N];
21 int f[N];
22 int lab[N];
23 boolean vis[N];
24
25 inline void init() {
26     for (int i = 0; i < 24; i++)
27         scanf("%d", req + i);
28     scanf("%d", &n);
29     memset(own, 0, sizeof(own));
30     for (int i = 1, x; i <= n; i++) {
31         scanf("%d", &x);
32         own[x]++;
33     }
34 }
35
36 queue<int> que;
37 inline boolean check(int mid) {
38     for (int i = 1; i < 8; i++)
39         g[16 + i][i] = req[i - 1] - mid;
40     g[0][24] = mid;
41     g[24][0] = -mid;
42     fill(f, f + 25, -2333);
43     memset(lab, 0,    sizeof(lab));
44     que.push(0);
45     f[0] = 0;
46     while (!que.empty()) {
47         int e = que.front();
48         que.pop();
49         vis[e] = false;
50         if (++lab[e] >= 25)    return false;
51         for (int i = 0; i < 25; i++)
52             if (g[e][i] >= -1000 && f[e] + g[e][i] > f[i]) {
53                 f[i] = f[e] + g[e][i];
54                 if (!vis[i]) {
55                     que.push(i);
56                     vis[i] = true;
57                 }
58             }
59     }
60 //    for (int i = 0; i <= 24; i++)
61 //        cerr << f[i] << " ";
62 //    cerr << endl;
63     return true;
64 }
65
66 inline void solve() {
67     memset(g, 0x80, sizeof(g));
68     for (int i = 0; i < 24; i++)
69         g[i][i + 1] = 0, g[i + 1][i] = -own[i];
70     for (int i = 8; i <= 24; i++)
71         g[i - 8][i] = req[i - 1];
72     int l = 0, r = n;
73     while (l <= r) {
74         int mid = (l + r) >> 1;
75         if (check(mid))
76             r = mid - 1;
77         else
78             l = mid + 1;
79     }
80     if (r == n)
81         puts("No Solution");
82     else
83         printf("%d\n", r + 1);
84 }
85
86 int main() {
87     scanf("%d", &T);
88     while(T--) {
89         init();
90         solve();
91     }
92     return 0;
93 }
posted @ 2018-02-16 10:38  阿波罗2003  阅读(...)  评论(...编辑  收藏