题目:

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

题意:

  德黑兰的一家每天24小时营业的超市,需要一批出纳员来满足它的需求。超市经理雇佣你来帮他解决一个问题————超市在每天的不同时段需要不同数目的出纳员(例如,午夜只需一小批,而下午则需要很多)来为顾客提供优质服务,他希望雇佣最少数目的纳员。
     超市经历已经提供一天里每一小时需要出纳员的最少数量————R(0),R(1),...,R(23)。R(0)表示从午夜到凌晨1:00所需要出纳员的最少数目;R(1)表示凌晨1:00到2:00之间需要的;等等。每一天,这些数据都是相同的。有N人申请这项工作,每个申请者i在每天24小时当中,从一个特定的时刻开始连续工作恰好8小时。定义ti(0<=ti<=23)为上面提到的开始时刻,也就是说,如果第i个申请者被录用,他(或她)将从ti时刻开始连续工作8小时。
    试着编写一个程序,输入R(i),i=0,...,23,以及ti,i=1,...,N,它们都是非负整数,计算为满足上述限制需要雇佣的最少出纳员数目、在每一时刻可以有比对应R(i)更多的出纳员在工作。

分析:

  因为一开始就知道这是一道差分约束题,于是就往这方面想了。然而可爱的是,还是没有想出来。TAT~

  其实就是不会搞循环的情况,感觉我的不等式这样子就有三个元了TAT。我们设xi为我们选择了xi个在i时间开始工作的员工,先不考虑循环的情况,那么对于时间点i,那么在时间点i这个时刻正在工作的人数为x[i-7]+x[i-6]+...x[i],用sum数组维护x,即为sum[i]-sum[i-8]。于是我们得到不等式sum[i]-sum[i-8]>=need[i]。(need[i]为题目输入的时间点i需要的员工数)

  循环的情况怎么搞呢?对于i(i<=7),那么后面某一段时间开始工作的员工也会影响答案,那么我们得到的不等式就是sum[24]-sum[i+16]+sum[i]>=need[i](这里的时间点我从1~24考虑)。这里有三个未知数啊,怎么办呢,于是我想了很久没有想出来。

  竟然是!!二分枚举一下sum[24]就好了,那么就把sum[24]看成常数把!!看到这里,我哭了,世界上竟有如此低智商的我!!

  好吧...因为如果我们雇x个人可以搞定全部,那么雇多一点人也可以的嘛,这就是某种单调性,于是我们可以二分。最后再连一条24->0的边权为-sum[24]就可以了。

  差分约束...

  对了,还有一些隐性条件,千万不要漏,上一题中已经说过了。

  eg. x[i]-x[i-1]>=0

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#include<queue>
#define Maxn 30 int a[Maxn],sum[Maxn];
int first[Maxn],dis[Maxn],cnt[Maxn];
bool inq[Maxn]; struct node
{
int x,y,c,next;
}t[Maxn*];int len; void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} queue<int > q;
bool spfa(int s)
{
while(!q.empty()) q.pop();
memset(dis,,sizeof(dis));
memset(inq,,sizeof(inq));
memset(cnt,,sizeof(cnt));
dis[s]=;inq[s]=;q.push(s);
while(!q.empty())
{
int x=q.front();q.pop();inq[x]=;
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
if(!inq[y])
{
q.push(y);
inq[y]=;
if(++cnt[y]>) return ;
}
}
}
}
return ;
} bool check(int x)
{
for(int i=;i<=;i++) t[len-+i].c=x-a[i];
t[len].c=-x;
return spfa();
} void ffind(int l,int r)
{
//check(1);
while(l<r)
{
int mid=(l+r)>>;
if(check(mid)) r=mid;
else l=mid+;
}
printf("%d\n",l);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int x,n;
memset(sum,,sizeof(sum));
for(int i=;i<=;i++) scanf("%d",&a[i]);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
sum[x+]++;
}
len=;
memset(first,,sizeof(first));
for(int i=;i<=;i++) ins(i-,i,sum[i]);
for(int i=;i<=;i++) ins(i,i-,);
for(int i=;i<=;i++) ins(i,i-,-a[i]);
for(int i=;i<=;i++) ins(i,+i,);
ins(,,);
if(!check(n)) {printf("No Solution\n");continue;}
ffind(,n);
}
return ;
}

