当前位置:网站首页>[training day13] backpack [dynamic planning] [greed]
[training day13] backpack [dynamic planning] [greed]
2022-07-25 22:30:00 【VL——MOESR】

Ideas :
Let's be greedy first , hold m Compress , Then the rest of the vacant seats DP.
I don't know the correctness .
Here is a kind of water method , Because the data is too poor, I got full marks , Don't buckle
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;
}
边栏推荐
- Simulated Xiaomi mall head home page
- [C syntax] void*
- Today, learn about the use of lists, hyperlinks, image tags, and audio and video
- xss-收集常用的代码
- internship:普通常用的工具类编写
- Leetcode 106. construct binary tree from middle order and post order traversal sequence
- JVM内存区域
- 平台架构搭建
- PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
- Matrix of C language
猜你喜欢

How to resolve a domain name to multiple IP addresses?

PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示

编译和反编译

Xiaobai programmer's seventh day

Xiaobai programmer's fifth day

Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture

MapGIS格式转ArcGIS方法

Data quality: the core of data governance

【集训DAY15】Boring【树形DP】
![[training day13] out race [mathematics] [dynamic planning]](/img/94/d86031a062c9311f83d63809492d71.png)
[training day13] out race [mathematics] [dynamic planning]
随机推荐
[leetcode] 502.ipo (difficult)
Simulated Xiaomi mall head home page
Usage of in in SQL DQL query
Binder原理
完啦,上班三个月,变秃了
【PMP学习笔记】第1章 PMP体系引论
Mitsubishi FX PLC free port RS command realizes Modbus Communication
【集训DAY13】Travel【暴力】【动态规划】
科大讯飞智能办公本Air电纸书阅读器,让我的工作生活更加健康
H5 lucky scratch lottery free official account + direct operation
VIM usage record
Xiaobai programmer the next day
QML module not found
3dslicer importing medical image data
Tfrecord write and read
Platform architecture construction
对需求的内容进行jieba分词并按词频排序输出excel文档
How to resolve a domain name to multiple IP addresses?
ThreadLocal summary (to be continued)
Xiaobai programmer day 8