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.
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
Output
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
/**
* poj
* Problem#1275
* Accepted
* Time: 16ms
* Memory: 672k
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
typedef bool boolean; const int N = ; int T;
int n;
int req[N], own[N];
int g[N][N];
int f[N];
int lab[N];
boolean vis[N]; inline void init() {
for (int i = ; i < ; i++)
scanf("%d", req + i);
scanf("%d", &n);
memset(own, , sizeof(own));
for (int i = , x; i <= n; i++) {
scanf("%d", &x);
own[x]++;
}
} queue<int> que;
inline boolean check(int mid) {
for (int i = ; i < ; i++)
g[ + i][i] = req[i - ] - mid;
g[][] = mid;
g[][] = -mid;
fill(f, f + , -);
memset(lab, , sizeof(lab));
que.push();
f[] = ;
while (!que.empty()) {
int e = que.front();
que.pop();
vis[e] = false;
if (++lab[e] >= ) return false;
for (int i = ; i < ; i++)
if (g[e][i] >= - && f[e] + g[e][i] > f[i]) {
f[i] = f[e] + g[e][i];
if (!vis[i]) {
que.push(i);
vis[i] = true;
}
}
}
// for (int i = 0; i <= 24; i++)
// cerr << f[i] << " ";
// cerr << endl;
return true;
} inline void solve() {
memset(g, 0x80, sizeof(g));
for (int i = ; i < ; i++)
g[i][i + ] = , g[i + ][i] = -own[i];
for (int i = ; i <= ; i++)
g[i - ][i] = req[i - ];
int l = , r = n;
while (l <= r) {
int mid = (l + r) >> ;
if (check(mid))
r = mid - ;
else
l = mid + ;
}
if (r == n)
puts("No Solution");
else
printf("%d\n", r + );
} int main() {
scanf("%d", &T);
while(T--) {
init();
solve();
}
return ;
}