[poj1275]

2016-04-12 13:51:47

【POJ1275】Cashier Employment的更多相关文章

  1. 【POJ1275】Cashier Employment 差分约束

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

  2. 【POJ 1275】 Cashier Employment(差分约束系统的建立和求解)

    [POJ 1275] Cashier Employment(差分约束系统的建立和求解) Cashier Employment Time Limit: 1000MS   Memory Limit: 10 ...

  3. 【POJ 1275】 Cashier Employment

    [题目链接] 点击打开链接 [算法] 设Ti为第i小时有多少个出纳员开始工作,Vi表示第i小时有多少个来应聘的出纳员 那么,有 : 1. 0 <= Ti <= Vi 2. Ti + Ti- ...

  4. 【HDOJ】1158 Employment Planning

    简单DP. #include <cstdio> #include <cstring> #include <cstdlib> #include <climits ...

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

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

  6. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. 【HDOJ1529】【差分约束+SPFA+二分】

    http://acm.hdu.edu.cn/showproblem.php?pid=1529 Cashier Employment Time Limit: 2000/1000 MS (Java/Oth ...

  9. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

随机推荐

  1. Android开发之设定Dialog的位置

    今天自定义了一个对话框,但是弹出时默认是显示在屏幕中间.主要代码:menuDialog = new AlertDialog.Builder(this).create();                ...

  2. Eclipse - 安装 run-jetty-run 插件及使用 jrebel 热部署

    安装 run-jetty-run 插件 1. 下载 run-jetty-run 2. 解压至 Eclipse/MyEclipse 安装目录下的 plugin 3. 右键 web 项工程,选择 Run ...

  3. 解决Visual Studio 找不到报表控件、rdlc中文乱码

    找回报表控件 运行安装程序中的 ..\packages\Reporting Services\RVAddon.msi 工具栏,右键选择ReportViewer,注意选择的版本 如果不能编辑报表文件(. ...

  4. 今天把PHP复习了一下.

    记录一下今天复习内容. $_GET[''] $_POST $_SESSION $_COOKIE 常用的 $var='a'; global 全局变量$GLOBALS[''] $$var (动态变量名的变 ...

  5. Fragment的生命周期和Activity之间的通信以及使用

    Fragment通俗来讲就是碎片,不能单独存在,意思就是说必须依附于Activity,一般来说有两种方式把Fragment加到Activity,分为静态,动态. 静态即为右键单击,建立一个Fragme ...

  6. cas系列(三)--HTTP和HTTPS、SSL

    (这段时间打算做单点登录,因此研究了一些cas资料并作为一个系列记录下来,一来可能会帮助一些人,二来对我自己所学知识也是一个巩固.) 本文转自異次元藍客点击打开链接 1.  HTTPS HTTPS(全 ...

  7. 深入理解shared pool共享池之library cache系列二

    背景 继续上文:深入理解shared pool共享池之library cache系列一,学习library cache数据结构,本文主要学习library cache object(lco)的数据结构 ...

  8. html 中设置span的width完美解决方法

    在默认情况下,由于span是行标签,设置width是无效的.只有改变display的属性,才可以实现设置宽度. 1.初步想法 span{ background-color:#ccc; display: ...

  9. CocoaPods详解之----进阶篇

    作者:wangzz原文地址:http://blog.csdn.net/wzzvictory/article/details/19178709转载请注明出处如果觉得文章对你有所帮助,请通过留言或关注微信 ...

  10. POJ 1159 Palindrome(LCS)

    题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...