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 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.
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
Output
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 - 差分约束 - 二分答案的更多相关文章
- hdu1529 Cashier Employment[差分约束+二分答案]
这题是一个类似于区间选点,但是有一些不等式有三个未知量参与的情况. 依题意,套路性的,将小时数向右平移1个单位后,设$f_i$为前$i$小时工作的人数最少是多少,$f_{24}$即为所求.设$c_i$ ...
- POJ 1275 Cashier Employment(差分约束)
http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...
- POJ 1275 Cashier Employment 挺难的差分约束题
http://poj.org/problem?id=1275 题目大意: 一商店二十四小时营业,但每个时间段需求的雇员数不同(已知,设为R[i]),现有n个人申请这份工作,其可以从固定时间t连续工作八 ...
- 图论(差分约束系统):POJ 1275 Cashier Employment
Cashier Employment Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7651 Accepted: 288 ...
- 【POJ1275】Cashier Employment 差分约束
[POJ1275]Cashier Employment 题意: 超市经历已经提供一天里每一小时需要出纳员的最少数量————R(0),R(1),...,R(23).R(0)表示从午夜到凌晨1:00所需要 ...
- POJ1275/ZOJ1420/HDU1529 Cashier Employment (差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题意:一商店二十四小时营业,但每个时间段需求的出纳员不同,现有n个人申请这份工作, ...
- HDU.1529.Cashier Employment(差分约束 最长路SPFA)
题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若 ...
- poj 1275 Cashier Employment
http://poj.org/problem?id=1275 #include <cstdio> #include <cstring> #include <algorit ...
- Cashier Employment 差分约束
题意:有一个超市需要一些出纳员,已给出这个超市在各个时间段(0-1,1-2,2-3...共24个时间段)至少需要的出纳员数目,现在前来应聘有n个人,每个人都有一个固定的开始工作的时间,这也意味着从这个 ...
随机推荐
- sqli-labs(十五)(堆叠注入)
第三十八关: 后面好几关都是堆叠注入.简单介绍下: Stacked injections:堆叠注入.从名词的含义就可以看到应该是一堆sql语句(多条)一起执行.而在真实的运用中也是这样的,我们知道在m ...
- jQuery-点击查看联系方式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jQuery-手风琴伸缩效果
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 3.GDScript(1)概览
GDScript 是上面提到的用于Godot的主要语言.和其他语言相比,它与Godot高度整合,有许多优点: 简单,优雅,设计上为Lua.Python.Squirrel等语言用户所熟悉. 加载和编译速 ...
- UML之状态机图
状态机图 基本概念: 状态机图,UML 1.x规范中称状态图,是一个展示状态机的图. 状态机图基本上就是一个状态机中元素的投影,这也就意味着状态机图包括状态机的所有特征.状态机图显示了一个对象如何根据 ...
- 财务自由VS精神自由
财务自由 财务自由,在物质层面改善人的生活.它使人不愁生计.住更宽敞明亮的房间,穿锦衣绸缎,自由自在地游玩,做自己想做的事儿.可是,这就是它的能力所及了.钱无法改变人的品味.审美和人格.它也无法告诉人 ...
- [转]Hive开发经验问答式总结
本文转载自:http://www.crazyant.net/1625.html 本文是自己开发Hive经验的总结,希望对大家有所帮助,有问题请留言交流. Hive开发经验思维导图 Hive开发经验总结 ...
- flask模板应用-加载静态文件:添加Favicon,使用CSS框架,使用宏加载静态资源
加载静态文件 一个Web项目不仅需要HTML模板,还需要许多静态文件,比如CSS.JavaScript文件.图片和声音声.在flask程序中,默认需要将静态文件存储在与主脚本(包含程序实例的脚本)同级 ...
- C#简单线程
一.实例1 static void Main(string [] args) { Console.WriteLine("开始线程"); startFunc(); Console.W ...
- div等比例缩放-------纯CSS实现自适应浏览器宽度的正方形
摘自:https://blog.csdn.net/u010513603/article/details/78200207 1.方案一:CSS3 vw 单位 CSS3 中新增了一组相对于可视区域百分比的 ...