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. unity3d-代码控制游戏角色控制器移动

    先上一个gif看看效果.因为图片大小限制.所以录制的比较小.个人认为效果比较牵强.特别是里面的逻辑代码. 不过我还是认为一切是为了先实现,因为我是刚接触的新手. 工程结构图 这次实现的效果是: 1:摄 ...

  2. XXE攻击学习

    环境:lAMP simplexml_load_string.php代码内容 <?php $data = file_get_contents('php://input'); $xml = simp ...

  3. JavaScript-switch-case运用-案例

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. scala mysql jdbc oper

    package egsql import java.util.Properties import com.sun.org.apache.xalan.internal.xsltc.compiler.ut ...

  5. Core Java Fundation

    http://www.cnblogs.com/cmfwm/p/7671188.html http://blog.csdn.net/fuckluy/article/details/50614983 ht ...

  6. Java IO/NIO教程

    Java IO教程 http://tutorials.jenkov.com/java-io/index.html Java NIO教程 英文版: http://tutorials.jenkov.com ...

  7. html5-相对定位

    *{    margin: 0px;    padding: 0px;}div{    width: 300px;    height: 300px;}#div1{    background: rg ...

  8. 【Elasticsearch学习之一】Elasticsearch

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 一.概念ElasticSearch: 基于Lucene全文搜 ...

  9. 【Hbase学习之一】Hbase 简介

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-2.1.3 ...

  10. PersistenceContext.properties()

    在做 Spring + SpringMVC + SpringData 时,单元测试 报这个错误: java.lang.NoSuchMethodError:javax.persistence.Persi ...