题目链接:
https://www.luogu.com.cn/problem/P1417
#01背包 #贪心 #Favorite
和能量石那道题一样,也和这道题 [[LG P1080 国王游戏]] 有关系
为什么能只考虑相邻的两个呢?
因为交换相邻的两个物品,只会对这两个物品产生影响,不会对这两个物品前面的值和后面的值产生影响
具体的推导过程见下图:
代码
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Main {
static int status;
static BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static PrintWriter cout = new PrintWriter(bw);
static StreamTokenizer st = new StreamTokenizer(buf);
public static int nextInt() throws IOException {
status = st.nextToken();
return (int) st.nval;
}
public static long nextLong() throws IOException {
status = st.nextToken();
return (long) st.nval;
}
public static String nextString() throws IOException {
status = st.nextToken();
return st.sval;
}
public static String nextLine() throws IOException {
String line = buf.readLine();
while (line.isEmpty())
line = buf.readLine();
return line;
}
static int n, m,cnt=0;
static int[] a,b,c;
static long[] f;
static Pair[] p;
static final int INF = 0x3f3f3f3f, MOD = (int) 1e9+7;
public static void main(String[] args) throws IOException {
m = nextInt();
n = nextInt();
a = new int[n+1];
b = new int[n+1];
c = new int[n+1];
f = new long[m+1];
p = new Pair[n+1];
for(int i=1;i<=n;i++) a[i] = nextInt();
for(int i=1;i<=n;i++) b[i] = nextInt();
for(int i=1;i<=n;i++) c[i] = nextInt();
for(int i=1;i<=n;i++)
p[i] = new Pair(a[i], b[i], c[i]);
Arrays.sort(p,1,1+n);
for(int i=1;i<=n;i++)
for(int j=m;j>=p[i].c;j--)
f[j]=max(f[j],f[j-p[i].c]+p[i].a-(long)j*p[i].b);
long ans=0;
for(int i=1;i<=m;i++)
ans = max(ans,f[i]);
cout.println(ans);
cout.flush();
}// End of main
static class Pair implements Comparable<Pair>{
int a,b,c;
public Pair(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(Pair that){
return this.c*that.b-this.b*that.c;
}
}
}