2017CCPC- 女生专场 第2题(hdu 6024)和第8题(hdu 6030)

来源:互联网 发布:电视连接网络怎么设置 编辑:程序博客网 时间:2024/06/08 04:37

1002  Building Shops http://acm.hdu.edu.cn/showproblem.php?pid=6024

这题题目有点难看懂;

题意: x轴上有n个班级,第i个班级建商店的费用为c[i];不建的话也会有费用,费用就是从i到距离i的左边最近的商店的距离,问总费用最少是多少;

题解:毫无疑问,第1个教室是肯定要建商店的;

然后就dp; dp[I][0]表示第I个教室不建商店,前I个教师的花费;

                    dp[I][1]表示第I个教室建商店,前I个教师的花费;

状态转移的话 dp[I][1] = min(dp[I-1][0],  dp[I-1][1]) + c[I]; 

                    dp[I][0] 的转移有点坑我一开始以为是由dp[I-1][0]转来的,然后贡献了一次wa;

                     是这样的,dp[I][0] = dp[j][0] + x[I]-x[j]; (j=1~I-1);

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long ll;const int MAXN = 3008;ll dp[MAXN][2];//int path[MAXN];struct classroom {    int x, c;} ritian[MAXN];int cmp(classroom xx, classroom yy) {    return xx.x < yy.x;}int main() {    int n;    //freopen("in.txt", "r", stdin);    while (~scanf("%d", &n)) {        for(int i = 1; i <= n; i++)            scanf("%d%d", &ritian[i].x, &ritian[i].c);        sort(ritian+1, ritian+n+1, cmp);        dp[1][0] = (ll)1<<60;        dp[1][1] = ritian[1].c;        for(int i = 2; i <= n; i++) {            dp[i][1] = min(dp[i-1][0], dp[i-1][1])+ritian[i].c;            ll tmp = 0;            dp[i][0] = (ll)1<<60;            for(int j = i-1; j > 0; j--) {                tmp = (ll)(ritian[j+1].x-ritian[j].x)*(i-j)+tmp;//这里少写了(ll)也贡献了2次wa,强迫症的我表示一个变量的值小于int型,我决不                       //会定义成long long 型->.->;                dp[i][0] = min(dp[i][0], dp[j][1]+tmp);            }        }        printf("%I64d\n", min(dp[n][0], dp[n][1]));    }    return 0;}

1008 Happy Necklace http://acm.hdu.edu.cn/showproblem.php?pid=6030
题意: 小A要给他女朋友买一条n颗珠子的项链,他女朋友要求项链的珠子只有红和蓝这两种颜色,而且要保证每连续的奇数个珠子中,红珠子要大于等于蓝珠子;
2 <= n <= 10^18;问这条项链有多少个放珠子的方法;
题解: n > 4时, a[n] = a[n-1] + a[n-3],(需要时间推出来),意思是n相当于在n-1上加一个红珠子和在n-3上加一个红红蓝,这么说你懂我意思了吧, 然后上矩阵快速幂;
代码:
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int N=3;typedef long long ll;const ll Mod=1000000000+7;struct Mat{    ll mat[N+1][N+1];};Mat Multiply(Mat a, Mat b){    Mat c;    memset(c.mat, 0, sizeof(c.mat));    for(int k = 0; k < N; ++k)        for(int i = 0; i < N; ++i)            if(a.mat[i][k])                for(int j = 0; j < N; ++j)                    if(b.mat[k][j])                        c.mat[i][j] = (c.mat[i][j] +a.mat[i][k] * b.mat[k][j])%Mod;    return c;}Mat QuickPower(Mat a, ll k){    Mat c;    memset(c.mat,0,sizeof(c.mat));    for(int i = 0; i < N; ++i)        c.mat[i][i]=1;    for(; k; k >>= 1)    {        if(k&1) c = Multiply(c,a);        a = Multiply(a,a);    }    return c;}int main(){    int T;    scanf("%d",&T);    ll a[]={0,1,3,4,6};    Mat A;    A.mat[0][0]=1,A.mat[0][1]=1,A.mat[0][2]=0;    A.mat[1][0]=0,A.mat[1][1]=0,A.mat[1][2]=1;    A.mat[2][0]=1,A.mat[2][1]=0,A.mat[2][2]=0;    while(T--)    {        ll n;        scanf("%I64d",&n);        if(n<=4)        {            printf("%I64d\n",a[n]);            continue;        }        Mat ans=QuickPower(A,n-4);        printf("%I64d\n",(a[4]*ans.mat[0][0]+a[3]*ans.mat[1][0]+a[2]*ans.mat[2][0])%Mod);    }    return 0;}


0 0
原创粉丝点击