我的漲分日記(一)——BestCoder Round #56

hdu5463-Clarke and minecraft

題目大意我就不贅述了,在比賽的頁面有中文版的題目

思路:一開始想複雜了,其實這個題是非常水的。開一個長度是500的數組,把每個種類的材料數目累積下來,然後掃一遍每64個的材料算作一格,不滿一格的必須按一格算不可以和其他材料疊加。最後將所有的格數加起來,再每36格運一趟,不足一趟的按一趟算。

代碼:

#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
#define maxn 500+5
#define clr(x,y) memset(x,y,sizeof(x))
int ans[maxn];
typedef long long ll;
int main()
{
    int T,n,a,b;
    cin>>T;
    while(T--)
    {
        clr(ans,0);
        cin>>n;
        for(int i=0; i<n; i++)
        {
            scanf("%d %d",&a,&b);
            ans[a]+=b;
        }
        int ant=0;
        for(int i=1; i<=500; i++)
            if(ans[i])
            {
                ant+=ans[i]/64;
                ans[i]%=64;
                if(ans[i])ant++;
            }
        int k=ant/36;
        ant%=36;
        if(ant)k++;
        cout<<k<<endl;
    }
    return 0;
}



hdu5464-Clarke and problem

題意也不說了

思路:說實話,看題目的時候沒看出來這是dp,看來我的動態規劃要重新學習!

但是看完題解才發現這確實肯定是dp,部分數據給的這麼小,而且滿足最優子結構、無後效性。。。

那麼令dp(i,j)表示用前i個數模p等於j的方案數,那麼狀態轉移方程是dp(i,j)=dp(i-1,j)+dp(i-1,(j-a[i])mod p),注意這裏狀態轉移方程與官方題解給的不一樣(我覺得那個題解是錯誤的!)

解釋是用前i個數模p等於j的方案數包括兩個部分的內容:

一個是不取第i個數,那麼就是用前i-1個數模p等於j的方案數

另一個是取第i個數,那麼就是用前i-1個數模p等於(j-a[i])的方案數   (取模後爲負數的定義我就不給出了)


代碼:

#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
#define maxn 1000+5
#define clr(x,y) memset(x,y,sizeof(x))
typedef long long ll;
int p;
ll a[maxn];
ll d[maxn][maxn];
ll dp(int i,int j)
{
    if(d[i][j]>=0)return d[i][j];
    if(!i)
    {
        if(!j)return d[i][j]=1;
        else return d[i][j]=0;
    }
    ll k=j-a[i];
    if(k>=0) k%=p;
    else
    {
        ll h=-k/p;
        k+=(h+1)*p;
        k%=p;
    }
    return d[i][j]=(dp(i-1,j)+dp(i-1,k))%1000000007;
}
int main()
{
    int T,n;
    cin>>T;
    while(T--)
    {
        cin>>n>>p;
        clr(d,-1);
        for(int i=1;i<=n;i++)
            scanf("%lld",a+i);
        printf("%lld\n",dp(n,0));
    }
    return 0;
}

hdu5465-Clarke and puzzle

這題暫時不會,還需要回去把博弈論裏的NIM取數遊戲和樹狀數組複習一下。。。

爭取這週週末把我自己的思路寫上來。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章