Jzoj P6309 完全揹包___貪心+揹包

題目大意:

在這裏插入圖片描述
在這裏插入圖片描述

分析:

在這裏插入圖片描述

代碼:

#pragma GCC optimize(3)
 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>

#define rep(i, st, ed) for (int i = st; i <= ed; i++)
#define rwp(i, ed, st) for (int i = ed; i >= st; i--)

#define N 1000005

using namespace std;

typedef long long ll;
typedef double db;

struct Node {
	int w, v;
}a[N];
ll f[2][N], ans, tot, m;
int val[105], maxv, n, usev;
db cmax = -1;

void read(int &x) {
	int f = 1; x = 0; char s = getchar();
	while (s < '0' || s > '9') { if (s == '-') f  = -1; s = getchar(); }
	while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); }
	x = x * f; 
}

int main() {
	freopen("backpack.in", "r", stdin);
	freopen("backpack.out", "w", stdout);
	read(n); scanf("%lld", &m);
	rep(i, 1, n) read(a[i].v), read(a[i].w), maxv = max(maxv, a[i].v);
	rep(i, 1, n) val[a[i].v] = max(val[a[i].v], a[i].w);
	rep(i, 1, maxv) {
		db now = (db)val[i] / i;
		if (now > cmax) cmax = now, usev = i;
	}
	tot = (ll)(m / usev) - 100;
	if (tot > 0) 
		m -= 1ll * tot * usev, ans += 1ll * tot * val[usev];
	int id = 0;
	rep(i, 1, maxv) {
		id ^= 1;
		rep(j, 0, i - 1) f[id][j] = f[id ^ 1][j];
		rep(j, i, m) f[id][j] = max(f[id][j - i] + val[i], f[id ^ 1][j]);
	}
	printf("%lld\n", f[id][m] + ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章