[USACO Mar10]星牛爭霸StarCowraft解題報告

題目


分析

首先可以發現,我們能夠把三種單位的戰鬥力同時乘以一個數而不改變結果。因此,不妨設第三種單位的戰鬥力S3=1.出於方便,不妨記x=S1,y=S2

對於一場比賽,我們能夠寫出一個類似這樣的式子:

j1x+j2y+j3<=b1x+b2y+b3(當然也有可能是>=)

整理後得到一個類似這樣的式子:

ax+by<=c

後面的朋友們,大聲告訴我這叫什麼?

沒錯,半平面!

所以做法就很簡單了:對所有測試局的半平面求交,其交集(或曰‘核’)是一個凸多邊形。將其所有頂點求出。在試圖判斷一次新的戰鬥時,顯然該場戰鬥對應於一條直線,如果凸多邊形的所有頂點均在直線一側,那麼該戰鬥的結果是確定的,否則不定。

注意,那個“單位之差不超過100”倍的條件也是要寫出來的。

代碼

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<deque>
#include<vector>
using namespace std;
const double eps=1e-10,pi=acos(-1.0),INF=1e10;
const int SIZE=2010;
class Point{
public:
	double x,y;
};
void print(const Point &p){
	cout<<"("<<p.x<<" "<<p.y<<")";
}
double cross(const Point &a,const Point &b){
	return a.x*b.y-b.x*a.y;
}
class H_Plane{//那啥啥的game,不對plane
public:
	double a,b,c;//ax+by<=c
	double pa;//極角
	void print(void)const{
		cout<<"極角:"<<pa<<" 不等式:"<<a<<"x+"<<b<<"y<="<<c;
		cout<<" 方程:y="<<-a/b<<"x+"<<c/b<<endl;
	}
	void assign(char winner,double j1,double j2,double j3,double b1,double b2,double b3){
		a=j1-b1,b=j2-b2,c=b3-j3;//這是按Bessie贏算的
		if(winner=='J') a*=-1,b*=-1,c*=-1;
		pa=atan2(-a,b);
		if(pa<0) pa+=2*pi;
	}
};
void print(const H_Plane &h){
	h.print();
}
bool cmp(const H_Plane &a,const H_Plane &b){
	return a.pa<b.pa;
}
bool inside(const Point &p,const H_Plane &h){
	return h.a*p.x+h.b*p.y<h.c-eps;
}
Point inter(const H_Plane &s,const H_Plane &t){//要求s,t極角不同
	Point ans;
	ans.x=(s.b*t.c-s.c*t.b)/(s.b*t.a-s.a*t.b);
	ans.y=(s.a*t.c-s.c*t.a)/(s.a*t.b-s.b*t.a);
	return ans;
}
void HPL_intersection(H_Plane h[],int n,deque<H_Plane> &Q){
	//要求此時已經計算出所有半平面的極角,存於h[1..n]
	//Q中返回約束了核的那些半平面
	sort(h+1,h+1+n,cmp);
	int tot=0;
	for(int i=1;i<=n;i++){//多個約束取最緊者
		if(tot==0||fabs(h[tot].pa-h[i].pa)>eps) h[++tot]=h[i];
		else if(h[i].c<h[tot].c) h[tot]=h[i];
	}
	n=tot;
	Q.clear();
	for(int i=1;i<=n;i++){
		while(Q.size()>=2&&!inside(inter(Q.back(),Q[Q.size()-2]),h[i])) Q.pop_back();
		while(Q.size()>=2&&!inside(inter(Q[0],Q[1]),h[i])) Q.pop_front();
		Q.push_back(h[i]);
	}
	while(Q.size()>=2&&!inside(inter(Q.back(),Q[Q.size()-2]),Q[0])) Q.pop_back();
}
int check(const vector<Point> &v,const H_Plane &h){
	double mx=-INF,mn=INF;
	for(int i=0;i<v.size();i++){
		double now=h.a*v[i].x+h.b*v[i].y;
		mx=max(mx,now);
		mn=min(mn,now);
	}
	if(mx<h.c-eps) return 1;//符合h的判斷
	else if(mn>h.c+eps) return -1;//不符合h的判斷
	else return 0;
}
int N,M;
H_Plane test_battle[SIZE];
deque<H_Plane> core;
vector<Point> core_points;
void work(void){
	HPL_intersection(test_battle,N,core);
	for(int i=0;i<core.size()-1;i++){
		core_points.push_back(inter(core[i],core[i+1]));
	}
	core_points.push_back(inter(core[0],core.back()));
	int j1,j2,j3,b1,b2,b3;
	H_Plane h;
	for(int i=1;i<=M;i++){
		scanf("%d%d%d%d%d%d\n",&j1,&j2,&j3,&b1,&b2,&b3);
		h.assign('B',j1,j2,j3,b1,b2,b3);
		int flag=check(core_points,h);
		if(flag==1) printf("B\n");
		else if(flag==-1) printf("J\n");
		else printf("U\n");
	}
}
void read(void){
	scanf("%d%d\n",&N,&M);
	char c;
	int j1,j2,j3,b1,b2,b3;
	for(int i=1;i<=N;i++){
		scanf("%c%d%d%d%d%d%d\n",&c,&j1,&j2,&j3,&b1,&b2,&b3);
		test_battle[i].assign(c,j1,j2,j3,b1,b2,b3);
	}
	test_battle[++N].assign('B',1,0,0,0,0,100);
	test_battle[++N].assign('J',100,0,0,0,0,1);
	test_battle[++N].assign('B',0,1,0,0,0,100);
	test_battle[++N].assign('J',0,100,0,0,0,1);
	test_battle[++N].assign('B',1,0,0,0,100,0);
	test_battle[++N].assign('J',100,0,0,0,1,0);
}
int main(){
	freopen("starc.in","r",stdin);
	freopen("starc.out","w",stdout);
	read();
	work();
	return 0;
}


發佈了53 篇原創文章 · 獲贊 17 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章