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.
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;
}