Pious works as a parcel delivery agent in a large ...

作成日: 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

思考

Clarifying the requirements

It seems like the user wants Java code that reads input and prints output using something like Scanner, avoiding BufferReader and comments. They mentioned "Scanner with nextInt", which suggests they want a simple, slow input/output method for an array length R (up to 1e5). The task likely involves computing distinct positions a special parcel can occupy after each move, so I need to focus on deriving the correct formula while keeping things efficient. Let me work through the logic here.

Analyzing the move sequence

So, Pious can move the parcel to either the front or back each time, meaning at each step, the parcel's position can change depending on previous moves. The challenge is to count the distinct positions the special parcel can occupy through all possible sequences of choices for each step. Initially, the special parcel is at position K. For each move, we compute the new possible positions and then calculate the number of distinct ones. This process needs to be efficient given the constraints on N and R, so an O(R) solution is required.

Analyzing position shifts

I've got to examine whether moving parcels always forms a contiguous block. Initially, with the special parcel at position K, each shift either moves the parcel to the front or back. If the special parcel isn't moved, it shifts by +1 or -1 based on the relative order. If the special parcel is moved, it goes to either position 1 or N. When another parcel is moved, I need to carefully account for shifts and how they impact the special parcel’s position, considering cases where it's either greater or less than the moved parcel’s position.

Analyzing moves and positions

I'm analyzing how the positions of parcels evolve with each move and whether they always form a contiguous interval. Starting with the special parcel, after each move it can either shift one position or remain in its original spot. After several moves, the positions might form two distinct intervals rather than a single contiguous block. The goal is to track the possible range of positions at each step and understand how gaps or merged intervals might emerge. I suspect that after enough moves, all positions could eventually merge into one interval.

Thought for 54 seconds

回答

java
import 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; } }

このQ&Aを共有