题目大意
给出n给二元组(ai,bi)(ai,bi),在里面选出至少n-m个二元组,使得选出来min(a)∗min(b)min(a)∗min(b)最大。
题解
很显然,最优答案一定是选n-m个,因为多选并不能使答案更优,反而有可能使答案更劣。
先按照a为第一关键字排序,
然后枚举min(a),在所有对应的b中选出最大那n-m个,用个数据结构维护一下就好了。
最简单的方法就是优先队列了。
code
#include <queue>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <time.h>
#define N 100003
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(int &n)
{
n=0;
ch=G();
while((ch<'0' || ch>'9') && ch!='-')ch=G();
int w=1;
if(ch=='-')w=-1,ch=G();
while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
n*=w;
}
priority_queue <int> q;
struct node
{
int a,b;
}a[N];
bool cmp(node a,node b)
{
return a.a>b.a;
}
int n,T,m;
long long ans;
int main()
{
freopen("d.in","r",stdin);
freopen("d.out","w",stdout);
for(read(T);T;T--)
{
for(;!q.empty();q.pop());
read(n);read(m);
for(int i=1;i<=n;i++)
read(a[i].a),read(a[i].b);
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n-m;i++)
q.push(-a[i].b);
ans=(long long)a[n-m].a*(-q.top());
for(int i=n-m+1;i<=n;i++)
{
q.push(-a[i].b);
q.pop();
ans=max(ans,(long long)a[i].a*(-q.top()));
}
printf("%lld\n",ans);
}
return 0;
}