题目链接:http://codeforces.com/gym/101987
题目描述
ICPC manager plans a new project which is to be carried out for n days. In this project, m persons numbered from 1 to m are supposed to work. Each day j (1 ≤ j ≤ n) requires dj persons, and each person i (1 ≤ i ≤ m) wants to work wi days.

To increase the efficiency in performing the project, the following two conditions should be satisfied:
    1.each person works for only consecutive w days when he/she works, and
    2.each person can work again after he/she has a rest for at least h days.
ICPC manager wants to find a working plan to assign the working days for all persons such that the number of working days of each person i (1 ≤ i ≤ m) is equal to wi and the number of persons who work for each day j (1 ≤ j ≤ n) is equal to dj, and above two conditions are also satisfied.

For example, assume the project is carried out for n = 9 days, and m = 4 persons participate in the project. Let w = 2 and h = 1. Also, assume (w1, w2, w3, w4) = (4, 4, 6, 2) and (d1, d2, d3, d4, d5, d6, d7, d8, d9) = (1, 3, 2, 1, 2, 1, 1, 3, 2). The table below shows a feasible solution where the i-th row corresponds to person i, and the j-th column corresponds to day j. If person i works or has a rest in day j, the value of the table element with row i and column j is 1 or 0, respectively.

Given m, n, w, h, wi (1 ≤ i ≤ m) which is a multiple of w, and dj (1 ≤ j ≤ n), write a program to find a feasible solution as a working plan.

输入
Your program is to read from standard input. The input starts with a line containing four integers, m, n, w, h (1 ≤ m ≤ 2,000, 1 ≤ n ≤ 2,000, 1 ≤ w, h ≤ n). The following line contains m integers where the i-th (1 ≤ i ≤ m) integer represents wi (1 ≤ wi ≤ n) which is a multiple of w. The next line contains n integers where the j-th (1 ≤ j ≤ n) integer represents dj (0 ≤ dj ≤ m).

输出
Your program is to write to standard output. If there is a feasible working plan, print 1 in the first line followed by m lines, each i-th (1 ≤ i ≤ m) line should contain wi/w integers. These integers form an increasing sequence of first days that person i works in the feasible plan. If there is no feasible working plan, print only -1 in the first line. The first sample below corresponds to the example given in the table above.

样例输入
样例数据

4 9 2 1
4 4 6 2
1 3 2 1 2 1 1 3 2
样例输出

1
1 8
2 7
2 5 8
4
题意:有m个工人,要工作n天,每个工人可以连续工作w天,之后就要休息h天,给定每个工人的工作天数b[i],和每天需要的工人数a[i],问工人的数量能否满足每天的工作安排,若可以,输出1,并输出每个工人每次开始工作的日期(每个工人的工作次数为b[i]/w),若不能,直接输出-1.

题解:建立两个优先队列,工作队列Q和休息队列T,Q的优先级是工作次数,工作次数越多的越先进入工作队列,T的优先级是休息之后可以开始工作的时间,工作时间越早越先进入队列。工作队列的每个工人工作w天之后进入休息队列,休息队列里记录休息h天之后可以开始工作的日期,若这个日期小于当天日期,就可以进入工作队列

因为每个工人可以连续工作,在用一个vis数组记录每天需要新加入的工人数量

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int n, m, w, h;
int a[], b[], vis[];//vis记录每天需要新加入几个工人
vector<int>v[];
typedef struct node
{
int num;//编号
int cnt;//工作次数
int time;//休息之后可以开始工作的日期
}node; typedef struct cmp1//对队列T,可以开始工作的日期越早越先入队列
{
bool operator () (const node &x, const node &y)
{
return x.time > y.time;
}
}cmp1;
typedef struct cmp2//对队列Q,工作次数越多的越先进入队列
{
bool operator () (const node &x, const node &y)
{
return x.cnt < y.cnt;
} }cmp2;
priority_queue<node, vector<node>, cmp2>Q;//工作队列
priority_queue<node, vector<node>, cmp1>T;//休息队列
int main()
{
node p[];
cin >> m >> n >> w >> h;
for (int i = ; i <= m; i++)
{
cin >> b[i];
p[i].num = i;
p[i].cnt = b[i] / w;
p[i].time = ;
Q.push(p[i]);
}
for (int i = ; i <= n; i++)
cin >> a[i];
int flag = ;
for (int i = ; i <= n; i++)
{
if(a[i]!=)
{
int temp = a[i];
vis[i] = a[i];
for (int j = i; j <= i + w - ; j++)//因为每个人都是连续工作的,之后的w天都要减去a[i]得到需要新加入的人数
a[j] = a[j] - temp;
}
}
node temp;
for (int i = ; i <= n; i++)
{
while (!T.empty())
{
temp = T.top();
if (temp.time <= i)//如果可以开始工作的日期小于当前日期
{
Q.push(temp);
T.pop();
}
else
break;
}
while (!Q.empty() && vis[i])
{
temp = Q.top();
Q.pop();
vis[i]--;
temp.time = i + w + h;
temp.cnt--;
v[temp.num].push_back(i);//记录每次开始工作的日期
if (temp.cnt != )
T.push(temp);
}
if (vis[i] != )//人数不够
{
flag = ;
break;
}
}
if (flag == )
cout << - << endl;
else
{
cout << << endl;
for (int i = ; i <= m; i++)
{
for (int j = ; j < v[i].size(); j++)
{
cout << v[i][j];
if (j != v[i].size())
cout << ' ';
}
cout << endl;
}
}
return ; }

