P2831 愤怒的小鸟
题解
首先看数据,基本就是状压没跑了。
解法:
先预处理出所有可能的抛物线,对于每一个小猪猪,我们去看最多能连上几个小猪猪。将坐标带进去求解二元一次方程。如果
a
<
0
a<0
a<0,就去拓展最多能打几只小猪。
其次就是状态转移方程,考虑对于打掉
i
i
i只小猪最少需要的小鸟数量。我们用集合
S
S
S代表已经打掉的小猪,
e
q
u
a
t
i
o
n
equation
equation代表预处理出来的抛物线能打掉的小猪集合
d
p
[
S
∣
e
q
u
a
t
i
o
n
]
=
m
i
n
(
d
p
[
S
∣
e
q
u
a
t
i
o
n
]
,
d
p
[
S
]
+
1
)
dp[S|equation] = min(dp[S|equation],dp[S]+1)
dp[S∣equation]=min(dp[S∣equation],dp[S]+1)
至于边界就是打掉
0
0
0只小猪需要
0
0
0只小鸟啦。
代码
#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
struct pig{
double x,y;
}d[20];
int dp[1<<20],equation[200],n,m,cnt;
void love(double &a,double &b,double x1,double x2,double y1,double y2)
{
a = (y2 - y1 * x2 / x1) / (x2 * x2 - x1 * x2);
b = (y1 - a * x1 * x1) / x1;
}
bool more(double a,double b,double x,double y)
{
double ret_y = a * x * x + b * x;
return fabs(ret_y - y) <= eps; //在一定误差范围内即可
}
void prepare()
{
double a,b;
for(int i = 0; i < n; ++i){
equation[cnt++] = (1<<i); //单独打这只小猪
for(int j = i+1, cur = 0; j < n; ++j){
if(cur >> j & 1) continue;
love(a,b,d[i].x,d[j].x,d[i].y,d[j].y); //解出a和b
if(a >= 0) continue;
equation[cnt] = 1<<i;
for(int k = j; k < n; ++k){
if(more(a,b,d[k].x,d[k].y)){ //满足y = a * x^2 + b * x的小猪
cur |= 1<<k;
equation[cnt] |= 1<<k;
}
}
cnt++;
}
}
}
void solve()
{
memset(dp,0x3f,sizeof dp);
cin >> n >> m;
for(int i = 0; i < n; ++i){
cin >> d[i].x >> d[i].y;
}
cnt = 0;
prepare();
dp[0] = 0;
for(int i = 0; i < (1<<n); ++i){
for(int j = 0; j < cnt; ++j){
dp[i|equation[j]] = min(dp[i|equation[j]],dp[i]+1);
}
}
cout<<dp[(1<<n)-1]<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int T;
cin >> T;
while(T--) solve();
return 0;
}