Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9078   Accepted: 3515

Description

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

Source

题意:

在一家超市里,每个时刻都需要有营业员看管,R(i) (0 <= i < 24)表示从i时刻开始到i+1时刻结束需要的营业员的数目,现在有N(N <= 1000)个申请人申请这项工作,并且每个申请者都有一个起始工作时间 ti,如果第i个申请者被录用,那么他会从ti时刻开始连续工作8小时。现在要求选择一些申请者进行录用,使得任何一个时刻i,营业员数目都能大于等于R(i)。求出至少需要录用多少营业员。

我们用$S[i]$表示一天内前$i+1$个小时录用的人员,

当$i>=7$时,我们需要满足$s[i]-s[i-8]>=R[i]$

当$0<=i<7$,经过推倒不难发现,我们需要满足$s[i]+s[23]-s[i+16]>=R[i]$

同时,因为题目中人数的限制,我们还需要满足$0<=s[i]-s[i-1]<=b[i]$

这样这道题就看起来可做了

但是我们的第二个式子左边有三项,不过还好$s[23]$是个常数项,我们可以二分解决

因为是求最小,所以我们按照套路连边,求最长路

#include<cstdio>
#include<queue>
#include<cstring>
#define INF 1e8+10
using namespace std;
const int MAXN=1e5+;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,MAXN,stdin),p1==p2)?EOF:*p1++)
char buf[MAXN],*p1=buf,*p2=buf;
inline int read()
{
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int R[MAXN],N,pep[MAXN],dis[MAXN],vis[MAXN];
struct node
{
int u,v,w,nxt;
}edge[MAXN];
int head[MAXN],num=;
inline void AddEdge(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;
edge[num].nxt=head[x];
head[x]=num++;
}
void PRE()
{
memset(head,-,sizeof(head));
memset(vis,,sizeof(vis));
num=;
}
int SPFA(int val)
{
memset(dis,-0x7f,sizeof(dis));
queue<int>q;
dis[]=;
q.push();
while(q.size()!=)
{
int p=q.front();q.pop();
vis[p]=;
if(p==&&dis[p]>val) return ;
for(int i=head[p];i!=-;i=edge[i].nxt)
{
if(dis[edge[i].v]<dis[p]+edge[i].w)
{
dis[edge[i].v]=dis[p]+edge[i].w;
if(!vis[edge[i].v]) vis[edge[i].v]=,q.push(edge[i].v);
}
}
}
return dis[]<=val?:; }
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int QWQ=read();
while(QWQ--)
{
memset(pep,,sizeof(pep));
for(int i=;i<=;i++) R[i]=read();
N=read();
for(int i=;i<=N;i++)
pep[read()]++;
int r=N+,l=;
int ans=INF;
while(l<=r)
{
PRE();
int mid=l+r>>;
for(int i=;i<=;i++) AddEdge(i,i+,),AddEdge(i+,i,-pep[i]);
for(int i=;i<=;i++) AddEdge(i-,i+,R[i]);
AddEdge(,,mid);AddEdge(,,-mid);
for(int i=;i<;i++) AddEdge(i+,i+,R[i]-mid);
if(SPFA(mid)) ans=mid,r=mid-;
else l=mid+;
}
if(ans>N) printf("No Solution\n");
else printf("%d\n",ans);
}
return ;
}

POJ1275 Cashier Employment(差分约束)的更多相关文章

  1. 【POJ1275】Cashier Employment 差分约束

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

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

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

  3. POJ1275 Cashier Employment[差分约束系统 || 单纯形法]

    Cashier Employment Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7997   Accepted: 305 ...

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

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

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

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

  6. Cashier Employment 差分约束

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

  7. 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 n ...

  8. [HDU 1529]Cashier Employment(差分约束系统)

    [HDU 1529]Cashier Employment(差分约束系统) 题面 有一个超市,在24小时对员工都有一定需求量,表示为\(r_i\),意思为在i这个时间至少要有i个员工,现在有n个员工来应 ...

  9. POJ1275 Cashier Employment 【二分 + 差分约束】

    题目链接 POJ1275 题解 显然可以差分约束 我们记\(W[i]\)为\(i\)时刻可以开始工作的人数 令\(s[i]\)为前\(i\)个时刻开始工作的人数的前缀和 每个时刻的要求\(r[i]\) ...

随机推荐

  1. ueditor后台配置项返回格式出错,上传功能将不能正常使用

    和https://ask.csdn.net/questions/382087问题一样. java+jsp1.config.json配置不对2.百度依赖的jar包没引入3.请求controller.js ...

  2. SpringMVC FistMVC详解

    实现一个简单的SpringMVC框架的配置 1.依赖 这是mybatis+spring+现在需要的依赖 <dependency> <groupId>junit</grou ...

  3. Python - 调试Python代码的方法

    调试(debug) 将可疑环节的变量逐步打印出来,从而检查哪里是否有错. 让程序一部分一部分地运行起来.从核心功能开始,写一点,运行一点,再修改一点. 利用工具,例如一些IDE中的调试功能,提高调试效 ...

  4. 利用vi编辑器创建和编辑正文文件(二)

    末行模式下的命令 1.       w:写文件,将编辑的内容保存到文件系统. 2.       w!:如果只读文件,强制写入系统. 3.       q!:退出vi,但文件内容修改的话,系统要提示是否 ...

  5. 手动实现一个虚拟DOM算法

    发现一个好文:<深度剖析:如何实现一个 Virtual DOM 算法> 源码 文章写得非常详细,仔细看了一遍代码,加了一些注释.其实还有有一些地方看的不是很懂(毕竟我菜qaq 先码 有时间 ...

  6. Java内存溢出异常(下)

    此篇是上一篇文章Java内存溢出异常(上)的续篇,没有看过的同学,可以先看一下上篇.本篇文章将介绍剩余的两个溢出异常:方法区和运行时常量池溢出. 方法区和运行时常量池溢出 这部分为什么会放在一起呢?在 ...

  7. Android利用Intent与其他应用交互

    前言: 上一篇博客给大家聊了Intent的定义,分类.属性和功能,相信大家对于Intent在Android中的作用已经清楚,这一篇博客将会给大家聊Intent的用法. Android系统的一个重要特性 ...

  8. git 撤回放到暂存区的文件

    git reset HEAD filename 如:git reset HEAD test.txt 或者使用 git reset .  撤回所有文件(注意后面还有个.)

  9. 一个前端开发者换电脑的过程(git篇)

    一,安装git. 要安装git,首先得把它下载下来.去到git官网. 现在开始安装. 讲真,这些东西哪些要勾哪些不要勾我也不清楚,所以全部都按默认的来,一路next. 现在再打开vscode的终端,发 ...

  10. arcgis 加载png图片实现图片跟随地图缩放 和图片的动态播放

    效果图: 主要原理: png加载到地图上是不可能的, 图像本身是没有地理信息的. 这里采用一种办法, 在地图上创建一个图形图层, 图形图层放一个矩形,给这个矩形用一个图片填充符号填充. 关键技术点: ...