Missile:雙狀態DP,missile狀態dp
題目
描述
Long , long ago ,country A invented a missile system to destroy the missiles from their enemy . That system can launch only one missile to destroy multiple missiles if the heights of all the missiles form a non-decrease sequence .
But recently , the scientists found that the system is not strong enough . So they invent another missile system . The new system can launch one single missile to destroy many more enemy missiles . Basically , the system can destroy the missile from near to far . When the system is begun , it chooses one enemy missile to destroy , and then destroys a missile whose height is lower and farther than the first missile . The third missile to destroy is higher and farther than the second missile … the odd missile to destroy is higher and farther than the previous one , and the even missile to destroy is lower and farther than the previous one .
Now , given you a list of the height of missiles from near to far , please find the most missiles that can be destroyed by one missile launched by the new system .
輸入
The input contains multiple test cases .
In each test case , first line is an integer n ( 0<n<=1000 ) , which is the number of missiles to destroy . Then follows one line which contains n integers ( <=109 ) , the height of the missiles followed by distance .
The input is terminated by n = 0 .
輸出
For each case , print the most missiles that can be destroyed in one line .
範例輸入
4
5 3 2 4
3
1 1 1
0
範例輸出
3
1
大意:一種飛彈,可以摧毀敵軍的飛彈。 但是有一種奇怪的設定:
敵軍飛彈由遠及近過來時, 此飛彈可以選擇其中的一顆開始摧毀, 然後接著摧毀比上一個遠的並且高度較低的飛彈,然後再摧毀更遠的但高度較高的飛彈,如此往複。
求最多打到的飛彈數量。
思路 這道題給人的第一感覺就是單調遞增子序列的變形 。 也就是最長凹凸子序列。。
很明顯就是dp問題。
求解dp問題的關鍵就是梳理出狀態轉移方程。 對於單調遞增子序列問題, 狀態轉移方程為: dp[i] = max (dp[k]+1 ) (k<i && v[k] < v[i]) ;
也就是,以當前點為結尾的的最長單調子序列, 是這個點之前的;值小於這個點的; 之中局部最優的+1 。
對於現在的問題,因為不是單調遞增的,所以要對dp方程進行變形 。
以為飛彈的規律是高低切換的,所以每個飛彈可能作為lower被擊落, 也有可能作為higher被擊落。 作為不同身份被擊落可能有不同的最優解,同時也會影響後面的飛彈。
因此,這裡需要儲存兩個狀態,簡單的約定為: a[i] : 飛彈i作為higher被擊落時的最優解 b[i] : 飛彈作為lower 被擊落時的最優解
相對的,新的dp方程為: a[i] = max{b[k]+1} (k<i&&v[k]<v[i]) b[i] = max{a[k]+1} (k<i&&v[k]>v[i])
初始化 a[0]=1 //第一個擊中i=0即可得到1這個解 b[0]=0 // 因為題意第一個擊中較高的, 所以排在位置0的飛彈不可能作為lower被擊落。 作為lower的最優解為0.
完整代碼:之前寫的代碼,很不規範。湊合看吧
#include<iostream>using namespace std;int a[1001],b[1001];int str[1001];int main(){ //freopen("aa.txt","r",stdin); int n,i,j,max2; while(scanf("%d",&n)!=EOF) { if(!n) break; for(i=0;i<n;i++) scanf("%d",&str[i]); for(i=0;i<n;i++) { a[i]=1; //as higher b[i]=0; //as lower } for(i=1;i<n;i++) { int max=1,max1=0; for(j=0;j<i;j++) { if(str[i]>str[j]) { a[i]=b[j]+1; if(a[i]>max) max=a[i]; } if(str[i]<str[j]) { b[i]=a[j]+1; if(b[i]>max1) max1=b[i]; } } b[i]=max1; a[i]=max; } max2=a[0]; for(i=0;i<n;i++) { if(a[i]>max2) max2=a[i]; if(b[i]>max2) max2=b[i]; } cout<<max2<<endl; }}
驗證 為什麼程式會保證得出的最優解是 “第一個擊中higher”的情況呢? 可以這樣證明: 因為所有位置i ,預設的b[i] 都為 0 。 如果最優解 出現在lower位置, 即b[k] 為所有a[] b[]中的最大值,則說明k前面必存在一個j使a[j] > b[k] . 也就是說有個higher先被擊中。
簡單測試。
DP590雙相鋼、DP780雙相鋼、DP980雙相鋼是哪個國家的牌號?線上急等
我們國家的,寶山鋼鐵股份有限公司的企業標準,詳見:Q/BQB 418-2009
DP是dual phase steels雙相鋼,數字是抗拉強度值。
永恒之塔劍星怎雙DP雙姿態
就是在人物頭像那邊對應的三行,分別是血、魔和DP。DP那一行顯示的就是你當前的dp值,這個數值平時可以通過打怪或者殺敵對種族獲得經驗值或者obs增加,現在滿值是4000。然後遊戲裡有兩種藥品,cd半小時,吃了以後分別可以增加2000或者4000的DP值。
而劍星普通技能書可以學到的兩個所謂姿態的技能分別需要3000和4000的dp值(對應的cd時間是半小時和一小時),兩種姿態都有攻擊力加成,持續30秒,一起開的話攻擊可以短時間提高很多,因為那個加dp的藥品是有cd時間的,所以就是說你打怪或者殺人攢夠了一個技能的dp值,然後用掉一個姿態,再立馬吃一個加dp的藥品再用一個姿態,就是所謂的雙dp雙姿態了。
不過劍星還有一個上級烙印技能突擊姿態,那個只需要魔,你可以一起開,就能達到三姿態,不過這個技能會降低你200的命中,如果命中本身就不夠建議別用這個技能。