2023年8月1日发(作者:)
背包问题——⽆限物品的背包问题,0-1背包问题与劲歌⾦曲(uva12563)⽆限物品的背包问题物品⽆限的背包问题。有n种物品,每种均有⽆穷多个。第i种物品的体积为Vi,重量为Wi。选⼀些物品装到⼀个容量为C的背包中,使得背包内物品在总体积不超过C的前提下重量尽量⼤。1≤n≤1001≤n≤100,1≤Vi≤C≤10000,1≤Wi≤106。测试数据:3 51 22 33 2answer:103 72 13 24 3answer:53 53 34 23 2answer:30解题思路dp思维:每个物品都可以⽆限使⽤,且我们不需要考虑能否完全利⽤背包的容量C,只要所装物品的重量总和最⼤即可。由于物品是⽆限使⽤的,所以在状态转移中我们所需要思考的是此时背包的容量,⽽不是对于⾯向物品思考状态。状态转移⽅程:i代表着背包的容积dp【i】=max(dp【i】,dp【i-v【j】】+w【j】)递归思维:相当于⼀个有向图的动态规划,我们对每个重量都探讨它接下可装物品的最⼤重量。递归:#include
1 22 43 44 5
5 6answer:10解题思路递归由于每个物品只能使⽤⼀次,所以我们需要标记,类似于DFS进⾏标记与恢复标记,再利⽤图的思维,对着不同体积,探讨它接下来体积所能装下物品重量的最⼤值#include
dp[j]=Max(dp[j],dp[j-v]+w); }劲歌⾦曲(If you smiled when you see the title, this problem is for you
_)For those who don’t know KTV, see: /wiki/Karaoke_boxThere is one very popular song called Jin Ge Jin Qu(劲歌⾦曲). It is a mix of 37 songs, and isextremely long (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, andsome other unofficial versions. But in this problem please forget about is it popular? Suppose you have only 15 seconds left (until your time is up), then you shouldselect another song as soon as possible, because the KTV will not crudely stop a song before it ends(people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extraseconds! ….and if you select Jin Ge Jin Qu, you’ll get 663 extra secondsNow that you still have some time, but you’d like to make a plan now. You should stick to thefollowing rules: Don’t sing a song more than once (including Jin Ge Jin Qu). For each song of length t, either sing it for exactly t seconds, or don’t sing it at all. When a song is finished, always immediately start a new goal is simple: sing as many songs as possible, and leave KTV as late as possible (since wehave rule 3, this also maximizes the total lengths of all songs we sing) when there are he first line contains the number of test cases T (T ≤ 100). Each test case begins with two positiveintegers n, t (1 ≤ n ≤ 50, 1 ≤ t ≤ 109), the number of candidate songs (BESIDES Jin Ge Jin Qu)and the time left (in seconds). The next line contains n positive integers, the lengths of each song, inseconds. Each length will be less than 3 minutes — I know that most songs are longer than 3 don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends. Sohere “length” actually means “length of the part that we want to sing”.It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly largerthan For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengthsof songs that you’ll ation:In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qufor another 678 the second example, we sing the first two (30+69=99 seconds). Then we still have one secondleft, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third songinstead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so wecan’t sing Jin Ge Jin Qu anymore!Universidad de Valladolid OJ: 12563 – Jin Ge Jin Qu hao 2/2Sample Input23 10060 70 803 10030 69 70Sample OutputCase 1: 2 758Case 2: 3 777题意:假如你的ktv还有15秒就时间到了,但是当你正在唱⼀⾸歌的时候他们并不会把你赶⾛⽽是会等该歌曲播完才让你离开,这时你可以点⼀⾸两分钟的歌,相当于你多唱了105秒。假如你正在ktv唱歌,还剩下t秒时间,你决定接下来只唱你最爱的n⾸歌,在时间结束之前你会再点⼀⾸《劲歌⾦曲》(时长678秒),求出你⼀共唱了⼏⾸歌和唱了多长时间。输⼊n(n<=50),t(t<=10^9)和每⾸歌的长度(保证不超过三分钟)。解题思路单纯的思考所唱的时间最长,就是⼀个简单的01背包问题,边界有所不同是t-1(留⼀秒给劲歌⾦曲)。#include
2023年8月1日发(作者:)
背包问题——⽆限物品的背包问题,0-1背包问题与劲歌⾦曲(uva12563)⽆限物品的背包问题物品⽆限的背包问题。有n种物品,每种均有⽆穷多个。第i种物品的体积为Vi,重量为Wi。选⼀些物品装到⼀个容量为C的背包中,使得背包内物品在总体积不超过C的前提下重量尽量⼤。1≤n≤1001≤n≤100,1≤Vi≤C≤10000,1≤Wi≤106。测试数据:3 51 22 33 2answer:103 72 13 24 3answer:53 53 34 23 2answer:30解题思路dp思维:每个物品都可以⽆限使⽤,且我们不需要考虑能否完全利⽤背包的容量C,只要所装物品的重量总和最⼤即可。由于物品是⽆限使⽤的,所以在状态转移中我们所需要思考的是此时背包的容量,⽽不是对于⾯向物品思考状态。状态转移⽅程:i代表着背包的容积dp【i】=max(dp【i】,dp【i-v【j】】+w【j】)递归思维:相当于⼀个有向图的动态规划,我们对每个重量都探讨它接下可装物品的最⼤重量。递归:#include
1 22 43 44 5
5 6answer:10解题思路递归由于每个物品只能使⽤⼀次,所以我们需要标记,类似于DFS进⾏标记与恢复标记,再利⽤图的思维,对着不同体积,探讨它接下来体积所能装下物品重量的最⼤值#include
dp[j]=Max(dp[j],dp[j-v]+w); }劲歌⾦曲(If you smiled when you see the title, this problem is for you
_)For those who don’t know KTV, see: /wiki/Karaoke_boxThere is one very popular song called Jin Ge Jin Qu(劲歌⾦曲). It is a mix of 37 songs, and isextremely long (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, andsome other unofficial versions. But in this problem please forget about is it popular? Suppose you have only 15 seconds left (until your time is up), then you shouldselect another song as soon as possible, because the KTV will not crudely stop a song before it ends(people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extraseconds! ….and if you select Jin Ge Jin Qu, you’ll get 663 extra secondsNow that you still have some time, but you’d like to make a plan now. You should stick to thefollowing rules: Don’t sing a song more than once (including Jin Ge Jin Qu). For each song of length t, either sing it for exactly t seconds, or don’t sing it at all. When a song is finished, always immediately start a new goal is simple: sing as many songs as possible, and leave KTV as late as possible (since wehave rule 3, this also maximizes the total lengths of all songs we sing) when there are he first line contains the number of test cases T (T ≤ 100). Each test case begins with two positiveintegers n, t (1 ≤ n ≤ 50, 1 ≤ t ≤ 109), the number of candidate songs (BESIDES Jin Ge Jin Qu)and the time left (in seconds). The next line contains n positive integers, the lengths of each song, inseconds. Each length will be less than 3 minutes — I know that most songs are longer than 3 don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends. Sohere “length” actually means “length of the part that we want to sing”.It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly largerthan For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengthsof songs that you’ll ation:In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qufor another 678 the second example, we sing the first two (30+69=99 seconds). Then we still have one secondleft, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third songinstead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so wecan’t sing Jin Ge Jin Qu anymore!Universidad de Valladolid OJ: 12563 – Jin Ge Jin Qu hao 2/2Sample Input23 10060 70 803 10030 69 70Sample OutputCase 1: 2 758Case 2: 3 777题意:假如你的ktv还有15秒就时间到了,但是当你正在唱⼀⾸歌的时候他们并不会把你赶⾛⽽是会等该歌曲播完才让你离开,这时你可以点⼀⾸两分钟的歌,相当于你多唱了105秒。假如你正在ktv唱歌,还剩下t秒时间,你决定接下来只唱你最爱的n⾸歌,在时间结束之前你会再点⼀⾸《劲歌⾦曲》(时长678秒),求出你⼀共唱了⼏⾸歌和唱了多长时间。输⼊n(n<=50),t(t<=10^9)和每⾸歌的长度(保证不超过三分钟)。解题思路单纯的思考所唱的时间最长,就是⼀个简单的01背包问题,边界有所不同是t-1(留⼀秒给劲歌⾦曲)。#include
发布评论