我的涨分日记(一)——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取数游戏和树状数组复习一下。。。

争取这周周末把我自己的思路写上来。。。

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