日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

[NOIP2016]愤怒的小鸟(状压DP)

發(fā)布時(shí)間:2023/12/3 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [NOIP2016]愤怒的小鸟(状压DP) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

[NOIP2016]憤怒的小鳥(niǎo)(狀壓DP)

題目描述

輸入輸出格式

輸入格式:

第一行包含一個(gè)正整數(shù)?T,表示游戲的關(guān)卡總數(shù)。

下面依次輸入這?T個(gè)關(guān)卡的信息。每個(gè)關(guān)卡第一行包含兩個(gè)非負(fù)整數(shù)?n,m,分別表示該關(guān)卡中的小豬數(shù)量和 Kiana 輸入的神秘指令類(lèi)型。接下來(lái)的?n行中,第?ii?行包含兩個(gè)正實(shí)數(shù)?,表示第?i只小豬坐標(biāo)為。數(shù)據(jù)保證同一個(gè)關(guān)卡中不存在兩只坐標(biāo)完全相同的小豬。

如果?m=0,表示Kiana輸入了一個(gè)沒(méi)有任何作用的指令。

如果?m=1,則這個(gè)關(guān)卡將會(huì)滿(mǎn)足:至多用 只小鳥(niǎo)即可消滅所有小豬。

如果?m=2,則這個(gè)關(guān)卡將會(huì)滿(mǎn)足:一定存在一種最優(yōu)解,其中有一只小鳥(niǎo)消滅了至少只小豬。

保證

輸出格式:

對(duì)每個(gè)關(guān)卡依次輸出一行答案。

輸出的每一行包含一個(gè)正整數(shù),表示相應(yīng)的關(guān)卡中,消滅所有小豬最少需要的小鳥(niǎo)數(shù)量。

輸入輸出樣例

輸入樣例#1:?

2 2 0 1.00 3.00 3.00 3.00 5 2 1.00 5.00 2.00 8.00 3.00 9.00 4.00 8.00 5.00 5.00

輸出樣例#1:?

1 1

輸入樣例#2:?

3 2 0 1.41 2.00 1.73 3.00 3 0 1.11 1.41 2.34 1.79 2.98 1.49 5 0 2.72 2.72 2.72 3.14 3.14 2.72 3.14 3.14 5.00 5.00

輸出樣例#2:

2 2 3

輸入樣例#3:?復(fù)制

1 10 0 7.16 6.28 2.02 0.38 8.33 7.78 7.68 2.09 7.46 7.86 5.77 7.44 8.24 6.72 4.42 5.11 5.42 7.79 8.15 4.99

輸出樣例#3:?

6

說(shuō)明

【樣例解釋1】

這組數(shù)據(jù)中一共有兩個(gè)關(guān)卡。

第一個(gè)關(guān)卡與【問(wèn)題描述】中的情形相同,22只小豬分別位于(1.00,3.00)(1.00,3.00)和?(3.00,3.00)(3.00,3.00),只需發(fā)射一只飛行軌跡為y = -x^2 + 4xy=?x2+4x的小鳥(niǎo)即可消滅它們。

第二個(gè)關(guān)卡中有55只小豬,但經(jīng)過(guò)觀察我們可以發(fā)現(xiàn)它們的坐標(biāo)都在拋物線?y = -x^2 + 6xy=?x2+6x上,故Kiana只需要發(fā)射一只小鳥(niǎo)即可消滅所有小豬。

【數(shù)據(jù)范圍】

?

Solution

(我們知道三點(diǎn)確定一個(gè)拋物線,此處的n<=18)

枚舉兩頭豬,設(shè)坐標(biāo)為,以作拋物線,求出拋物線上有哪些豬,記為:

于是?

這顯然是一個(gè)的做法,似乎不太優(yōu)秀。

事實(shí)上,這個(gè)DP有可優(yōu)化之處:每一次枚舉的拋物線必須包含未消滅的第一頭豬!

Because枚舉的拋物線的順序與最優(yōu)解無(wú)關(guān),并且未消滅的豬在之后的拋物線中必須被覆蓋到至少一次,那么與其先覆蓋其他的豬,不如直接先覆蓋第一頭未消滅的豬,這對(duì)答案是沒(méi)有影響的。因此,每一次轉(zhuǎn)移只需要枚舉能覆蓋第一頭未消滅的豬的拋物線即可。

改變了轉(zhuǎn)移之后,時(shí)間復(fù)雜度變?yōu)?#xff0c;輕松AC。

#include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <cassert> #include <string.h> //#include <unordered_set> //#include <unordered_map>#define MP(A,B) make_pair(A,B) #define PB(A) push_back(A) #define SIZE(A) ((int)A.size()) #define LEN(A) ((int)A.length()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define fi first #define se secondusing namespace std;template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; } template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }typedef long long ll; typedef unsigned long long ull; typedef long double lod; typedef pair<int,int> PR; typedef vector<int> VI;const lod eps=1e-6; const lod pi=acos(-1); const int oo=1<<30; const ll loo=1ll<<62; const int mods=1e9+7; const int MAXN=20; const int INF=0x3f3f3f3f;//1061109567 /*--------------------------------------------------------------------*/inline int read() {int w=0; bool q=1; char c=getchar();while ((c<'0'||c>'9')&&c!='-') c=getchar();if (c=='-') q=0,c=getchar();while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar();return q?w:-w; }int g[MAXN][MAXN],s[MAXN],f[(1<<MAXN)+5]; double x[MAXN],y[MAXN]; inline bool check(double x,double y){ return abs(x-y)<eps; } int main(){s[0]=1; for (int i=1;i<=19;i++) s[i]=s[i-1]<<1;int Case=read();while (Case--){int n=read(),m=read();for (int i=1;i<=n;i++) scanf("%lf%lf",&x[i],&y[i]);memset(g,0,sizeof(g));memset(f,INF,sizeof(f)); f[0]=0;for (int i=1;i<=n;i++)for (int j=i+1;j<=n;j++) {if (check(x[i],x[j])) continue; double a=(y[j]/x[j]-y[i]/x[i])/(x[j]-x[i]);if (a>=0) continue; double b=y[i]/x[i]-a*x[i];for (int k=1;k<=n;k++) g[i][j]+=s[k-1]*(check(a*x[k]+b,y[k]/x[k]));}for (int i=0;i<=s[n]-1;i++)for (int j=1;j<=n;j++)if (!(i&s[j-1])){for (int k=j+1;k<=n;k++) upmin(f[i|g[j][k]],f[i]+1);upmin(f[i|s[j-1]],f[i]+1);}printf("%d\n",f[s[n]-1]);}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的[NOIP2016]愤怒的小鸟(状压DP)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。