Pious works as a parcel delivery agent in a large ...
생성일: 2025년 7월 13일
답변에 사용된 모델 O4 Mini High by Chat01
생성일: 2025년 7월 13일
답변에 사용된 모델 O4 Mini High by Chat01
Pious works as a parcel delivery agent in a large warehouse with N parcels placed in a row from 1 to N. Each parcel has a fixed position, but Pious's special parcel is at position K.
Pious receives R delivery requests one by one. In the i-th (1<=1<=R) request. Pious needs to move the parcel at position A[i] to either the first position (1) or the last position (N) in the row
After each request Pious will calculate the total number of distinct positions the special parcel can occupy and store it in an array Arr.
Find the sum of values stored in Arr. Since the answer can be large return it modulo 10^9+7.
Input Format
The first line contains an integer. N, denoting the number of parcels.
The next line contains an integer, K. denoting the initial position of Pious's special parcel.
The next line contains an integer, R, denoting the number of delivery requests.
Each line i of the R subsequent lines (where 1 < i <= R) contains an integer describing A[i].
Constraints
2 <= N <= 10 ^ 9
1 <= K <= N
1 <= R <= 10 ^ 5
1 < A[i] <= N
Sample Test Cases
Case 1
Input:
5
2
3
1
2
3
Output
10
Explanation:
N-5, K-2, R-3, A=[1,2,3]
1-1: Me have 2 different arrays: [1,2,3,4,5] and [2,3,4,5,1] where 2 is at 2 different positions. Plous special parcel could be at 2 or 12 distinct positions.
1-2: [1,2,3,4,5] from previous i(i.e isl) becomes [2,1,3,4,5] or [1,3,4,5,21 (2,3,4,5,1) berrmes [3,2,4,5,1] or [2,4,5,1,3], 50, 2 is at 3 different positions.
1-3: All these 4 arrays contribute to 5 different positions for 2 when moving parcel at position A[3]:3.
so 2+3+5=10.
code in java without comment and buffer reader
javaimport java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long N = sc.nextLong(); long K = sc.nextLong(); int R = sc.nextInt(); List<long[]> curr = new ArrayList<>(); curr.add(new long[]{K, K}); long ans = 0; final long M = 1000000007; for (int i = 0; i < R; i++) { long A = sc.nextLong(); List<long[]> next = new ArrayList<>(); for (long[] iv : curr) { long l = iv[0], r = iv[1]; if (r < A) { next.add(new long[]{l + 1, r + 1}); } else if (l > A) { next.add(new long[]{l, r}); } else { if (l <= A - 1) next.add(new long[]{l + 1, A}); next.add(new long[]{1, 1}); if (r >= A + 1) next.add(new long[]{A + 1, r}); } } for (long[] iv : curr) { long l = iv[0], r = iv[1]; if (r < A) { next.add(new long[]{l, r}); } else if (l > A) { next.add(new long[]{l - 1, r - 1}); } else { if (l <= A - 1) next.add(new long[]{l, A - 1}); next.add(new long[]{N, N}); if (r >= A + 1) next.add(new long[]{A, r - 1}); } } curr = merge(next); long cnt = 0; for (long[] iv : curr) { cnt = (cnt + iv[1] - iv[0] + 1) % M; } ans = (ans + cnt) % M; } System.out.println(ans); } static List<long[]> merge(List<long[]> a) { if (a.isEmpty()) return a; a.sort(Comparator.comparingLong(o -> o[0])); List<long[]> r = new ArrayList<>(); long s = a.get(0)[0], e = a.get(0)[1]; for (int i = 1; i < a.size(); i++) { long cs = a.get(i)[0], ce = a.get(i)[1]; if (cs <= e + 1) { e = Math.max(e, ce); } else { r.add(new long[]{s, e}); s = cs; e = ce; } } r.add(new long[]{s, e}); return r; } }