Working Plan 优先队列+贪心的更多相关文章

  1. 【CF526G】Spiders Evil Plan(贪心)

    [CF526G]Spiders Evil Plan(贪心) 题面 洛谷 CodeForces 给定一棵树,要求选择\(y\)条链,满足被链覆盖的所有点在树上联通,且\(x\)必定在联通块中. 对于每次 ...

  2. 最高的奖励 - 优先队列&贪心 / 并查集

    题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...

  3. POJ2431 优先队列+贪心 - biaobiao88

    以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...

  4. hdu3438 Buy and Resell(优先队列+贪心)

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. ZOJ-3410Layton's Escape(优先队列+贪心)

    Layton's Escape Time Limit: 2 Seconds      Memory Limit: 65536 KB Professor Layton is a renowned arc ...

  6. CodeForces - 853A Planning (优先队列,贪心)

    Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are n ...

  7. poj2431(优先队列+贪心)

    题目链接:http://poj.org/problem?id=2431 题目大意:一辆卡车,初始时,距离终点L,油量为P,在起点到终点途中有n个加油站,每个加油站油量有限,而卡车的油箱容量无限,卡车在 ...

  8. H - Expedition 优先队列 贪心

    来源poj2431 A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being ...

  9. 1350: To Add Which? (优先队列+贪心 或者 数组模拟)

    1350: To Add Which? Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitt ...

随机推荐

  1. 【剑指Offer面试编程题】题目1519:合并两个排序的链表--九度OJ

    题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. (hint: 请务必使用链表.) 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每 ...

  2. Intel推出两款新处理器路线图公布

    10月份Intel会推出两款新处理器,一个是酷睿i9-9900KS,全核5GHz处理器,还有就是Cascade Lake-X系列的HEDT旗舰,今年的处理器布局大概就是这样了. 与AMD承诺每年发布一 ...

  3. Zero 初识Sciter

    在浏览有关Sciter技术前,您需要花点时间浏览以下内容. 您是否需要花时间学习Sciter? 如果您的工作或您想从事的工作与桌面应用开发无关,那么您不需要学习Sciter. 如果您不认同HTML\C ...

  4. 「SP2713」GSS4 - Can you answer these queries IV

    传送门 Luogu 解题思路 区间开方以及区间求和. 考虑用线段树来做. 开方操作看似没有任何结合律可言,但这题有另外一个性质: 一个数的初始值不超过 \(10^{18}\) ,而这个数被开方6次左右 ...

  5. 严重: Exception loading sessions from persistent storage

    2011-11-24 10:05:00|  分类: java学习|举报|字号 订阅     当tomcat启动的时候出现下面错误: [ERROR] org.apache.catalina.sessio ...

  6. STM32学习笔记:IIC通信协议详解(附带软件模拟源码)

    什么是IIC(I2C)? IIC 即Inter-Integrated Circuit(集成电路总线),这种总线类型是由飞利浦半导体公司设计出来的一种简单.双向.二线制.同步串行总线.它是一种多向控制总 ...

  7. Linux 内核 编译模块

    背景: 由于调试内核或者由于分区大小限制,有时候内核组件不一定完全需要编进内核中. 所以,在开发中经常将内核组件编译成为模块,等到在恰当的时机加载. 概览: Linux内核模块的编译方法有两种: 1. ...

  8. ElasticSearch学习,入门篇(一)

    概念解析 1.什么是搜索 搜索就是在任何场景下,找寻你想要的信息,这个时候你会输入一段要搜索的关键字,然后期望找到这个关键字相关的有效信息. 2.如果用数据库做搜素会怎么样 select * from ...

  9. 对S7通信的连接的理解以及对比CAN通信协议来理解PLC通讯

    对S7通信的连接的理解以及对比CAN通信协议来理解PLC通讯. 对功能块 SFB12 和 SFB13 的R_ID参数的理解 ? 对于同一个数据包.发送方与接收方的R_ID应该相同. 用下图解释 双向连 ...

  10. yum出现Error downloading packages错误

    yum出现Error downloading packages错误错误表现方式:yum可以list,可以clean cache,但是无法安装,错误提示: Downloading packages: E ...