#P15562. [CCPC 2025 哈尔滨站] 01 背包

    ID: 17483 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2025Special JudgeCCPC哈尔滨

[CCPC 2025 哈尔滨站] 01 背包

Problem Description

The 0/1 knapsack problem is a classic combinational optimization problem in algorithm competitions. Little w has learned a greedy algorithm to solve this problem. The definition of the 0/1 knapsack problem and Little w's greedy algorithm are as follows.

0/1 Knapsack Problem\textbf{0/1 Knapsack Problem}:

Given nn items. The weights of the items are positive integers w1,w2,,wnw_1,w_2,\ldots,w_n, and the values of the items are positive integers v1,v2,,vnv_1,v_2,\ldots,v_n. You are also given the knapsack capacity WW. You need to choose x1,x2,,xnx_1,x_2,\ldots,x_n (1in\forall 1 \le i \le n, xi{0,1}x_i \in \{0,1\}), such that:

i=1nwixiW\sum_{i=1}^n w_ix_i \le W

and maximize:

V=i=1nvixiV = \sum_{i=1}^n v_ix_i

Greedy Algorithm\textbf{Greedy Algorithm}:

  1. Sort the nn items in descending order of viwi\frac{v_i}{w_i}. If viwi\frac{v_i}{w_i} is the same, sort by wiw_i in descending order.
  2. Set an initial variable W0W_0 to 00, and enumerate ii from 11 to nn. If W0+wiWW_0 + w_i \le W, then set xi1,W0W0+wix_i \leftarrow 1, W_0 \leftarrow W_0 + w_i; otherwise set xi0x_i \leftarrow 0.
  3. After the enumeration, you obtain the required x1,x2,,xnx_1,x_2,\ldots,x_n and VV.

Of course, you know this algorithm is incorrect, but Little w does not believe it. Even if you give Little w some counterexamples, Little w still thinks that under many different WW, this algorithm can still produce the optimal VV. So now you want to construct a set of w1,w2,,wnw_1,w_2,\ldots,w_n and v1,v2,,vnv_1,v_2,\ldots,v_n such that:

  1. 2WWlim\forall 2 \le W \le W_{lim} (WlimW_{lim} is a given constant), Little w's algorithm cannot obtain the optimal VV.
  2. Under condition 11, make nn as small as possible.
  3. Under conditions 1,21,2, make max(w1,w2,,wn)\max(w_1,w_2,\ldots,w_n) as small as possible.
  4. Under conditions 1,2,31,2,3, make max(v1,v2,,vn)\max(v_1,v_2,\ldots,v_n) as small as possible.

Now you need to construct a 0/1 knapsack instance that satisfies the requirements above to convince Little w. Can you do it? If there are multiple construction methods, you may output any one.

Input Format

The input consists of one line containing one integer WlimW_{lim} (2Wlim5×1032 \le W_{lim} \le 5 \times 10^3), representing the upper bound of WW.

Output Format

The first line contains one integer nn (1n1041 \le n \le 10^4), representing the number of items in the constructed 0/1 knapsack instance.

The second line outputs nn integers w1,w2,,wnw_1,w_2,\ldots,w_n (1wiWlim1 \le w_i \le W_{lim}), representing the item weights.

The third line outputs nn integers v1,v2,,vnv_1,v_2,\ldots,v_n (1vi1091 \le v_i \le 10^9), representing the item values.

It can be proven that under the given problem and input constraints, a solution within the given constraints always exists.

2
2
1 2
2 3

Hint

Translated by ChatGPT 5