比较经典的差分约束

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

题目大意

直接挂loj的翻译算了……

题目分析

算是差分约束类型有难度并且挺经典的题目。

设$s_i$为$i$时刻能够开始工作的人数;$x_i$为$i$时刻实际雇佣的人数。于是有$x_i≤num_i$。设$a_i$为$i$时刻至少需要工作的人数。有:

$x_{i-7}+x_{i-6}+...+x_{i-1}+x_i≥a_i$

设$t_i=x_1+x_2+...+x_i$,则得到

$0≤t_i-t_{i-1}≤s_i,0≤i≤23,$

$t_i-t_{i-8}≥a_i,8≤i≤23,$

$t_{23}+t_i-t_{i+16}≥a_i,0≤i≤7$

那么在建出约束关系之后,就是枚举$t_{23}$.

之后就是处理的细节需要注意一下。

 #include<cstdio>
#include<cctype>
#include<cstring>
const int maxn = ;
const int maxm = ; struct Edge
{
int y,val;
Edge(int a=, int b=):y(a),val(b) {}
}edges[maxm];
int T,n,ans,a[],s[maxn],dis[maxn];
int edgeTot,head[maxn],nxt[maxm];
bool vis[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void init()
{
edgeTot = ;
memset(dis, -0x3f3f3f3f, sizeof dis);
// memset(dis, 0, sizeof dis);
memset(vis, , sizeof vis);
memset(head, -, sizeof head);
}
void addedge(int u, int v, int c)
{
edges[++edgeTot] = Edge(v, c), nxt[edgeTot] = head[u], head[u] = edgeTot;
}
bool dfs(int x)
{
vis[x] = ;
for (int i=head[x]; i!=-; i=nxt[i])
{
int v = edges[i].y, w = edges[i].val;
if (dis[v] < dis[x]+w){
dis[v] = dis[x]+w;
if (vis[v]||dfs(v)) return ;
}
}
vis[x] = ;
return ;
}
bool check(int w)
{
init(), dis[] = ;
// for (int i=1; i<=23; i++) addedge(i-1, i, 0);addedge(23, 0, 0);
// for (int i=1; i<=23; i++) addedge(i, i-1, -w);addedge(0, 23, -w);
for (int i=; i<=; i++) addedge(i-, i, ), addedge(i, i-, -s[i]);
// for (int i=8; i<=23; i++) addedge(i-8, i, a[i]);
// for (int i=0; i<=7; i++) addedge(i+16, i, s[i]-w);    //注意细节处理
for (int i=; i<=; i++) addedge(i-, i, a[i]);
for (int i=; i<=; i++) addedge(i+, i, a[i]-w);
addedge(, , w);
return dfs();
}
int main()
{
T = read();
while (T--)
{
memset(s, , sizeof s);
for (int i=; i<=; i++) a[i] = read();
n = read(), ans = -;
for (int i=; i<=n; i++) s[read()+]++;
for (int i=; i<=n; i++)
if (!check(i)){
ans = i;
break;
}
if (ans==-) puts("No Solution");
else printf("%d\n",ans);
}
return ;
}

END

【差分约束】poj1275Cashier Employment的更多相关文章

  1. 【POJ1275】Cashier Employment 差分约束

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

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

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

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

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

  4. POJ1275 Cashier Employment 二分、差分约束

    传送门 题意太长 为了叙述方便,将题意中的$0$点看作$1$点,$23$点看做$24$点 考虑二分答案(其实从小到大枚举也是可以的) 设$x_i$是我们选的雇员第$i$小时开始工作的人数,$s_i$是 ...

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

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

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

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

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

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

  8. ACM差分约束笔记

    https://www.cnblogs.com/31415926535x/p/10463112.html 很早之前学最短路的时候就看了一眼差分约束,,当时以为这种问题不怎么会出现,,而且当时为了只为了 ...

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

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

随机推荐

  1. Nginx在Linux里安装 以及nginx实现负载均衡

    Nginx 一.在Linux里安装软件 1. rpm命令 rpm: redhat package manager,红帽软件包管理套件 常用命令: 安装:rpm -ivh 软件包 i :安装模式 v : ...

  2. curl_setopt 注意

    最近碰到好多奇怪的BUG,今天就是一个例子. 我在用CURL调用麦考林的接口,在浏览器测试完全没问题,调用全都成功.但是用命令行执行PHP时,却一直不行,返回http code 302错误.百思不得其 ...

  3. 老男孩IT教育-每日一题汇总

    老男孩IT教育-每日一题汇总 第几天 第几周 日期 快速访问链接 第123天 第二十五周 2017年8月25日 出现Swap file….already exists以下错误如何解决? 第122天 2 ...

  4. winform 程序隐藏窗口运行

    DWPublishForm frm = new DWPublishForm(); frm.IsAutoUpdate = true; frm.ShowInTaskbar = false; frm.For ...

  5. 09SpringAopAdvice

    Spring原生的经典模式 实现 AOP 通知: 前置通知:在目标方法执行之前执行,不能改变方法的执行流程和结果! 实现 MethodBeforeAdvice接口! 后置通知:在目标方法执行之后执行, ...

  6. Java基础语法(Eclipse)

    Java基础语法 今日内容介绍 u Eclipse开发工具 u 超市库存管理系统 第1章 Eclipse开发工具 Eclipse是功能强大Java集成开发工具.它可以极大地提升我们的开发效率.可以自动 ...

  7. [20190620]日常学习记录(三)-初识promise及vuex

    在学习promise之前重温了Ajax的原生js实现, 在原生js中发送一个http请求首先new XMLHttpRequest() 然后定义状态变更事件 浏览器监听请求的状态,触发不同状态下相应的代 ...

  8. iOS 最新判断机型设备方法

    #define isIphoneXXS [UIScreen mainScreen].bounds.size.width == 375  && [UIScreen mainScreen] ...

  9. SlickEdit 18.0 版本发布 同时更新破解文件

    18.0版本没有太大的惊喜 多了如下功能 Multiple Document Group Interface Repository Log Browser History Diff Support f ...

  10. 如何正确配置 Nginx + PHP ???

    本文转自如何正确配置 Nginx + PHP,如有侵权,请联系管理员及时删除!