Angry bird-like pressure DP-

Angry bird

Kiana recently indulged in a magical game can not extricate themselves.

In short, this game is made on a plane.

There are a slingshot located at (0, 0), it can emit each Kiana a red bird, birds are the flight path of the form y = ax2 + bx to the first quadrant of the curve, where a, b is Kiana specified parameters, and must satisfy a <0.

When the bird back to the floor (ie, x-axis), it will instantly disappear.

N-green piglets only a hurdle in the first quadrant of the game, the coordinates of which are located i piglets (xi, yi).

If a bird's flight path through the (xi, yi), then the first i piglets will be destroyed, while the birds will continue to fly along the original trajectory;

If a bird's flight path does not go through (xi, yi), then the whole process of the bird's flight would not have been the first pig i only have any effect.

For example, when the two pigs are located at (1, 3), and (3, 3), Kiana be selected to transmit a bird trajectory y = -x2 + 4x, so that it will be only two pigs together to destroy the birds.

The object of the game is to destroy all the pigs by launching birds.

Each level of this amazing game it is very difficult to Kiana, so Kiana also entered some mysterious instructions such that they can more easily complete the game.

These instructions will be detailed in the input format.

Suppose the game a total of T checkpoints, now Kiana want to know, for each level, the number of birds in order to launch at least need to destroy all the pigs.

Because she does not count, so I hope to you to tell her.

Note: This problem except NOIP original data, but also to strengthen the data.

Input format
The first line contains a positive integer T, it represents the total number of points in the game.

This information is sequentially input T checkpoints below.

Each row comprises two first level non-negative integer n, m, respectively, the number of piglets mysterious instruction type and the level of the input Kiana.

The next n-th row, the i-th row comprising two positive real numbers (xi, yi), i represents piglets coordinates (xi, yi), with a small level to ensure that the data does not exist in the coordinates of two identical pig.

If m = 0, a command is input Kiana indicates no effect.

If m = 1, then the level will satisfy: up with ⌈n / 3 + 1⌉ birds to destroy all pigs.

If m = 2, then the level will be met: there must be an optimal solution, which has wiped out at least a bird ⌊n / 3⌋ piglets.

Ensure 1≤n≤18,0≤m≤2,0 <xi, yi <10, average real input in two after the decimal point.

Above, and the symbol ⌈c⌉ ⌊c⌋ c, respectively, a rounding up and rounding down, for example: ⌈2.1⌉ = ⌈2.9⌉ = ⌈3.0⌉ = ⌊3.0⌋ = ⌊3.1⌋ = ⌊3.9⌋ = 3.

Output formats
sequentially output one line answer for each level.

Each line contains a positive integer representing the corresponding level, the number of birds minimum required to eliminate all piglets.

Sample input:
2
2 0
1.00 3.00
3.00 3.00
52
1.00 5.00
2.00 8.00
3.00 9.00
4.00 8.00
5.00 5.00
Output Sample:
. 1
. 1

answer:

Generally parabolic equation: y = a x 2 + b x + c y = a x 2 + b x + c y=ax^2+bx+cy=ax^2+bx+c
title parabola has two characteristics:

Through the origin, i.e., c = 0, c = 0
downwardly open, i.e. a <0, a <0
Thus the parabolic equation: y = a x 2 + b x y = a x 2 + b x y=ax^2+bxy=ax^2+bx , there are two unknowns, thus determined points to a parabola.
So there are at most n2n2 different parabola. The next set of all points obtained all the different points of the parabola, and can cover.
At this point the question becomes classic lines as little as possible "repeat coverage problem," that a given matrix 01, asked to choose, will cover all the columns. Here the standard practice is to use Dancing Links.
However, because this data is relatively small so we like pressing to do, and you can write a lot less code.
We define the path [x] [y] y status parabola through this point is the point x. f [i] representative of our state is the answer i.
So our final answer must be f [(1 << n) -1includes all points)
So how once we encountered a parabola does not contain a point when treatment became our key movements.
If it does not then we must find the point j contains parabola j point of the parabola and can contain more points as possible. So we played a role on the path. Because when we enumerate the state does not contain x, then we must ensure that the next state f ( j p a t h [ x ] [ i ] ) f(j|path[x][i]) that the maximum value. Otherwise we will present the state of +1, the use of a parabola to contain him.

#include<bits/stdc++.h>
using namespace std;
const int N=20;
int f[1<<20];
typedef pair<double, double> PDD;
PDD q[N];
int path[N][N],n,m;
const double eps = 1e-8;
int cmp(double x, double y)
{
    if (fabs(x - y) < eps) return 0;
    if (x < y) return -1;
    return 1;
}
int main()
{
	int t; cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i=0;i<n;i++) cin>>q[i].first>>q[i].second;
		memset(path,0,sizeof path);
		for(int i=0;i<n;i++){
			path[i][i]=1<<i;
			for(int j=0;j<n;j++){
				double x1 = q[i].first, y1 = q[i].second;
                double x2 = q[j].first, y2 = q[j].second;
                if (!cmp(x1, x2)) continue;//垂直向上是不可能的 
                double a = (y1 / x1 - y2 / x2) / (x1 - x2);
                double b = y1 / x1 - a * x1;
                if(cmp(a,0)>=0) continue; //保证斜率为负
                int state=0;
                for(int k=0;k<n;k++){
                	double x=q[k].first,y=q[k].second;
                	if(!cmp(a*x*x+b*x,y)){
                		state+=1<<k;
					}
				}
				path[i][j]=state;
			}
		}
		memset(f,0x3f,sizeof f);
		f[0]=0;
		for(int i=0;i+1<1<<n;i++){
			int x=0;
			for(int j=0;j<n;j++){
				if(!(i&1<<j)){
					x=j;
					break;
				}
			}
			for(int j=0;j<n;j++){
				f[i|path[x][j]]=min(f[i|path[x][j]],f[i]+1);
			}
		}
		cout<<f[(1<<n)-1]<<endl;
	}
}
发布了92 篇原创文章 · 获赞 6 · 访问量 1174

Guess you like

Origin blog.csdn.net/weixin_42979819/article/details/103915893