POJ - 2008 Moo University - Team Tryouts

題目描述

給定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;
} 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章