头像

晓玲

重庆邮电大学


访客:9864

离线:7小时前


活动打卡代码 AcWing 734. 能量石

晓玲
1天前
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 10010;
int n, t;
LL f[N];

struct Stone {
    int s, e, l;
    bool operator< (const Stone &W) const {
        return s * W.l <= W.s * l;
    }
}stone[N];

int main() {
    cin >> t;
    for (int cnt = 1; cnt <= t; cnt ++) {
        cin >> n;
        int m = 0;
        memset(f, -0x3f, sizeof f);
        f[0] = 0;
        for (int i = 1; i <= n; i ++) {
            int s, e, l;
            cin >> s >> e >> l;
            stone[i] = {s, e, l};
            m += s;
        }
        sort(stone + 1, stone + 1 + n);
        //for (int i = 1; i <= n; i ++) cout << stone[i].s << " " << stone[i].l << endl;
        for (int i = 1; i <= n; i ++) {
            int s = stone[i].s, e = stone[i].e, l = stone[i].l;
            for (int j = m; j >= s; j --)
                f[j] = max(f[j], f[j - s] + max(0, e - (j - s) * l));
        }
        int res = *max_element(f + 1, f + 1 + m);
        printf("Case #%d: %d\n", cnt, res);
    }
    return 0;
}



晓玲
1天前
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
long long f[N][N];
int n, m;
int w[N], v[N];


int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++) {
        cin >> v[i] >> w[i];
    }

    for (int i = n; i >= 1; i --)
        for (int j = 0; j <= m; j++) {
            f[i][j] = f[i + 1][j];
            if (j >= v[i]) f[i][j] = max(f[i][j], f[i + 1][j - v[i]] + w[i]);
        }

    //cout << f[1][m] << endl;
    int j = m;
    for (int i = 1; i <= n; i ++) {
        if(j >= v[i] && f[i][j] == f[i + 1][j - v[i]] + w[i]) {
            cout << i << " ";
            j -= v[i];
        }
    }

    return 0;
}



晓玲
1天前
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
int n, m;
int f[N], g[N];
int MOD = 1e9+7;

int main() {
    cin >> n >> m;
    fill(g, g + n, 1);
    for (int i = 0; i < n; i ++) {
        int v, w;
        cin >> v >> w;
        for (int j = m; j >= v; j --) {
            int maxv = max(f[j], f[j - v] + w);
            int cnt = 0;
            if (maxv == f[j]) cnt += g[j];
            if (maxv == f[j - v] + w) cnt = (cnt + g[j - v]) % MOD;
            f[j] = maxv;
            g[j] = cnt;
        }
    }
    //cout << f[m] << endl;
    cout << g[m];
    return 0;
}


活动打卡代码 AcWing 7. 混合背包问题

晓玲
1天前
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
int f[N];
int n, m;

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++) {
        int v, w, s;
        scanf("%d %d %d", &v, &w, &s);

        if (s == 0) {
            for (int j = v; j <= m; j ++)
                f[j] = max(f[j], f[j - v] + w);
        }
        else {
            if (s == -1) s = 1;
            int k = 1;
            while (k <= s) {
                int kv = k * v;
                int kw = k * w;
                s -= k;
                k *= 2;
                for (int j = m; j >= kv; j --)
                    f[j] = max(f[j], f[j - kv] + kw);
            }
            if (s) {
                int kv = s * v;
                int kw = s * w;
                for (int j = m; j >= kv; j --)
                    f[j] = max(f[j], f[j - kv] + kw);
            }
        }
    }
    cout << f[m];
    return 0;
}



活动打卡代码 AcWing 426. 开心的金明

晓玲
2天前
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int N = 30010;
LL f[N];
int m, n;

int main() {
    cin >> m >> n;
    for (int i = 0; i < n; i ++) {
        int v, w;
        cin >> v >> w;
        w = v * w;
        for (int j = m; j >= v; j --)
            f[j] = max(f[j], f[j - v] + w);
    }
    cout << f[m] << endl;
    return 0;
}



活动打卡代码 AcWing 1013. 机器分配

晓玲
2天前
#include <bits/stdc++.h>
using namespace std;

const int N = 12, M = 16;
long long f[N][M];
int w[N][M];
int n, m;

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i ++)
        for (int j = 1; j <= m; j ++)
            cin >> w[i][j];

    for (int i = n; i >= 1; i --) {
        for (int j = 0; j <= m; j ++) {
            f[i][j] = f[i + 1][j];
            for (int k = 1; k <= m; k ++)
                if (j >= k)
                    f[i][j] = max(f[i][j], f[i + 1][j - k] + w[i][k]);
        }
    }
    cout << f[1][m] << endl;
    int j = m;
    for (int i = 1; i <= n; i ++) {
        for (int k = 0; k <= m; k ++)
            if (f[i][j] == (f[i + 1][j - k] + w[i][k])) {
                printf("%d %d\n", i, k);
                j -= k;
                break;
            }
    }
    return 0;
}


活动打卡代码 AcWing 1020. 潜水员

晓玲
2天前
#include <bits/stdc++.h>
using namespace std;

const int N = 25, M = 80;
long long f[N][M];
int n, m, k;

int main() {
    cin >> n >> m >> k;
    memset(f, 0x3f, sizeof f);
    f[0][0] = 0;
    for (int i = 0; i < k; i ++) {
        int v1, v2, w;
        cin >> v1 >> v2 >> w;
        for (int j = n; j >= 0; j --)
            for (int t = m; t >= 0; t --) {
                f[j][t] = min(f[j][t], f[max(0, j - v1)][max(0, t - v2)] + w);
            }

    }
    cout << f[n][m] << endl;
    return 0;
}


活动打卡代码 AcWing 1021. 货币系统

晓玲
2天前
#include <bits/stdc++.h>
using namespace std;

const int N = 3010, M = 20;
typedef long long LL;
LL f[N];
int n, m;

int main() {
    cin >> n >> m;
    f[0] = 1;
    for (int i = 0; i < n; i ++) {
        int v;
        cin >> v;
        for (int j = v; j <= m; j ++) {
            //f[j] += f[i - 1][j];
            f[j] += f[j-v];
        }
    }
    cout << f[m] << endl;
    return 0;
}


活动打卡代码 AcWing 1161. 数字游戏

晓玲
4天前
#include<bits/stdc++.h>
using namespace std;

int main() {
    string str;
    cin >> str;
    int ans = 0;
    for (auto s:str)
        if (s == '1')
            ans ++;
    cout << ans << endl;
    return 0;
}



晓玲
4天前
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int f[N][N];
int n, V, M;

int main() {
    cin >> n >> V >> M;
    for (int i = 0; i < n; i ++){
        int v, m, w;
        cin >> v >> m >> w;
        for (int j = V; j >= v; j --)
            for (int k = M; k >= m; k --)
                f[j][k] = max(f[j][k], f[j - v][k - m] + w);
    }
    cout << f[V][M];
    return 0;
}