【codechef】Chef and Polygons(灵活题,坑题)


Input:
1
3
6
-2 2 -1 1 2 2 2 -1 1 -2 -2 -2
3
-1 -1 1 -1 1 1
4
3 3 -3 3 -3 -3 3 -3
Output:
1 0 2

Explanation

In the picture the first polygon is marked in green, second - in red and third in blue color.

explanation

http://www.codechef.com/JUNE15/problems/CHPLGNS

对于这题我只能说,我想得实在是太复杂了……

第一眼是马上去找 一个多边形是否在另一个多边形内 的模板,然后没找到;后来又尝试用面积相等的方法求,但是忽略了它可以是凹多边形;过了n天突然想到可以转化成只要求多边形任意一点是否在另一个多边形内,然后提交,10分。

【任意两个多边形都是一个包含另一个】这句话非常重要。仔细分析,每个多边形横坐标最靠左的点的横坐标,肯定是最外面的多边形在最左边,然后依次。

要仔细分析题目里面的细节,抓住可以投机取巧的点。

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
pair<int,int> x[100001];
int cmp(pair<int,int> a,pair<int,int> b){
	return a.second<b.second;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,m,a,b,min;
		scanf("%d",&n);
		for(int i=0;i<n;++i){
			x[i].second=i;
			scanf("%d",&m);
			min=1000000001;
			for(int j=0;j<m;++j){
				scanf("%d %d",&a,&b);
				if(a<min)
					min=a;
			}
			x[i].first=min;
		} 
		sort(x,x+n);
		for(int i=0;i<n;++i)
			x[i].first=n-i-1;
		sort(x,x+n,cmp);
		for(int i=0;i<n;++i)
			printf("%d ",x[i].first);
		printf("\n");
	}
    return 0;
}
再顺便附上求一个点是否在多边形内的模板吧:

int pnpoly (int nvert, float *vertx, float *verty, float testx, float testy) {

    int i, j, c = 0;

    for (i = 0, j = nvert-1; i < nvert; j = i++) {

        if ( ( (verty[i]>testy) != (verty[j]>testy) ) &&

(testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )

            c = !c;

    }

    return c;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值