zoukankan      html  css  js  c++  java
  • P1541 乌龟棋

    30分做法,暴力枚举:

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 400;
    int n, m;
    int a[maxn], b[maxn]; //a:格子,b:卡牌
    void read() {
        cin >> n >> m;
        for(int i = 0; i < n; i++) cin >> a[i];
        for(int i = 0; i < m; i++) cin >> b[i];
    }
    int main() {
        read();
        sort(b, b+m);
        int ans = 0, tmp, pos, i;
        do{
            i = 0;
            tmp = 0;
            pos = 0;
            while(i <= m && pos <= n) {
                tmp += a[pos];
                pos += b[i++];
            }
            ans = max(ans, tmp);
        }while(next_permutation(b, b+m));
        cout << ans;
    } 
    

    100分做法:dp,f[i][j][k][l]为使用i张1,j张2,k张3,l张4;
    开始没有想到这个办法是因为没有注意到条件;
    上代码

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 355;
    const int maxm = 125;
    const int maa = 41;
    int n, m,one = 0, two = 0, three = 0, four = 0;
    int a[maxn], b[maxm]; //a:格子,b:卡牌
    void read() {
        cin >> n >> m;
        for(int i = 0; i < n; i++) cin >> a[i];
        for(int i = 0; i < m; i++) {cin >> b[i]; 
            if(b[i] == 1) one++;
            else if(b[i] == 2) two++;
            else if(b[i] == 3) three++;
            else four++;
        }
    }
    
    int main() {
        read();
        int f[maa][maa][maa][maa];
        f[0][0][0][0] = a[0];
        for(int i = 0; i <= one; i++) {
            for(int j = 0; j <= two; j++) {
                for(int k = 0; k <= three; k++) {
                    for(int l = 0; l <= four; l++) {
                        int maxx = 0;
                        if(i > 0)maxx = max(maxx, f[i-1][j][k][l]);
                        if(j > 0)maxx = max(maxx, f[i][j-1][k][l]);
                        if(k > 0)maxx = max(maxx, f[i][j][k-1][l]);
                        if(l > 0)maxx = max(maxx, f[i][j][k][l-1]);
                        f[i][j][k][l] = maxx + a[i*1+j*2+k*3+l*4];
                    }
                }
            }
        }
        cout << f[one][two][three][four];
    }
    
  • 相关阅读:
    ubuntu修改文件访问权限
    ubuntu使用root账户登录
    centos7 列出所有系统服务
    virtualbox 虚拟机硬盘扩容
    CI的意思
    更改centos 7 的默认启动为命令界面
    git Staging Deleted files
    Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义
    List of data structures:数据结构大全
    List of algorithms:算法大全
  • 原文地址:https://www.cnblogs.com/gengchen/p/6040557.html
Copyright © 2011-2022 走看看