poj 2008 通过排序把O(n^3)变成O(n^2)

题意:给你n只小牛的身高H和体重W(1<=n<=1000),给你三个整数A,B,C,满足A*(H-h) + B*(W-w) <= C的一堆小牛可以组成一个队伍,h和w分别是这个队伍中最小的身高和最小的体重(不一定是同一头牛),问你这个队伍最多多少人

输入
8 //n
1 2 4 //A B C
5 1 //每只小牛的身高体重
3 2
2 3
2 1
7 2
6 4
5 1
4 3

输出
5 //表示1,2,3,4,7这5只小牛一队

思路
在这里插入图片描述
在这里插入图片描述

代码

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <stack>

#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define ull unsigned long long
#define uint unsigned int

using namespace std;

#define maxn 1005

struct Point {
	int w, h, k;
}a[maxn],*sh[maxn],*sk[maxn];

int n, cw, ch, A, B, C;
int ans, cnt, hnum, knum;

bool cmph(Point *a, Point *b) {
	return a->h > b->h;
}

bool cmpk(Point *a, Point *b) {
	return a->k > b->k;
}

int cal(int h, int w) {
	for (; hnum < n&&sh[hnum]->h >= h; hnum++)
		if (sh[hnum]->w >= w && sh[hnum]->w <= w + cw)
			cnt++;
	
	int k = A * h + B * w + C;

	for (; knum<n&&sk[knum]->k>k; knum++)
		if (sk[knum]->w >= w && sk[knum]->w <= w + cw)
			cnt--;

	return cnt;
}

void work(int x) {
	cnt = hnum = knum = 0;
	int w = sh[x]->w;
	int h = sh[x]->h;

	for (int i = x; i < n&&sh[i]->h >= h - ch; i++)
		if (sh[i]->w >= w && sh[i]->w <= w + cw)
			ans = max(ans, cal(sh[i]->h, w));
}

int main() {
	scanf("%d", &n);
	scanf("%d%d%d", &A, &B, &C);
	ch = C / A;
	cw = C / B;

	for (int i = 0; i < n; i++) {
		scanf("%d%d", &a[i].h, &a[i].w);
		a[i].k = A * a[i].h + B * a[i].w;
		sh[i] = &a[i];
		sk[i] = &a[i];
	}
	sort(sh, sh + n, cmph);
	sort(sk, sk + n, cmpk);
	ans = 0;
	for (int i = 0; i < n; i++)
		work(i);
	printf("%d\n", ans);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值