题目
给你三个数n,m,k
然后会根据n,m生成数列
1×1,1×2,······,1×m
2×1,2×2,······,2×m
···
n×1,n×2,······,n×m
问你将这些数字中第k大的数字是多少?
题解思路
看清题目是序列 。 所以可以先将每个序列的最后一位放入优先队列(肯定最大)。
每次从大顶堆里取一个然后push进他的乘数减一 。
这样就能保证堆里可以表示所有序列值,以及堆顶是最大值。
取出k-1次后。
堆顶的肯定是第k大值。
这题也能二分,看代码。
AC代码
优先队列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair <long long , long long > PII ;
int main ()
{
ios::sync_with_stdio(false);
int n , m , k ;
cin >> n >> m >> k ;
priority_queue <PII> q ;
for (int i = 1 ; i <= n ; i++ )
{
q.push({i*m,i}) ;
}
k--;
while (k--)
{
auto p = q.top() ;
// cout << p.first << "\n" ;
q.pop();
q.push({p.first - p.second , p.second }) ;
}
cout << q.top().first << "\n" ;
return 0 ;
}
二分写法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
using namespace std;
const int INF = 0x3f3f3f3f;
int n , m , k ;
bool che ( long long tt )
{
long long pp = 0 ;
for (int i = 1 ; i <= n ; i++ )
{
pp += max( (long long) 0 , m - ( tt + i - 1 ) / i + 1 ) ;
}
return pp >= k ;
}
int main ()
{
ios::sync_with_stdio(false) , cin.tie(nullptr) ;
cin >> n >> m >> k ;
long long t1 = 1 , t2 = 1e12 + 2 ;
while ( t2 - t1 > 1 )
{
long long mid = ( t1 + t2 ) >> 1 ;
if (che(mid))
t1 = mid ;
else
t2 = mid ;
}
if ( che(t2) )
t1 = t2 ;
cout << t1 << "\n" ;
return 0 ;
}