AcWing 1205. 买不到的数目
思路:
裴蜀定理: 对任何整数 a a a、 b b b,存在整数 x x x、 y y y,使得 a x + b y = ( a , b ) ax + by = (a, b) ax+by=(a,b), ( a , b ) (a, b) (a,b)表示 a a a、 b b b的最大公因数,令 d = ( a , b ) d=(a, b) d=(a,b)。若 d = 1 d = 1 d=1,则 a n x + b n y = n d anx + bny = nd anx+bny=nd, n n n 的取值即为整数 a a a、 b b b可以凑得的方案数,整数 a a a、 b b b凑不出来的数有一个上界,且一定小于 a b ab ab,因此可以枚举求凑不出来的最大值,或者使用结论:不能被表示出来的数有一个上界,上界为 a b − a − b ab - a - b ab−a−b。
相关证明:a,b组合数不能表示的上界
思路:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/2/27 9:30
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String[] nm = in.readLine().split(" ");
int n = Integer.parseInt(nm[0]), m = Integer.parseInt(nm[1]);
out.println(n * m - n - m);
out.flush();
}
}
AcWing 1211. 蚂蚁感冒
思路:
首先判断感冒的蚂蚁往哪走。
- 往左走,则在该蚂蚁左边且往右走的蚂蚁都会感染。
- 往右走,则在该蚂蚁右边且往左走的蚂蚁都会感染。
否则最后感染的只有初始感冒的那一只。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/3/1 9:22
*/
public class Main {
static final int N = 55;
static int[] a = new int[N];
static int n;
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
n = Integer.parseInt(in.readLine());
String[] s = in.readLine().split(" ");
int start = 0, l = 0, r = 0;
// 1表示正,-1表示负
int f = 0;
for (int i = 1; i <= n; i++) {
a[i] = Integer.parseInt(s[i - 1]);
if (i == 1)
{
start = a[i];
f = start > 0 ? 1 : -1;
}
// 往右走并且在感冒蚂蚁左边的
if (a[i] > 0 && Math.abs(a[i]) < Math.abs(start)) l++;
// 往左走并且在感冒蚂蚁右边的
if (a[i] < 0 && Math.abs(a[i]) > Math.abs(start)) r++;
}
// 感冒蚂蚁往右走 但是 右边没有往左走
// 感冒蚂蚁往左走 但是 左边没有往右走
if ((f == 1 && r == 0) || (f == -1 && l == 0)) out.println(1);
else out.println(l + r + 1);
out.flush();
}
}
AcWing 1216. 饮料换购
思路:
瓶盖数等于饮料数,每换一瓶饮料,瓶盖数 - 3,饮料数 + 1, 瓶盖数 + 1,最后当瓶盖数小于3时,就不能换购了。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/2/27 9:34
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(in.readLine().split(" ")[0]);
int ans = n;
// 瓶盖数
int cnt = n;
while (cnt >= 3) {
int tmp = cnt / 3;
ans += tmp;
cnt %= 3;
cnt += tmp;
}
out.println(ans);
out.flush();
}
}
AcWing 2. 01背包问题
思路:
01背包问题,表示每个物品要么放,要么不放。从集合的角度分析DP问题,状态表示为:选择前i个物品,总体积小于等于j的选法的集合,属性f[i][j]
表示价值的最大值。状态计算,因为每个状态可以表示为选择当前的物品,或者不选当前的物品,二者价值取最大值即可,即状态转移方程为:
f
[
i
]
[
j
]
=
m
a
x
(
f
[
i
−
1
]
[
j
]
,
f
[
i
−
1
]
[
j
−
v
i
]
+
w
i
)
,
j
≥
v
i
f[i][j] = max(f[i - 1][j], f[i - 1][j - v_i]+w_i), j \ge v_i
f[i][j]=max(f[i−1][j],f[i−1][j−vi]+wi),j≥vi
一维优化
f[i][j] = f[i - 1][j];
f[i][j] = max(f[i][j], f[i - 1][j - v[i]] + w[i]);
变化为
f[j] = f[j] // 省略
f[j] = max(f[j], f[j - v[i]] + w[i])
由于f[i][j]
需要用到第i - 1层的结果,j-v[i]严格小于j, j - v[i] 在第i层已经被算过了,所以用j-v[i]更新j时,用的是第i层的结果,j逆序,则 j 会比 j - v[i]先被计算,所以用到 j - v[i]时,用的是上一层即第 i - 1层的结果。
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/2/27 9:39
*/
public class Main {
static final int ln = 1005;
static int[] v = new int[ln], w = new int[ln];
static int[] dp = new int[ln];
static int N, V;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String[] nv = in.readLine().split(" ");
N = Integer.parseInt(nv[0]);
V = Integer.parseInt(nv[1]);
for (int i = 1; i <= N; i++) {
String[] s = in.readLine().split(" ");
v[i] = Integer.parseInt(s[0]);
w[i] = Integer.parseInt(s[1]);
}
for (int i = 1; i <= N; i++)
for (int j = V; j >= v[i]; j--)
dp[j] = Math.max(dp[j], dp[j - v[i]] + w[i]);
out.println(dp[V]);
out.flush();
}
}
AcWing 1015. 摘花生
思路:
到达每一点有两种方式,一种是从该点左边,一种是从该点上边到达,则最大价值即为这两种方式的最大价值,加上该点的价值。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/3/1 9:55
*/
public class Main {
static final int N = 105;
static int[][] f = new int[N][N];
static int t, r, c;
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String T = in.readLine();
t = Integer.parseInt(T);
while (t-- != 0) {
String[] rc = in.readLine().split(" ");
r = Integer.parseInt(rc[0]);
c = Integer.parseInt(rc[1]);
for (int i = 1; i <= r; i++) {
String[] s = in.readLine().split(" ");
for (int j = 1; j <= c; j++) {
f[i][j] = Integer.parseInt(s[j - 1]);
}
}
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
f[i][j] += Math.max(f[i][j - 1], f[i - 1][j]);
}
}
out.println(f[r][c]);
out.flush();
}
}
}
AcWing 895. 最长上升子序列
思路:
线性DP的使用,状态是一维的,转移又是一维的,所以一共是两维。枚举每一个整数,则以该整数结尾的子序列的长度初始化为1,枚举该数之前的数,若大于之前的数,则状态转移,取长度的最大值。最后计算以每个整数结尾的子序列长度的最大值。
状态转移方程:
d p [ i ] = m a x ( d p [ i ] , d p [ j + 1 ] + 1 ) a [ i ] > a [ j ] , 1 ≤ j ≤ i dp[i] = max(dp[i], dp[j+1] + 1) \: a[i] > a[j] ,\: 1 \le j \le i dp[i]=max(dp[i],dp[j+1]+1)a[i]>a[j],1≤j≤i
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/2/27 9:54
*/
public class Main {
static final int ln = 1005;
static int[] a = new int[ln];
static int[] dp = new int[ln];
static int n;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String[] nv = in.readLine().split(" ");
n = Integer.parseInt(nv[0]);
String[] s = in.readLine().split(" ");
for (int i = 1; i <= n; i++) a[i] = Integer.parseInt(s[i - 1]);
int ans = 0;
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int j = i - 1; j > 0; j--) {
if (a[i] > a[j]) dp[i] = Math.max(dp[i], dp[j] + 1);
}
ans = Math.max(ans, dp[i]);
}
out.println(ans);
out.flush();
}
}
AcWing 1212. 地宫取宝
思路:
本题数据范围较小,所以可能有多维,用闫氏DP分析法进行分析。
由于
0
≤
C
i
≤
12
0 \le C_i \le 12
0≤Ci≤12,放价值为0的和不放物品等价,为了区分,将每一个物品的价值加一,则
1
≤
C
i
′
≤
13
1 \le C_i' \le 13
1≤Ci′≤13。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
/**
* @Description
* @Author: PrinceHan
* @CreateTime: 2023/3/1 10:15
*/
public class AC1212 {
static final int N = 55, mod = 1000000007;
static int[][] c = new int[N][N];
// 前两维表示位置,第三维表示取了k个物品,第思维表示当前最大值
static int[][][][] f = new int[N][N][13][14];
static int n, m, k;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
String[] nmk = in.readLine().split(" ");
n = Integer.parseInt(nmk[0]);
m = Integer.parseInt(nmk[1]);
k = Integer.parseInt(nmk[2]);
for (int i = 1; i <= n; i++) {
String[] s = in.readLine().split(" ");
for (int j = 1; j <= m; j++) {
c[i][j] = Integer.parseInt(s[j - 1]);
// 因为不放物品与放价值为0的物品 价值是一样的 加1是为了区分
// -1~12 0~13
c[i][j]++;
}
}
// 不取第一个物品的方案数为1
f[1][1][0][0] = 1;
// 取第一个物品的方案数为1
f[1][1][1][c[1][1]] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int u = 0; u <= k; u++) {
for (int v = 0; v <= 13; v++) {
// 不选 (i, j)
f[i][j][u][v] = (f[i][j][u][v] + f[i - 1][j][u][v]) % mod;
f[i][j][u][v] = (f[i][j][u][v] + f[i][j - 1][u][v]) % mod;
// 选 (i, j) v表示选择之后价值的最大值,所以选择的话 c[i][j] = v
if (u > 0 && c[i][j] == v) {
for (int c = 0; c < v; c++) {
f[i][j][u][v] = (f[i][j][u][v] + f[i - 1][j][u - 1][c]) % mod;
f[i][j][u][v] = (f[i][j][u][v] + f[i][j - 1][u - 1][c]) % mod;
}
}
}
}
}
}
int res = 0;
for (int i = 0; i <= 13; i++)
res = (res + f[n][m][k][i]) % mod;
out.println(res);
out.flush();
}
}