Cf 105 Div.2

來源:互聯網
上載者:User


全部1A的,orz——————————。




A題

D很小,直接for(i,1,D) 。


B題

追擊問題。

我的做法:  while(1){ ... }    

最開始 int front= T * vp.  

那麼 vd * times == front + vp* times; 算出times 然後判斷是否有超過 dis,如果沒有,則

front+  ( ( f + times + times ) * vp ). 進入下一迴圈。


C題

構造問題,這個題得細心了。

給定n,a,b,表示n個數位序列,A類數有a個,B類數有b個,保證 n> a+b;

A類:   前面的數都小於該數;

B類:   前面所有數的和 小於 該數;

注意如果既是A類又是B類,那麼該數算B類。

我的構造方法:

開始是1 ,然後先構造B類,2,4,8,16...然後構造A類。取B類的最後那個數+1. 然後逐個加1.最後有多餘的全部取1.

比如 b=3,a=3,n=10 ==》

1  2  4  8  9 10 11 1 1 1.


注意了有一些特殊情況:

n=2, a=1,b=0:  你可能會寫: 1 2,這是不對的。

n=3, a=2,b=0: 。。。。。。1 2 3,這是不對的。

n=4, a=2, b=0: 。。。。。。1 2 3 1,這是不對的,我的解決方案: 1 1 2 3。

所以 ==》if( b==0 && 3+a-1 > n )  則puts("-1");


D題

有點遞推的意思,又有點博弈的味道,某個最終的局面由一個個子局面遞推而來。

dp[ w ][ b ][ 0 ]: 面對 w個white,b個black的局面,輪到p來取的時候,  p最後贏的機率。

dp[ w ][ b ][ 1 ]: 面對 w個white,b個black的局面,輪到d來取的時候,  p最後贏的機率。

遞推方程:

dp[ w ][ b ][ 0 ]=  w/(w+b) + b/(w+b)*dp[ w ][ b-1 ][ 1 ];

dp[ w ][ b ][ 1 ]:

考慮取w和取b兩個情況,同時每個情況下考慮跳出的mouse是w的還是b的。代碼比較簡單。


E題

This problem involved dynamic programming with precalculation.

The first part of the solution was to precalculate the maximal cost of i items taken from the shelf (i ranging from 1 to the number of items on the shelf) for each shelf. Note that this can’t be done greedily: this can be seen on
the shelf 6: 5 1 10 1 1 5.

The second part is a standard dynamic programming, which calculates the maximal cost of items taken for index of last shelf used and total number of items taken. To advance to the next shelf, one has to try all possible numbers
of items taken from it and increase the total cost of items taken by corresponding precalculated values.

上面是 blog。

我的做法是 預先處理每個row + 01背包。

對於每個row,假設有cnt個,需要得到取 j個 ( 1<=j<=cnt )個的最大值,那麼做法是逆向dp,題目需要的是兩邊向中間取,所以

f[ i ]: 對於某行,連續長度為i的子段的最小值。那麼總和sum-f[i]就是 取 cnt-i 個的最大值。

然後是01背包。複雜度O(n*m*100)。最終還是ac了,cf居然暴力也過了。其實我認為會逾時的,題目資料弱。


然後是我問了問題,結果國外那位熱心的大牛回答了: 10000*10000= 100000000,8個0,不會逾時。。。



表示差點忘記01背包了。

/*Feb 14,2012 3:45:03 PM yimao E-Porcelain GNU C++ Accepted 380ms 1800KB*/#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;#define MM(a,b) memset(a,b,sizeof(a));typedef long long lld;typedef unsigned long long u64;#define maxn 100020void up_max(int &x,int y){if((x<y)||(x==-1))x=y;}void up_min(int &x,int y){if((x>y)||(x==-1))x=y;}int dp[maxn];int a[110];int t[110]; // t[i]: consecutive length=i, the minmum value;int solve(int n){    int i,j,sum=0;    for(i=1;i<=n;++i) sum+= a[i];    t[n]= sum;    t[0]= 0;    for(i=1;i<=n;++i){        int s=0;        for(j=1;j<=i;++j)            s+= a[j];        t[i]=s;        for(j=i+1;j<=n;++j){            s-= a[j-i];            s+= a[j];            up_min( t[i], s );        }    }    return sum;}int main(){    int n,m,i,j,k;    while(cin>>n>>m){        MM(dp,-1);        dp[0]= 0; ////////        for(i=1;i<=n;++i){            int num;            scanf("%d",&num);            for(j=1;j<=num;++j)                scanf("%d", a+j);            int sum= solve(num);            for(j=m;j>0;--j){                for(k=1;k<=num&&k<=j;++k)                    up_max( dp[j], dp[j-k]+ ( sum- t[num-k] ) );            }        }        printf("%d\n", dp[m]);    }}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.