Description
有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行。
机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求。
Input
对于每组测试数据:
第一行是两个数字n, m,表示工作表里面有n个任务, 有m个询问;
第二行是n个不同的数字t1, t2, t3....tn,表示机器在ti时间执行第i个任务。
接下来m行,每一行有一个数字q,表示在q时间有一个工作表之外的任务请求。
特别提醒:m个询问之间是无关的。
[Technical Specification]
1. T <= 50
2. 1 <= n, m <= 10^5
3. 1 <= ti <= 2*10^5, 1 <= i <= n
4. 1 <= q <= 2*10^5
Output
Sample Input
1 5 5 1 2 3 5 6 1 2 3 4 5
Sample Output
4 4 4 4 7
二分查找区间中时间大于工作的--
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
bool fafe[200100];
int ge[200100];
void s()
{
int a,n,m;
memset(fafe,false,sizeof(fafe));
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++)
{
scanf("%d",&a);
fafe[a]=true;
}
ge[0]=0;
for (int i=1;i<200100;i++)
{
ge[i]=ge[i-1];
if (fafe[i])
ge[i]++;
}
while (m--)
{
scanf("%d",&a);
if (!fafe[a])
{
printf("%d\n",a);
continue;
}
int L=a,M,R=200010,ans=0;
while (L<=R)
{
M=(L+R)/2;
if (ge[M]-ge[a]<M-a)
{
R=M-1;
ans=M;
}
else
L=M+1;
}
// printf("%d 666\n",ans);
if (ans)
printf("%d\n",ans);
// else
// printf("")
}
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
s();
}
return 0;
}
Description
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n × m multiplication table, where the element on the intersection of the i-th row and j-th column equals i·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all n·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
Input
The single line contains integers n, m and k(1 ≤ n, m ≤ 5·105; 1 ≤ k ≤ n·m).
Output
Print the k-th largest number in a n × m multiplication table.
Sample Input
2 2 2
2
2 3 4
3
1 10 5
5
Hint
A 2 × 3 multiplication table looks like this:
1 2 3 2 4 6
无奈---开始一直想要用二维的查找---然后不知道怎么写--
让求的是乘积中的一个数--1-n*m二分即可---保留最小的个数大于等于k的那个数---
代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL __int64
LL n,m,k;
LL zhao(LL xx)
{
LL lp=0;
for (LL i=1;i<=m;i++)
lp+=min(xx/i,n);
return lp;
}
int main()
{
scanf("%I64d%I64d%I64d",&n,&m,&k);
LL ans=0,L=1,R=k,M;
if (m>n)
{
M=m;m=n;n=M;
}
while (L<=R)
{
M=(L+R)/2;
if (zhao(M)>=k)
{
ans=M;
R=M-1;
}
else
L=M+1;
}
printf("%I64d\n",ans);
return 0;
}