CF529B Group Photo 2 (online mirror version)

日常不搞清楚题意乱写 WA。

看值域这么小,考虑枚举最大高度 (maxh)

  • (h_i>maxh)(w_i>maxh),不合法。
  • (h_i>maxh)(w_ileq maxh),必须换。
  • (h_ileq maxh)(w_i>maxh),不能换。
  • (h_ileq maxh)(w_ileq maxh),可换可不换,按 (w_i-h_i) 从大到小贪心选择。

时间复杂度 (O(n(h+log n)))

code:

#include<bits/stdc++.h>
using namespace std;
#define Min(x,y)((x)<(y)?x:y)
#define For(i,x,y)for(i=x;i<=(y);i++)
struct people
{
	int w,h;
}a[1005];
int mn=1000000000,n;
inline bool cmp(people _,people __)
{
	return _.w-_.h>__.w-__.h;
}
void pd(int mx)
{
	int i,sum,tot;
	sum=tot=0;
	For(i,1,n)
	if(a[i].w>mx&&a[i].h>mx)return;
	else if(a[i].h>mx)tot++;
	if(tot>n>>1)return;
	For(i,1,n)
	if(a[i].h>mx)sum+=a[i].h;
	else if(a[i].h<a[i].w&&a[i].w<=mx&&tot<n>>1)tot++,sum+=a[i].h;
	else sum+=a[i].w;
	mn=Min(mn,sum*mx);
}
int main()
{
	int i;
	scanf("%d",&n);
	For(i,1,n)scanf("%d%d",&a[i].w,&a[i].h);
	sort(a+1,a+n+1,cmp);
	For(i,1,1000)pd(i);
	cout<<mn;
	return 0;
}
原文地址:https://www.cnblogs.com/May-2nd/p/14370786.html