题目描述
给定dn组数据, 数据包括w, h, 给定a, b, c;
求一个子集, 子集里面所有的元素都满足A*(H-h) + B*(W-w) <= C
w为子集里面最小的w , h表示子集里面最小的h
样例
Sample Input
8
1 2 4
5 1
3 2
2 3
2 1
7 2
6 4
5 1
4 3
Sample Output
5
思路
- 题目上的要求, 可以将题目转换用一个直角三角形,去圈点, 求圈得到的最多点的数量
- 将数据按照h从大到小排序, 可以得到三角形的斜边是从上往下平移的,我们就按照这样一个顺序去遍历数组, 用队列保存满足要求的所有答案
- 每组数据,保存ah+bw, 用于比较这组数据与当前确定的三角形是否满足要求, 将题目要求化简后合用得到 要求为a * h+b * w <= mh * a + mw * b + c
- 用优先级队列, 队头保存最大的 a * h + b * w 这样每次就将更新了m后的不满足的答案弹出去
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1010;
struct node
{
int w, h, s;
bool operator <(const node &a) const {
return s < a.s;
}
};
node t[N];
int a, b, c, n;
bool cmp(node a, node b)
{
return a.h > b.h;
}
int main()
{
while(~scanf("%d%d%d%d", &n, &a, &b, &c))
{
for(int i = 0; i < n; i++)
{
scanf("%d%d", &t[i].h, &t[i].w);
t[i].s = a * t[i].h + b * t[i].w;
}
sort(t, t + n, cmp);
priority_queue<node> q;
int ans = 0;
for(int i = 0; i < n; i++)
{
int mh = t[i].h;
int mw = t[i].w;
while(q.size())
q.pop();
for(int j = 0; j < n; j++)
{
if(t[i].s > mh * a + mw * b + c)
break;
mh = min(mh, t[j].h);
if(t[j].s <= mh * a + mw * b + c)
{
if(t[j].w >= mw)
q.push(t[j]);
while(q.size() && q.top().s > mh * a + mw * b + c)
q.pop();
}
ans = max(ans, (int)q.size());
}
}
printf("%d\n", ans);
}
return 0;
}