NOIP2016提高组复赛C 愤怒的小鸟

时间:2025-01-11 11:03:38

题目链接:http://uoj.ac/problem/265

题目大意:

  太长了不想概括。。。

分析:

  状压DP的模板题,把所有可能的抛物线用二进制表示,然后暴力枚举所有组合,详情见代码内注释

代码如下:

 #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std; #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define Rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define rFor(i,t,s) for (int i = (t); i >= (s); --i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define pii pair<int,int>
#define piii pair<pair<int,int>,int>
#define MP make_pair
#define PB push_back
#define ft first
#define sd second template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef unsigned long long uLL;
typedef pair< double, double > PDD;
typedef set< int > SI;
typedef vector< int > VI;
const double EPS = 1e-;
const int inf = 1e9 + ;
const LL mod = 1e9 + ;
const int maxN = 1e5 + ;
const LL ONE = ; int sgn(double x) {
if(fabs(x) < EPS) return ;
return x > ? : -;
} struct Matrix{
double m[][]; Matrix(){}
Matrix(double x11, double x12, double x21, double x22) {
m[][] = x11;
m[][] = x12;
m[][] = x21;
m[][] = x22;
} double det() {
return m[][] * m[][] - m[][] * m[][];
}
}; int T, n, m, ans;
PDD pig[];
// 用n位二进制数记录一条抛物线可能穿过的点的状态
// 比如抛物线过1号,5号,7号点,那么数值为 :000000000001010001
VI state;
SI si;
// f[i]为当前状态的最小步数
int f[ << ]; int bitcount32(int bits) {
bits = (bits & 0x55555555) + (bits >> & 0x55555555);
bits = (bits & 0x33333333) + (bits >> & 0x33333333);
bits = (bits & 0x0f0f0f0f) + (bits >> & 0x0f0f0f0f);
bits = (bits & 0x00ff00ff) + (bits >> & 0x00ff00ff);
return (bits & 0x0000ffff) + (bits >> & 0x0000ffff);
} // 通过2个点算抛物线参数[a, b]
bool calcAB(PDD x, PDD y, PDD &para) {
Matrix D = Matrix(x.ft * x.ft, x.ft, y.ft * y.ft, y.ft);
if(sgn(D.det()) == ) return false;
Matrix D1 = Matrix(x.sd, x.ft, y.sd, y.ft);
Matrix D2 = Matrix(x.ft * x.ft, x.sd, y.ft * y.ft, y.sd); para.ft = D1.det() / D.det();
if(sgn(para.ft) >= ) return false;
para.sd = D2.det() / D.det();
return true;
} // 验证点x是否符合抛物线
bool check(PDD x, PDD &para) {
if(sgn(para.ft * x.ft * x.ft + para.sd * x.ft - x.sd) == ) return true;
return false;
} void solve() {
int ret = ; // 枚举所有点对
For(i, , n) {
For(j, i + , n) {
PDD p;
if(!calcAB(pig[i], pig[j], p)) continue;
// 看是否有其他点也经过这条抛物线
int st = ;
st |= << (i - );
st |= << (j - );
For(k, , n) {
if(k == i || k == j) continue;
if(check(pig[k], p)) st |= << (k - );
}
if(si.find(st) == si.end()) {
si.insert(st);
state.PB(st);
}
}
}
// 把只过一个点的抛物线也存一下
Rep(i, n) state.PB( << i);
// 把所有抛物线都得到后,问题就变成在这些抛物线中最少能选取几条,进行或运算后二进制1~n位全为1
msI(f);
f[] = ;
int len = state.size(); Rep(i, << n) {
// 当枚举到f[i]时,f[i]已经是最优解了
Rep(j, len) {
f[i | state[j]] = min(f[i | state[j]], f[i] + );
}
} ans = f[( << n) - ];
} int main(){
INIT();
cin >> T;
while(T--) {
state.clear();
si.clear();
cin >> n >> m;
For(i, , n) cin >> pig[i];
solve(); cout << ans << endl;
} return ;
}