Nobita's neighborhood is under attack by Gian's po...
Created on: July 13, 2025
Answered using O4 Mini High by Chat01
Created on: July 13, 2025
Answered using O4 Mini High by Chat01
Nobita's neighborhood is under attack by Gian's powerful robot army.
The only defense is a long protective barrier, which is divided into N sections Each section already has some security cameras installed, which can detect robots at nearby sections.
A security camera at section i can scan all sections j within a distance of at most R meaning it can help in monitoring sections where li - jl ≤ R
The Shield Level of a section is the total number of cameras (including those from neighboring sections) monitoring it.
To stop the robot invasion, Doraemon has K additional cameras that he can place anywhere along the barrier.
The goal is to maximize the weakest section's defense level, ensuring that no section is left vulnerable.
Find the maximum possible defense level for the weakest section.
Input Format
The first line contains an integer, N, denoting the number of sections in the barrier
The next line contains an integer, R, denoting the maximum range a camera can scan.
The next line contains an integer, K, denoting the number of additional cameras available.
Each line i of the N subsequent lines (where 1 <= i <= N ) contains an integer describing Cameras[i].
Constraints
1 <= N <= 10 ^ 5
<= R <= N
0 <= K <= 10 ^ 12
0 <= Cameras[i] <= 10^9
Sample Test Cases
Case 1
Input
Input
5
03534 480
Output:
5
Explanation:
N-5 R-0 K-3
Cameras [534891
Explanation:
Each camera only monitors its own section (R0).
Current weakest section: The 2nd section (only 3 cameras).
We place 2 extra cameras there defense level becomes 5.
Distribute the remaining 1 cameras optimally to other weaker sections ile 3rd one
The minimum defense level across all sections is now 5.
code in java without comments and buffer reader
javaimport java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int R = sc.nextInt(); long K = sc.nextLong(); long[] cams = new long[N]; for (int i = 0; i < N; i++) { cams[i] = sc.nextLong(); } long[] prefix = new long[N]; prefix[0] = cams[0]; for (int i = 1; i < N; i++) { prefix[i] = prefix[i - 1] + cams[i]; } long[] init = new long[N]; for (int i = 0; i < N; i++) { int L = Math.max(0, i - R); int Rg = Math.min(N - 1, i + R); init[i] = prefix[Rg] - (L > 0 ? prefix[L - 1] : 0); } long low = Long.MAX_VALUE; for (long v : init) if (v < low) low = v; long high = low + K; while (low < high) { long mid = (low + high + 1) >>> 1; if (canReach(init, N, R, K, mid)) { low = mid; } else { high = mid - 1; } } System.out.println(low); } static boolean canReach(long[] init, int N, int R, long K, long target) { long used = 0, extra = 0; long[] endAt = new long[N + 1]; for (int i = 0; i < N; i++) { extra -= endAt[i]; long cur = init[i] + extra; if (cur < target) { long need = target - cur; used += need; if (used > K) return false; extra += need; int p = Math.min(N - 1, i + R); int e = Math.min(N - 1, p + R); endAt[e + 1] += need; } } return true; } }
You are given in three integers N. L and R
It is given that a flip flop array is delined as an array of length N such that:
Each element is between Land R
There are no two adjacent values that are equal
No three adjacent values that are increasing or decreasing
Moreover, it is guaranteed that L < R.
Find the total number of possible flip top arrays of size N. Since the answer can be large return it modulo 10^9+7.
Input Format
The first fine contains an integer. N. denoting the length of the array
The next line contains an integet, L. denoting the lower bound.
The next line contains an integer, R. denoting the upper bound
Constraints
3 <= N <= 2000
1 <= Lc = 2000
1 <= R <= 2000
Sample Test Cases
Case 1
Input:
3 1 2
Output
2
Explanation:
N=3, L-1, R-2
The arrays are:
[1, 2, 1]
17, 1, 21
Because for all i, there is no i such that A(i) = A(1 + 1)
There is no i such that [1] subset A [1 + 1] subset A[1+] ]
There is no i such that A[1] > A[1 + 1] ,>A[1-2]
Case: 2
Input:
333
Output:
10
Explanation:
N-3, L-1, R-3
The arrays are:
[1, 2, 1]
[1, 3, 1]
[1, 3, 2]
[2, 1, 2]
[2, 1, 3]
(2, 3, 1]
[2, 3, 2]
[3, 1, 2]
(3, 1, 3]
[3, 2, 3]1
Case 3
Input:
334
Output:
2
code in java without comments and buffer reader such that passes all test cases
javaimport java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int L = sc.nextInt(); int R = sc.nextInt(); int M = R - L + 1; int mod = 1000000007; System.out.println((count(N, M, mod, 1) + count(N, M, mod, -1)) % mod); } static int count(int N, int M, int mod, int p2) { int[] dpPrev = new int[M + 1]; int[] dpCurr = new int[M + 1]; int[] prefix = new int[M + 1]; for (int i = 1; i <= M; i++) dpPrev[i] = 1; for (int i = 2; i <= N; i++) { prefix[0] = 0; for (int j = 1; j <= M; j++) { int v = dpPrev[j] + prefix[j - 1]; if (v >= mod) v -= mod; prefix[j] = v; } boolean less = (p2 == 1 ? i % 2 == 0 : i % 2 == 1); for (int x = 1; x <= M; x++) { if (less) { dpCurr[x] = prefix[x - 1]; } else { int v = prefix[M] - prefix[x]; if (v < 0) v += mod; dpCurr[x] = v; } } int[] t = dpPrev; dpPrev = dpCurr; dpCurr = t; } int res = 0; for (int i = 1; i <= M; i++) { res += dpPrev[i]; if (res >= mod) res -= mod; } return res; } }