P2831 愤怒的小鸟
作者:互联网
P2831 愤怒的小鸟
神仙状压dp
首先解一个非常休闲的二元一次方程组
接下来对于每两只鸟,我们计算出它们构成的二次函数(需判断合不合法)
接下来
记录函数上有几个点
然后开始愉快地dp
代码:
#include<bits/stdc++.h> using namespace std; typedef long double ld; const int N=19; int t; int n,ppppp; ld a[N],b[N]; bool clean[1<<N]={0}; bool yuy[1<<N]={0}; int f[1<<N]; int qu[1<<N]={0}; ld lpabs(ld x){ if(x<0.0000001) return -x; return x; } bool cpair(ld x,ld y){ if(lpabs(x-y)<0.0000001) return 1; return 0; } ld calx(ld x1,ld y1,ld x2,ld y2){ return (y1*x2-x1*y2)/(x1*x1*x2-x1*x2*x2); } ld caly(ld x1,ld y1,ld x2,ld y2){ return (x1*x1*y2-y1*x2*x2)/(x1*x1*x2-x1*x2*x2); } bool is_on(ld a,ld b,ld x,ld y){ if(cpair(a*x*x+b*x,y)) return 1; return 0; } int main(){ memset(f,127,sizeof(f)); scanf("%d",&t); while(t--){ memset(clean,0,sizeof(clean)); memset(yuy,0,sizeof(yuy)); memset(f,127,sizeof(f)); memset(qu,0,sizeof(qu)); scanf("%d%d",&n,&ppppp); for(int i=1;i<=n;i++){ cin>>a[i]>>b[i]; } for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j) continue; if(cpair(a[i],a[j])) continue; ld dx,dy; dx=calx(a[i],b[i],a[j],b[j]); dy=caly(a[i],b[i],a[j],b[j]); //if(cpair(a[i]*a[i]*a[j]-a[i]*a[j]*a[j],0)||cpair(a[i]*a[i]*a[j]-a[i]*a[j]*a[j],0)) continue; if(dx>=0) continue; int cnt=0; int state=0; for(int k=1;k<=n;k++){ if(is_on(dx,dy,a[k],b[k])){ state+=(1<<(k-1)); } } for(int k=1;k<=n;k++){ if(is_on(dx,dy,a[k],b[k])){ for(int l=1;l<=n;l++){ if(is_on(dx,dy,a[l],b[l])){ // cout<<k<<" -> "<<l<<endl; clean[(1<<(k-1))|(1<<(l-1))]=1; qu[(1<<(k-1))|(1<<(l-1))]=state; } } } } } } f[0]=0; for(int i=1;i<=n;i++) yuy[(1<<(i-1))]=1; for(int i=1;i<=n;i++) f[1<<(i-1)]=1; for(int i=0;i<=(1<<n);i++){ for(int j=1;j<=n;j++){ if((i&(1<<(j-1)))==0){ for(int k=1;k<=n;k++){ if(j==k) continue; if((i&(1<<(k-1)))==0){ // cout<<qu[(1<<(j-1))|(1<<(k-1))]<<endl; f[i|qu[(1<<(j-1))|(1<<(k-1))]]=min(f[i|qu[(1<<(j-1))|(1<<(k-1))]],f[i]+1); } } } } for(int j=1;j<=n;j++){ if((i&(1<<(j-1)))==0){ f[i|(1<<(j-1))]=min(f[i|(1<<(j-1))],f[i]+1); } } } // for(int i=1;i<=(1<<n)-1;i++){ // cout<<f[i]<<" :::"<<endl; // } cout<<f[(1<<n)-1]<<endl; } return 0; }
标签:ld,int,P2831,小鸟,愤怒,dp 来源: https://www.cnblogs.com/QYJ060604/p/11828242.html