大致题意:
给出平面上n个向量,对于每个向量可以选择正的V或负的-V,求按照选择的向量走完,最后距离原点<=1.5*1e6的一个选择方案
非正解!!!!!!!!!!
先按距离原点距离由远到近贪心,贪完后答案若不满足,则进行一次dfs找出正确地路线即可(数据较水)
也可以每次每次讲向量随机排序然后贪心,直到贪到正确答案为止。
#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<stack> #include<time.h> #include<cstdlib> #include<cmath> #include<list> using namespace std; #define MAXN 100100 #define eps 1e-9 #define For(i,a,b) for(int i=a;i<=b;i++) #define Fore(i,a,b) for(int i=a;i>=b;i--) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define mkp make_pair #define pb push_back #define cr clear() #define sz size() #define met(a,b) memset(a,b,sizeof(a)) #define iossy ios::sync_with_stdio(false) #define fre freopen #define pi acos(-1.0) #define inf 1e6+7 #define Vector Point const int Mod=1e9+7; typedef unsigned long long ull; typedef long long ll; int dcmp(double x){ if(fabs(x)<=eps) return 0; return x<0?-1:1; } struct Point{ double x,y; int id; Point(double x=0,double y=0):x(x),y(y) {} bool operator < (const Point &a)const{ if(x==a.x) return y<a.y; return x<a.x; } Point operator - (const Point &a)const{ return Point(x-a.x,y-a.y); } Point operator + (const Point &a)const{ return Point(x+a.x,y+a.y); } Point operator * (const double &a)const{ return Point(x*a,y*a); } Point operator / (const double &a)const{ return Point(x/a,y/a); } void read(){ scanf("%lf%lf",&x,&y); } void out(){ cout<<"debug: "<<x<<" "<<y<<endl; } bool operator == (const Point &a)const{ return dcmp(x-a.x)==0 && dcmp(y-a.y)==0; } }; double Dot(Vector a,Vector b) { return a.x*b.x+a.y*b.y; } double dis(Vector a) { return sqrt(Dot(a,a)); } double Cross(Point a,Point b){ return a.x*b.y-a.y*b.x; } int Maxx=1500000; bool cmp(Point a,Point b){ return dcmp(dis(a)-dis(b))>0; } bool check(Point o,Point p1){ return dcmp(dis(o+p1)-dis(o-p1))<=0; } int ans[100005]; Point p[100005]; int n,mk; void dfs(int now,Point o){ if(mk) return ; if(now==n) { if(dcmp(dis(o)-Maxx)>0) return ; mk=1; For(i,0,n-1) { if(i) printf(" "); printf("%d",ans[i]); } cout<<endl; return ; } ans[p[now].id]=1; dfs(now+1,o+p[now]); ans[p[now].id]=-1; dfs(now+1,o-p[now]); } void solve(){ cin>>n; For(i,0,n-1) p[i].read(),p[i].id=i; mk=0; sort(p,p+n,cmp); Point o(0,0); For(i,0,n-1){ if(check(o,p[i])) ans[p[i].id]=1,o=o+p[i]; else ans[p[i].id]=-1,o=o-p[i]; } if(dcmp(dis(o)-Maxx)<=0) { For(i,0,n-1){ if(i) printf(" "); printf("%d",ans[i]); } cout<<endl; return ; } dfs(0,Point(0,0)); } int main(){ // fre("in.txt","r",stdin); int t=0; solve(); return 0; }