Code this in java
Создано: 12 июля 2025 г.
Создано: 12 июля 2025 г.
pythonfrom PIL import Image import matplotlib.pyplot as plt img = Image.open('/mnt/data/17523127315957391627349811000387.jpg') plt.imshow(img) plt.axis('off') (-0.5, 4095.5, 3071.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img.crop((250,180,800,1200))) plt.axis('off') (-0.5, 549.5, 1019.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img.crop((350,250,1100,1900))) plt.axis('off') (-0.5, 749.5, 1649.5, -0.5)
pythonimg.size (4096, 3072)
pythoncrop = img.crop((1500,200,2900,2800)) plt.figure(figsize=(6,10)) plt.imshow(crop) plt.axis('off') (-0.5, 1399.5, 2599.5, -0.5)
pythonplt.figure(figsize=(6,6)) plt.imshow(crop.crop((0,0,1400,700))) plt.axis('off') (-0.5, 1399.5, 699.5, -0.5)
pythonplt.figure(figsize=(6,6)) plt.imshow(crop.crop((0,500,1400,1200))) plt.axis('off') (-0.5, 1399.5, 699.5, -0.5)
pythonplt.figure(figsize=(6,6)) plt.imshow(crop.crop((0,900,1400,1500))) plt.axis('off') (-0.5, 1399.5, 599.5, -0.5)
pythonimg2 = Image.open('/mnt/data/17523129261337587811820463745918.jpg') plt.imshow(img2) plt.axis('off') (-0.5, 4095.5, 3071.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img2.crop((1500,100,2700,2200))) plt.axis('off') (-0.5, 1199.5, 2099.5, -0.5)
pythonplt.figure(figsize=(6,6)) plt.imshow(img2.crop((1500,950,2700,1850))) plt.axis('off') (-0.5, 1199.5, 899.5, -0.5)
pythonplt.figure(figsize=(6,6)) plt.imshow(img2.crop((1500,1850,2700,2800))) plt.axis('off') (-0.5, 1199.5, 949.5, -0.5)
pythonimg3 = Image.open('/mnt/data/17523129808021070562433759136027.jpg') plt.imshow(img3) plt.axis('off') (-0.5, 4095.5, 3071.5, -0.5)
pythonplt.figure(figsize=(6,8)) plt.imshow(img3.crop((1500,400,2700,2100))) plt.axis('off') (-0.5, 1199.5, 1699.5, -0.5)
pythonplt.figure(figsize=(6,8)) plt.imshow(img3.crop((1500,1250,2700,2800))) plt.axis('off') (-0.5, 1199.5, 1549.5, -0.5)
Below is a clean, self-contained solution in Java that follows the rules you showed on-screen:
javaimport java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { private static final int MOD = 1_000_000_007; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); /* ---------- input ---------- */ int N = Integer.parseInt(br.readLine().trim()); // array size int L = Integer.parseInt(br.readLine().trim()); // minimum index gap int R = Integer.parseInt(br.readLine().trim()); // maximum index gap int X = Integer.parseInt(br.readLine().trim()); // required sum int[] A = new int[N]; for (int i = 0; i < N; ++i) { A[i] = Integer.parseInt(br.readLine().trim()); } /* ---------- DP ---------- dp[i][s] = # of “interesting” **increasing** subsequences that end at index i and have sum s. A subsequence can extend from an earlier j if: • A[j] ≤ A[i] (non-decreasing values) • L ≤ i − j ≤ R (index-gap constraint) --------------------------------*/ long[][] dp = new long[N][X + 1]; long answer = 0; for (int i = 0; i < N; ++i) { // single-element subsequence if (A[i] <= X) { dp[i][A[i]] = 1; } // extend from previous valid positions for (int j = Math.max(0, i - R); j <= i - L; ++j) { if (A[j] > A[i]) continue; // must stay non-decreasing for (int s = 1; s + A[i] <= X; ++s) { long ways = dp[j][s]; if (ways != 0) { int newSum = s + A[i]; dp[i][newSum] = (dp[i][newSum] + ways) % MOD; } } } answer = (answer + dp[i][X]) % MOD; // collect all ending at i whose sum==X } System.out.println(answer); } }
dp[i][s]
counts all valid subsequences finishing at i
whose element-sum is s
.i
if A[i] ≤ X
.j
with the required distance (L ≤ i-j ≤ R
) and non-decreasing value (A[j] ≤ A[i]
), extend each subsequence ending at j
by appending A[i]
.O(N · (R-L+1) · X)
(≤ 500·500·1000 ≈ 2.5·10^8
worst-case, ok for the given limits).O(N·X)
(500·1001 ≈ 0.5 M
integers).The algorithm respects every rule in the prompt:
A[j] ≤ A[i]
).L ≤ distance ≤ R
).== X
).Compile (javac Main.java
) and run (java Main
) with input formatted exactly as shown in your screenshots.
Only tell the function according to the temple of code is given only the function in that code template
javastatic int get_ans(int N, int L, int R, int X, List<Integer> A) { final int MOD = 1_000_000_007; long[][] dp = new long[N][X + 1]; long ans = 0; for (int i = 0; i < N; i++) { int ai = A.get(i); // start a new single-element subsequence if (ai <= X) dp[i][ai] = 1; // extend valid subsequences ending within [i-R, i-L] for (int j = Math.max(0, i - R); j <= i - L; j++) { if (A.get(j) > ai) continue; // maintain non-decreasing order for (int s = 1; s + ai <= X; s++) { long ways = dp[j][s]; if (ways == 0) continue; dp[i][s + ai] = (dp[i][s + ai] + ways) % MOD; } } ans = (ans + dp[i][X]) % MOD; // count subsequences whose sum == X } return (int) ans; }