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$ 只船,每只船的长度均为 ...
随机推荐
- java实体类如果不重写toString方法,会如何?
先认识一下Object Object 类的 toString 方法 返回一个字符串,该字符串由类名(对象是该类的一个实例).at 标记符“@”和此对象哈希码的无符号十六进制表示组成.换句话说,该方法返 ...
- 多媒体开发之--- h264 图像、帧、片、NALU
图像.帧.片.NALU 是学习 H.264的人常常感到困惑的一些概念,我在这里对自己的理解做一些阐述,欢迎大家讨论: H.264 是一次概念的革新,它打破常规,完全没有 I 帧.P帧.B 帧的概念,也 ...
- 【入门】创建express项目
1.创建项目(图解) 2.访问http://localhost:3000/就看到熟悉的页面了 3.查看项目目录 参考文档:http://jingyan.baidu.com/article/92 ...
- 自定义WPF ListBox的选择样式
(下图:进行多项选择的ListBox) 首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBr ...
- DLX精确覆盖与重复覆盖模板题
hihoCoder #1317 : 搜索四·跳舞链 原题地址:http://hihocoder.com/problemset/problem/1317 时间限制:10000ms 单点时限:1000ms ...
- windows下php配置redis
方法/步骤 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本 2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2.5-5.5-ts-vc11- ...
- Residual (numerical analysis)
In many cases, the smallness of the residual means that the approximation is close to the solution, ...
- php自定义函数: 加密下载地址
function getdownurl($downurl, $extime = "3600", $serverid = 1) { if (empty($downurl)) { re ...
- 【python】-- 队列(Queue)、生产者消费者模型
队列(Queue) 在多个线程之间安全的交换数据信息,队列在多线程编程中特别有用 队列的好处: 提高双方的效率,你只需要把数据放到队列中,中间去干别的事情. 完成了程序的解耦性,两者关系依赖性没有不大 ...
- 【题解】POJ2279 Mr.Young′s Picture Permutations dp
[题解]POJ2279 Mr.Young′s Picture Permutations dp 钦定从小往大放,然后直接dp. \(dp(t1,t2,t3,t4,t5)\)代表每一行多少人,判断边界就能 ...