codeforces 589G G. Hiring(树状数组+二分)
题目链接:
G. Hiring
4 seconds
512 megabytes
standard input
standard output
The head of human resources department decided to hire a new employee. He created a test exercise for candidates which should be accomplished in at most m working days. Each candidate has to pass this test exercise. During the j-th day a candidate is allowed to be in the office for at most tj units of time.
Overall, n candidates decided to apply for the job and sent out their resumes. Based on data received the head has defined two parameters describing every candidate: di and ri. The parameter di is the time to get prepared for work which the i-th candidate spends each morning. This time doesn't depend on day. The parameter ri is the total working time needed for the i-th candidate to accomplish the whole test exercise.
Thus the time spent in the office in the j-th day consists of di units of time to get prepared and some units of time to proceed with the exercise. A candidate can skip entire working day and do not come to the office. Obviously in this case he doesn't spend di units of time to prepare.
To complete the exercise a candidate should spend exactly ri units of time working on the exercise (time to prepare is not counted here).
Find out for each candidate what is the earliest possible day when he can fully accomplish the test exercise. It is allowed to skip working days, but if candidate works during a day then he must spend di units of time to prepare for work before he starts progressing on the exercise.
The first line contains two integer numbers n, m (1 ≤ n, m ≤ 2·105) — the number of candidates and the maximum number of working days to do the test exercise.
The second line contains m integer numbers t1, t2, ..., tm (1 ≤ tj ≤ 106) — the durations of working days in time units.
The following n lines contain two integers each: di, ri (0 ≤ di ≤ 106, 1 ≤ ri ≤ 106) — how much time in the beginning of a day is required for i-th candidate before he starts his work on the test exercise and how much time it is needed for him to accomplish this task.
Output a sequence of n integer numbers b1, b2, ..., bn, where bi is the earliest day when the i-th candidate can finish the test exercise.
In case the i-th candidate cannot finish the test exercise in m days output bi = 0.
Days in this problem are numbered from 1 to m in the order they are given in the input.
3 3
4 2 5
1 3
2 5
3 4
1 3 0 题意:m天,每天允许的工作时间是t[i],n个工人,每个工人的工作总量需要时间为r[i],而且每天工作前要预热d[i]时间,问最早能在第几天完成工作;
思路:假设答案是ans,那么在前ans天中每天允许的时间大于d[i]的就是可以工作的一天t[i]-d[i]就是这天能完成的工作量,前ans天的加一块就>=r[i个工人],把这些按大到小排序,然后update到树状数组中,一个数组记录总量,一个记录有多少天update了sum-num*d[i]和r[i]比较就好了,这时又需要二分来快速找到答案ans;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+;
int n,m,a[N],ans[N],num[N],L,R;
long long sum[N];
struct nod
{
friend bool operator< (nod x,nod y)
{
return x.b>y.b;
}
int b,pos;
};
nod qu[N];
struct node
{
int l,r,pos;
};
node po[N];
bool cmp(node x,node y)
{
return x.l>y.l;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int nu)
{
while(x<=m)
{
num[x]++;
sum[x]+=(long long)nu;
x+=lowbit(x);
}
}
long long get_sum(int x)
{
long long s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
long long get_num(int x)
{
long long s=;
while(x>)
{
s+=num[x];
x-=lowbit(x);
}
return s;
}
int get_ans(int x,int s)
{
long long fx=(long long)x;
int l=,r=m,mid;
while(l<=r)
{
mid=(l+r)>>;
if(get_sum(mid)-get_num(mid)*fx<s)l=mid+;
else r=mid-;
}
if(l>m)return ;
return l;
}
int main()
{
memset(num,,sizeof(num));
memset(sum,,sizeof(sum));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d",&a[i]);
qu[i].b=a[i];
qu[i].pos=i;
}
sort(qu+,qu+m+);
for(int i=;i<=n;i++)
{
scanf("%d%d",&L,&R);
po[i].l=L;
po[i].r=R;
po[i].pos=i;
}
sort(po+,po+n+,cmp);
int flag=;
for(int i=;i<=n;i++)
{
while(qu[flag].b>po[i].l&&flag<=m)
{
int y=qu[flag].pos;
update(y,a[y]);
flag++;
}
ans[po[i].pos]=get_ans(po[i].l,po[i].r);
}
for(int i=;i<=n;i++)
{
printf("%d ",ans[i]);
}
return ;
}
codeforces 589G G. Hiring(树状数组+二分)的更多相关文章
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- TZOJ 4602 高桥和低桥(二分或树状数组+二分)
描述 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不算“淹了两次”.举 ...
- POJ 2182 Lost Cows 【树状数组+二分】
题目链接:http://poj.org/problem?id=2182 Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- 树状数组+二分||线段树 HDOJ 5493 Queue
题目传送门 题意:已知每个人的独一无二的身高以及排在他前面或者后面比他高的人数,问身高字典序最小的排法 分析:首先对身高从矮到高排序,那么可以知道每个人有多少人的身高比他高,那么取较小值(k[i], ...
- P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]
题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...
- The Stream of Corning 2( 权值线段树/(树状数组+二分) )
题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...
- 牛客多校第3场 J 思维+树状数组+二分
牛客多校第3场 J 思维+树状数组+二分 传送门:https://ac.nowcoder.com/acm/contest/883/J 题意: 给你q个询问,和一个队列容量f 询问有两种操作: 0.访问 ...
- Codeforces 567D - One-Dimensional Battle Ships - [树状数组+二分]
题目链接:https://codeforces.com/problemset/problem/567/D 题意: 在一个 $1 \times n$ 的网格上,初始摆放着 $k$ 只船,每只船的长度均为 ...
随机推荐
- [转]基于Python的接口测试框架
http://blog.csdn.net/wyb199026/article/details/51485322 背景 最近公司在做消息推送,那么自然就会产生很多接口,测试的过程中需要调用接口,我就突然 ...
- PDP开发环境搭建
1. 安装git 2.创建SSH-rsa钥匙 3. 写入 gitlab 4. 克隆分支 git clone -b dev_pdp_minz_ep_metting git@gitlab.csvw ...
- css3 transition效果
<meta charset="UTF-8"> <style> .btn { display: inline-block; font-size: 12px; ...
- centos samba 服务器的配置和使用
1.安装samba服务 #yum install samba samba-client samba-swat 2.修改配置文件 #vi /etc/samba/smb.conf security = u ...
- 【python】-- web开发之DOM
DOM 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是, ...
- 最简单的 GitExtensions 教程(持续更新中)
一.安装 GitExtensions 下载 GitExtensions 完全版,一直点 Next,安装全部组件. 二.将项目文件夹/文件提交到 Git 服务器(以 GitHub 为例) 新建一个文件夹 ...
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之游戏效果预览(一)
今天看完了李刚老师的<疯狂Android讲义>一书中的第18章<疯狂连连看>,从而学会了如何编写一个简单的Android疯狂连连看游戏. 开发这个流行的小游戏,难度适中,而且能 ...
- android中handler和bundle有什么区别和联系 都是用来传递消息吗都是信息的载体吗
1.handler是消息处理者,通常重写Handler的handleMessage()方法,在方法中处理接收到的不同消息,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Ha ...
- python+NLTK 自然语言学习处理四:获取文本语料和词汇资源
在前面我们通过from nltk.book import *的方式获取了一些预定义的文本.本章将讨论各种文本语料库 1 古腾堡语料库 古腾堡是一个大型的电子图书在线网站,网址是http://www.g ...
- sql把字符数组转换成表
需求:把字符串1,2,3变成表里的行数据 方法:用自定义函数实现 /* 获取字符串数组的 Table */ from sysobjects where id = object_id('Get_StrA ...