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

 /**
* 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 ;
}

poj 1275 Cashier Employment - 差分约束 - 二分答案的更多相关文章

  1. hdu1529 Cashier Employment[差分约束+二分答案]

    这题是一个类似于区间选点,但是有一些不等式有三个未知量参与的情况. 依题意,套路性的,将小时数向右平移1个单位后,设$f_i$为前$i$小时工作的人数最少是多少,$f_{24}$即为所求.设$c_i$ ...

  2. POJ 1275 Cashier Employment(差分约束)

    http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...

  3. POJ 1275 Cashier Employment 挺难的差分约束题

    http://poj.org/problem?id=1275 题目大意: 一商店二十四小时营业,但每个时间段需求的雇员数不同(已知,设为R[i]),现有n个人申请这份工作,其可以从固定时间t连续工作八 ...

  4. 图论(差分约束系统):POJ 1275 Cashier Employment

    Cashier Employment Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7651   Accepted: 288 ...

  5. 【POJ1275】Cashier Employment 差分约束

    [POJ1275]Cashier Employment 题意: 超市经历已经提供一天里每一小时需要出纳员的最少数量————R(0),R(1),...,R(23).R(0)表示从午夜到凌晨1:00所需要 ...

  6. POJ1275/ZOJ1420/HDU1529 Cashier Employment (差分约束)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题意:一商店二十四小时营业,但每个时间段需求的出纳员不同,现有n个人申请这份工作, ...

  7. HDU.1529.Cashier Employment(差分约束 最长路SPFA)

    题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...

  8. poj 1275 Cashier Employment

    http://poj.org/problem?id=1275 #include <cstdio> #include <cstring> #include <algorit ...

  9. Cashier Employment 差分约束

    题意:有一个超市需要一些出纳员,已给出这个超市在各个时间段(0-1,1-2,2-3...共24个时间段)至少需要的出纳员数目,现在前来应聘有n个人,每个人都有一个固定的开始工作的时间,这也意味着从这个 ...

随机推荐

  1. 强力上攻后,在缓解期结束时,MACD将死叉,的后续情形

    强力上攻后,在缓解期结束时,MACD将死叉,的后续情形

  2. C++ 使用openssl库实现 DES 加密——CBC模式 && RSA加密——公加私解——私加公解

    之前工作上需要用C++把软件生成的用户序列号用des加密cbc的模式,加密后为二进制,转化为十六进制,然后提供给java写的授权码管理平台. java平台会根据用户序列号,生成一个授权码,授权码是用r ...

  3. Linux SSH 免秘钥登录

    SSH 免秘钥登录 ssh:是一种安全加密协议 ssh  username@hostname     ssh gongziyuan.com:以当前用户登录该机器(如果不是当前用户,需要这么干:ssh ...

  4. 网站的title添加图片

    将图片作为ico格式,大小设置为16 * 16px左右,太大显示不完整, 命名需为"favicon.ico", 命名需为"favicon.ico", 命名需为& ...

  5. python基础-abstractmethod、__属性、property、setter、deleter、classmethod、staticmethod

    python基础-abstractmethod.__属性.property.setter.deleter.classmethod.staticmethod

  6. Git-什么是分支

    为了理解什么是分支,我们先要回顾Git是如何存储数据的. Git并不会保存文件的差异值或者说变化量,而是直接保存文件的快照. 在Git中提交时,会保存一个commit对象,该对象包含一个指向暂存内容快 ...

  7. <6>Lua元表和冒号 self

    Lua中没有像C.C++.JAVA中的类概念,面向对象等 ,但我们可以模拟出来 1. Lua中有个很重要的概念元表 设置元表setmetatable()函数  获取元表getmetatable()函数 ...

  8. Day9 面向对象高级

    一.方法 方法包括:普通方法.静态方法和类方法,三种方法在内存中都归属于类,区别在于调用方式不同. 普通方法:由对象调用:至少一个self参数:执行普通方法时,自动将调用该方法的对象赋值给self: ...

  9. Tomcat JAR包冲突报错

    查看Tomcat下有两个PDF加密的jar包如图: 删除这个bcprov-jdk14-138.jar包,然后重启Tomcat就好了. 这个jar包和Tomcat中的一个包冲突,反复调用导致的. 参考: ...

  10. mysql 创建用户,删除用户,增加权限

    1,查询mysql 数据库已经存在的用户: SELECT USER,HOST FROM MYSQL.USER; 2,创建mysql 用户: '; USERNAME:用户名 HOST:主机,PASSWO ...