当前位置:网站首页>【集训DAY13】Backpack【动态规划】【贪心】
【集训DAY13】Backpack【动态规划】【贪心】
2022-07-25 22:24:00 【VL——MOESR】

思路:
我们先贪心,把m压缩,然后剩下的空位再DP。
正确性不知道。
这里的是一种水法,由于数据太水得了满分,大家勿扣
c o d e code code
#include<iostream>
#include<cstdio>
#include<algorithm>
#define re register
#define ll long long
using namespace std;
const ll MAXN = 1e6 + 10;
ll n, tot, k, f[MAXN];
ll m;
struct node {
ll wei, val;
}a[MAXN], b[MAXN];
inline bool cmp1(re node x, re node y) {
if(x.wei != y.wei) return x.wei < y.wei;
else return x.val > y.val;
}
inline bool cmp2(re node x, re node y) {
if(x.val != y.val) return x.val < y.val;
else return x.wei < y.wei;
}
inline bool cmp3(re node x, re node y) {
if(m / x.wei * x.val != m / y.wei * y.val)
return m / x.wei * x.val > m / y.wei * y.val;
else return x.wei < y.wei;
}
int main() {
freopen("backpack.in", "r", stdin);
freopen("backpack.out", "w", stdout);
scanf("%lld%lld", &n, &m);
for(re ll i = 1; i <= n; ++ i) scanf("%d%d", &a[i].wei, &a[i].val);
sort(a + 1, a + 1 + n, cmp1);
for(re ll i = 1; i <= n; ) {
re ll j = i + 1;
while(a[j].wei == a[i].wei && j <= n) j ++;
-- j;
b[++ tot].wei = a[i].wei, b[tot].val = a[i].val;
i = j + 1;
}
n = tot;
tot = 0;
sort(b + 1, b + 1 + n, cmp2);
for(re ll i = 1; i <= n; ) {
re ll j = i + 1;
while(b[j].val == b[i].val && j <= n) j ++;
-- j;
a[++ tot].wei = b[i].wei, a[tot].val = b[i].val;
i = j + 1;
}
n = tot;
if(m * n <= 100000000 && m <= 1000000) {
for(re ll i = 1; i <= n; i ++)
for(re ll j = a[i].wei; j <= m; j ++)
f[j] = max(f[j], f[j - a[i].wei] + a[i].val);
printf("%d", f[m]);
}
else {
sort(a + 1, a + 1 + n, cmp3);
re ll ans = 0;
for(re ll i = 1; i <= n; i ++) {
ans = ans + m / a[i].wei * a[i].val;
m -= m / a[i].wei * a[i].wei;
}
printf("%lld", ans);
}
return 0;
}
边栏推荐
- 点亮字符串中所有需要点亮的位置,至少需要点几盏灯
- Randomly generate 10 (range 1~100) integers, save them to the array, and print the array in reverse order. And find the average value, the maximum value and the subscript of the maximum value, and fin
- What have I experienced to become a harder tester than development?
- 英文术语对应的解释
- 什么是类加载?类加载的过程?
- (1) DDL, DML, DQL, DCL and common data types
- 2day
- Playwright tutorial (II) suitable for Xiaobai
- QML module not found
- vim用法记录
猜你喜欢

Fill the whole square with the float property

ArcGIS10.2配置PostgreSQL9.2标准教程

SQL中in的用法 DQL 查询

Basic principle of torque motor control

『Skywalking』. Net core fast access distributed link tracking platform

Victoriametrics single node of kubernetes

xss-工具-Beef-Xss安装以及使用

Visitor mode

Wechat official account application development (I)
![[C syntax] void*](/img/34/b29b7bbf8eae9f1730352cac1301a4.png)
[C syntax] void*
随机推荐
科大讯飞智能办公本Air电纸书阅读器,让我的工作生活更加健康
【Leetcode】502.IPO(困难)
Method of converting MAPGIS format to ArcGIS
C语言逆序打印字符串的两种方法
Selenium basic use and use selenium to capture the recruitment information of a website (continuously updating)
Square root of X
Basic principle of torque motor control
访问者模式(visitor)模式
xss-收集常用的代码
【C语法】void*浅说
ORM common requirements
Arcgis10.2 configuring postgresql9.2 standard tutorial
MySQL --- 子查询 - 列子查询(多行子查询)
Ts:typera code fragment indentation display exception (resolved) -2022.7.24
Visitor mode
IFLYTEK smart office book air e-book reader makes my work life healthier
3day
分享两个音乐播放地址
[leetcode] 502.ipo (difficult)
谷歌分析UA怎么转最新版GA4最方便