【Codeforces Round 330 (Div 2)E】【贪心 暴力】Edo and Magnets 给定矩形最多去除m个,最小面积矩形使得覆盖所有小矩形重心

84 篇文章 5 订阅
55 篇文章 0 订阅
Edo and Magnets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Edo has got a collection of n refrigerator magnets!

He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be positive integers.

Edo figured out how he wants to place the magnets on the refrigerator. He introduced a system of coordinates on the plane, where each magnet is represented as a rectangle with sides parallel to the coordinate axes.

Now he wants to remove no more than k magnets (he may choose to keep all of them) and attach all remaining magnets to the refrigerator door, and the area of ​​the door should be as small as possible. A magnet is considered to be attached to the refrigerator door if its center lies on the door or on its boundary. The relative positions of all the remaining magnets must correspond to the plan.

Let us explain the last two sentences. Let's suppose we want to hang two magnets on the refrigerator. If the magnet in the plan has coordinates of the lower left corner (x1y1) and the upper right corner (x2y2), then its center is located at () (may not be integers). By saying the relative position should correspond to the plan we mean that the only available operation is translation, i.e. the vector connecting the centers of two magnets in the original plan, must be equal to the vector connecting the centers of these two magnets on the refrigerator.

The sides of the refrigerator door must also be parallel to coordinate axes.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100 0000 ≤ k ≤ min(10, n - 1)) — the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator.

Next n lines describe the initial plan of placing magnets. Each line contains four integers x1, y1, x2, y2 (1 ≤ x1 < x2 ≤ 109,1 ≤ y1 < y2 ≤ 109) — the coordinates of the lower left and upper right corners of the current magnet. The magnets can partially overlap or even fully coincide.

Output

Print a single integer — the minimum area of the door of refrigerator, which can be used to place at least n - k magnets, preserving the relative positions.

Sample test(s)
input
3 1
1 1 2 2
2 2 3 3
3 3 4 4
output
1
input
4 1
1 1 2 2
1 9 2 10
9 9 10 10
9 1 10 2
output
64
input
3 0
1 1 2 2
1 1 1000000000 1000000000
1 3 8 12
output
249999999000000001
Note

In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly.

In the second test sample it doesn't matter which magnet to remove, the answer will not change — we need a fridge with door width 8 and door height 8.

In the third sample you cannot remove anything as k = 0.

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=1e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n,m;
struct A
{
	int v;
	int p;
	bool operator < (const A&b)const 
	{
		if(v!=b.v)return v<b.v;
		else return p<b.p;
	}
}x[N],y[N];
int e[N];
int main()
{
	int x1,y1,x2,y2;
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;++i)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			x[i].v=(x2+x1);x[i].p=i;
			y[i].v=(y1+y2);y[i].p=i;
		}
		sort(x+1,x+n+1);
		sort(y+1,y+n+1);
		LL ans=4e18;
		int num=0;
		for(int bot=1;;++bot)
		{
			for(int top=n;;--top)
			{
				for(int lft=1;;++lft)
				{
					for(int rgt=n;;--rgt)
					{
						LL a=max((y[top].v-y[bot].v),2);if(a&1)++a;
						LL b=max((x[rgt].v-x[lft].v),2);if(b&1)++b;
						gmin(ans,a*b);
						if(++e[x[rgt].p]==1)++num;
						if(num>m){while(rgt<=n)if(--e[x[rgt++].p]==0)--num;break;}
					}
					if(++e[x[lft].p]==1)++num;
					if(num>m){while(lft>=1)if(--e[x[lft--].p]==0)--num;break;}
				}
				if(++e[y[top].p]==1)++num;
				if(num>m){while(top<=n)if(--e[y[top++].p]==0)--num;break;}
			}
			if(++e[y[bot].p]==1)++num;
			if(num>m){while(bot>=1)if(--e[y[bot--].p]==0)--num;break;}
		}
		printf("%lld\n",ans/4);
	}
	return 0;
}
/*
【trick&&吐槽】
the refrigerator door must be rectangle,
and both the length and the width of the door must be positive integers.
没看题导致wa一发呀>_<,读题读题好重要!

1,用for循环实现类递归的搜索形式的话,还是在当步实现回溯比较方便清晰。
2,哪怕是e[x--]--这样的操作,都是在初始操作完成之后再进行两个--操作的。

【题意】
给你n(1<=n<=1e5)个矩形,以左下角和右上角的形式给出,每个矩形的坐标给在[1,1e9]范围。
我们想要最多去除k个矩形,(0<=k<=min(n-1,10))。
使得我们可以使用一个面积尽可能小的大矩形,包含剩下所有矩形的重心。
让你输出这个大矩形的面积(大矩形的长和宽都必须为正整数)

【类型】
贪心

【分析】
我们发现,很显然,我们有意义的对于矩形的去除,是最上,最下,最左,最右的矩形。
于是我们对所有矩形的重心排序。放到横坐标排一次,再放到纵坐标排一次。
接下来枚举上下左右各去除了多少个矩形,如果满足限制条件,则更新答案。这道题就做完啦!

然而还有一点,就是重心的横纵坐标是可能为.5的,这个会带来精度上的误差和困扰。
为了解决这个问题,我们把重心的坐标扩大2倍。然后再贪。
但是要保证所有数都是偶数,这样才能有实际的长和宽都为整数。
这样这道题就做完啦!

【时间复杂度&&优化】
O(nlogn+k^4)

【数据】
2 0
1 1 2 2
3 3 5 5
重心分别为——
(1.5,1.5)
(4,4)
那么矩形的面积就是3*3=9
然而5*5/4的答案却只有6.
所以可见审题还是非常重要呀!虽然这题不知道怎么这样的错误给放过去了2333

*/



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值