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个人,每个人都有一个固定的开始工作的时间,这也意味着从这个 ...
随机推荐
- java-Unicode与中文的转换
package utils; import org.apache.commons.lang3.StringUtils; public class UnicodeUtils { public stati ...
- 记录一则rebuild index消除索引碎片的效果
背景:在一次某客户的停产维护中,有一项例行对大表rebuild索引的操作,本是按部就班的操作,其效果却出乎我的意料,大部分索引的效果前后都有4倍左右的变化,最大的那个索引前后居然差了7倍多,并且重建索 ...
- opencv-resize()放缩函数简介
主要介绍函数resize(); 图像缩放的效果图如下: 主程序代码及函数解释如下所示: /******************************************************* ...
- GetLastError()返回值列表
GetLastError()返回值列表: [0]-操作成功完成.[1]-功能错误.[2]-系统找不到指定的文件.[3]-系统找不到指定的路径.[4]-系统无法打开文件.[5]-拒绝访问.[6]-句柄无 ...
- JavaScript数组去重方法总结
一.双重遍历去重 function onlyFigure(arr) { let newarr = []; const length = arr.length for (let i = 0; i < ...
- python时间和日期
一.time 和 calendar 模块可以用于格式化日期和时间 import time; # 引入time模块 ticks = time.time() print "当前时间戳为:&quo ...
- E. Bear and Drawing
E. Bear and Drawing time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 特征点方法 - Harris和SURF的手工实现
整理去年做的小项目,纪念我的图像处理入门. 因为要在DSP上实现,所以完全手工C代码垒起来的,还要保证和PC端跑的结果一样,觉得可能特殊场景会有用,上传github,没有依赖任何库: 格式注释什么的暂 ...
- Factory Method
Question:Based on the previous article,what could you do if we must add an extra function? For exam ...
- 使用SpringAOP获取一次请求流经方法的调用次数和调用耗时
引语 作为工程师,不能仅仅满足于实现了现有的功能逻辑,还必须深入认识系统.一次请求,流经了哪些方法,执行了多少次DB操作,访问了多少次文件操作,调用多少次API操作,总共有多少次IO操作,多少CPU操 ...