本文主要是介绍hdu 2907 凹陷面,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
给定一个多边形,让你求出其价值。价值的定义是:-p*凹面的个数+q*凸面的个数。。。最小值为0凸面的个数就是凸包中的点的个数,但是当出现凹面时,就会减少一个凸面,这是因为这时候的凸面是虚拟出来的!!!!!
题意参考这里 点击打开链接
int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;
}struct point{double x , y ;int id ;point(){}point(double _x , double _y):x(_x) , y(_y){}friend bool operator == (const point &a , const point &b){return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0 ;}friend double operator ^ (const point &a , const point &b){return a.x * b.y - a.y * b.x ;}friend double dist(const point &a , const point &b){return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) ;}point operator - (point o){return point(x - o.x , y - o.y) ;}
};bool cmpless(const point &a , const point &b){return cmp(a.x - b.x) < 0|| cmp(a.x - b.x == 0) && cmp(a.y - b.y) < 0 ;
}vector<point> convex_hull(vector<point> a){vector<point> src(2 * a.size() + 5) ;sort(a.begin() , a.end() , cmpless) ;a.erase(unique(a.begin() , a.end()) , a.end()) ;int m = 0 ;for(int i = 0 ; i < a.size() ; i++){while(m > 1 && cmp( (src[m-1] - src[m-2]) ^ (a[i] - src[m-2]) ) <= 0)m-- ;src[m++] = a[i] ;}int k = m ;for(int i = a.size() - 2 ; i >= 0 ; i--){while(m > k && cmp( (src[m-1] - src[m-2]) ^ (a[i] - src[m-2])) <= 0)m-- ;src[m++] = a[i] ;}src.resize(m) ;if(a.size() > 1) src.resize(m-1) ;return src ;
}int vis[38] ;int main(){int n , i , t , pcnt , qcnt , p , q ;cin>>t ;while(t--){cin>>p>>q>>n ;vector<point> lis(n) ;for(i = 0 ; i < n ; i++){scanf("%lf%lf" , &lis[i].x , &lis[i].y) ;lis[i].id = i ;}vector<point> hull = convex_hull(lis) ;qcnt = hull.size() ;memset(vis , 0 , sizeof(vis)) ;for(i = 0 ; i < hull.size() ; i++) vis[hull[i].id] = 1 ;pcnt = 0 ;for(i = 0 ; i < n ; i++){if(vis[i] && !vis[(i+1)%n]) pcnt++ ;}qcnt -= pcnt ;cout<< max(0 , q * qcnt - p * pcnt )<< endl ;}return 0 ;
}
这篇关于hdu 2907 凹陷面的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!