A. Requirements


A. Requirements
Code (90%)
You can write your code in Java, Python, C, or C++. The time limit may vary among different
languages, depending on the performance of the language. Your code must be a complete excutable
program instead of only a function. We guarantee test data strictly compliance with the requirements
in the description, and you do not need to deal with cases where the input data is invalid.
No AI Assistance or Plagiarism: All code must be your own. The use of AI tools (e.g., ChatGPT,
GitHub Copilot) or copying from external sources or peers is strictly forbidden.
Violations of the plagiarism rules will result in 0 points or even failure of this course.
Libraries in this assignment:
• For C/C++, you can only include standard library.
• For Java, you can only import java.util.*
• For Python, you can only import standard library. In other words, you cannot import libraries
such as numpy.
We provide an example problem to illustrate the information above better.
Report (10%)
You also need to write a report in pdf type to explain the following:
• What are the possible solutions for the problem?
• How do you solve this problem?
• Why is your solution better than others?
Please note that the maximum number of pages allowed for your report is 5 pages.
Remember that the report is to illustrate your thinking process. Keep in mind that your report is
supposed to show your ideas and thinking process. We expect clear and precise textual descriptions
in your report, and we do not recommend that you over-format your report.
B. Example Problem: A + B Problem
Description
Given 2 integers A and B, compute and print A + B
Input
Two integers in one line: A, and B
Output
One integer: A + B
Sample Input 1
1 2
Sample Output 1
3
1Problem Scale & Subtasks
For 100% of the test cases, 0 ≤ A, B ≤ 10
6
Solutions
Java
import java . util .*;
public class Example {
public static void main ( String [] args ) {
int a, b;
Scanner scanner = new Scanner ( System .in );
a = scanner . nextInt ();
b = scanner . nextInt ();
scanner . close ();
System . out . println (a + b);
}
}
Python
AB = input (). split ()
A, B = int (AB [0]) , int (AB [1])
print (A + B)
C
# include <stdio .h>
int main ( int argc , char * argv [])
{
int A, B;
scanf ("%d%d", &A, &B);
printf ("%d\n", A + B);
return 0;
}
C++
# include <iostream >
int main ( int argc , char * argv [])
{
int A, B;
std :: cin >> A >> B;
std :: cout << A + B << std :: endl ;
return 0;
}
C. Submission
After ffnishing this assignment, you are required to submit your code to the Online Judge System
(OJ), and upload your .zip package of your code ffles and report to BlackBoard.
C.1 Online Judge
Once you have completed one problem, you can submit your code on the page on the Online Judge
platform (oj.cuhk.edu.cn, campus only) to gain marks for the code part. You can submit your
solution of one problem for no more than 80 times.
2After you have submitted your program, OJ will test your program on all test cases and give you a
grade. The grade of your latest submission will be regarded as the ffnal grade of the corresponding
problem. Each problem is tested on multiple test cases of different difffculty. You will get a part of
the score even if your algorithm is not the best.
Note: The program running time may vary on different machines. Please refer to the result of
the online judge system. OJ will show the time and memory limits for different languages on the
corresponding problem page.
If you have other questions about the online judge system, please refer to OJ wiki (campus network
only). If this cannot help you, feel free to contact us.
C.2 BlackBoard
You are required to upload your source codes and report to the BlackBoard platform. You need
to name your ffles according to the following rules and compress them into A1_<Student ID>.zip :
A1_ < Student ID >. zip
|-- A1_P1_ < Student ID >. java /py/c/ cpp
|-- A1_P2_ < Student ID >. java /py/c/ cpp
|-- A1_Report_ < Student ID >. pdf
For Java users, you don’t need to consider the consistency of class name and ffle name.
For example, suppose your ID is 123456789, and your problem 1 is written in Python, problem 2 is
written in Java then the following contents should be included in your submitted A1_123456789.zip:
A1_123456789 .zip
|-- A1_P1_123456789 .py
|-- A1_P2_123456789 . java
|-- A1_Report_123456789 . pdf
C.3 Late Submissions
Submissions after Sept. 29 2024 23:59:00(UTC+8) would be considered as LATE.
The LATE submission page will open after deadline on OJ.
Submisson time = max{latest submisson time for every problem, BlackBoard submisson time}
There will be penalties for late submission:
• 0–24 hours after deadline: ffnal score = your score×0.8
• 24–72 hours after deadline: ffnal score = your score×0.5
• 72+ hours after deadline: ffnal score = your score×0
FAQs
Q: My program passes samples on my computer, but not get AC on OJ.
A: Refer to OJ Wiki Q&A
3CSC3100 Data Structures Fall 2024
Programming Assignment 1
Yige Jiang: yigejiang@link.cuhk.edu.cn
Chunxu Lin: 221012033@link.cuhk.edu.cn
Due: Sept. 29 2024 23:59:00
Assignment Link: https://oj.cuhk.edu.cn/d/csc3100_2024_fall/homework/66e704de6605d3c4e7f63c35
1 Array Problem (40% of this assignment)
1.1 Description
You are given a sequence of integers ai of length n. Additionally, you are given m operations to perform
on this sequence. Each operation is one of the following:
- Given k, x, y, c, update the value of ak using the formula:
ak =
(x
2 + ky + 5x) mod P

∗ c
Obviously, the resulting value will be between [1 − P, P − 1], where c = ±1.
- Query the sum of all elements in the sequence, i.e., compute:
Xn
i=1
ai
- Query the maximum number of distinct values in the sequence if each element is multiplied by either
1 or −1 (you can ffip the sign of some elements and count the maximum number of distinct numbers).
Your task is to process these operations efffciently.
1.2 Input
The ffrst line contains three integers n, m and P (1 ≤ n, m ≤ 10
6
, 1 ≤ P ≤ 10
6
) — the length of the
sequence, the number of operations and the divisor in modulo operation, respectively.
The second line contains n integers, representing the original value of the array a, denoted as a1, a2, ..., an
(−P < a[i] < P).
Each of the next m lines contains a description of one of the following types of operations:
- For update operations, the line will contain ffve integers 1, k, x, y, c (1 ≤ k ≤ n, 0 ≤ x, y <
min(P, 2000), c ∈ {−1, 1}).
- For sum queries, the line will contain a single integer 2.
- For distinct value queries, the line will contain a single integer 3.
41.3 Output
For each sum query, output the sum of all elements in the array.
For each distinct value query, output the maximum number of distinct values that can be obtained by
multiplying each element by either 1 or −1.
Sample Input 1
5 5 3
0 0 0 1 -2
3
1 5 1 2 -1
3
1 3 2 1 1
3
Sample Output 1
3
3
4
Sample Input 2
10 10 5
-1 -2 2 -3 2 0 -4 3 3 -3
3
1 2 4 4 -1
2
1 3 1 2 -1
3
2
3
1 2 4 4 1
3
1 4 4 0 -1
Sample Output 2
7
-5
8
-9
8
8
Sample Input 3
in ’ array_sampleinput3 .in ’
Sample Output 3
in ’ array_sampleinput3 .ans ’
Problem Scale & Subtasks
For about 60% test cases, distinct value queries are not evolved.
Test Case No. Constraints
1-4 n, m ≤ 20
5-7 n, m ≤ 5 × 10
3
8-10 n, m ≤ 10
6
Hint
If you encounter a TLE, and your algorithm’s time complexity is efffcient, try optimizing your I/O
operations.
2 List (50% of this assignment)
2.1 Description
Given an array, which is a permutation of size n (an array of size n where every integer from 1 to n
appears exactly once), we perform q operations. During the i-th operation, we perform the following:
• Choose any subarray that contains at least 2 elements.
5• Split it into two non-empty arrays.
• Obtain two integers li and ri
, where li
is the left most element in the left part of the split,
and ri
is the right most element in the right part of the split.
For example, if the initial array is [6, 3, 4, 1, 2, 5], we perform the following operations:
1. Choose the array [6, 3, 4, 1, 2, 5] and split it into [6, 3] and [4, 1, 2, 5]. Then, l1 = 6 and r1 = 5.
2. Choose the array [4, 1, 2, 5] and split it into [4, 1, 2] and [5]. Then, l2 = 4 and r2 = 5, resulting
in the arrays [6, 3], [4, 1, 2], and [5].
3. Choose the array [4, 1, 2] and split it into [4] and [1, 2]. Then, l3 = 4 and r3 = 2, resulting in
the arrays [6, 3], [4], [1, 2], and [5].
Objective. Given two integers n and q, along with two sequences [l1, l2, ..., lq] and [r1, r2, ..., rq], a
permutation is called valid if we can perform q operations and generate the given sequences [l1, l2, ..., lq]
and [r1, r2, ..., rq].
Problem. Determine whether a given permutation with q operations is valid.
2.2 Input
1. The first line contains two integers n and q (1 ≤ q < n ≤ 106
).
2. The second line contains a permutation of size n.
3. The third line contains q integers, l1, l2, ..., lq (1 ≤ li ≤ n).
4. The fourth line contains q integers, r1, r2, ..., rq ( 1 ≤ ri ≤ n )
2.3 Output
Output 1 if the given permutation is valid, otherwise output 0.
Sample Input 1
6 3
6 3 4 1 2 5
6 4 4
5 5 2
Sample Output 1
1
Sample Input 2
7 3
7 6 3 4 1 2 5
6 4 4
5 5 2
Sample Output 2
0
Sample Input 3
7 3
6 3 4 1 2 5 7
6 4 4
5 5 2
Sample Output 3
0
Problem Scale & Subtasks
For 100% of the test cases, 1 ≤ q < n ≤ 106
.
6Test Case No. Constraints
1-2 n ≤ 10
3-5 n ≤ 103
6-10 n ≤ 106
Figure 1: Hint2
Hint
Hint1 : For C/C++ and Java users, an int type stores integers range from -2,147,483,648 to 2,147,483,647.
It may be too small for this problem. You need other data types, such as long long for C/C++ and
long for Java. They store integers ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Use scanf("%lld",&n) for C, cin>>n for C++ and n = scanner.nextLong() for Java to get the input
n. And the other operations for long and long long are quite same as int.
Hint2 : The process of Sample Input 1 can be described as in the Figure 1.
Hint3 : This problem can be easily solved by following the above process from bottom to top.
Hint4 : Consider how using the data structure, such as dictionary or list, to store the indices of li and
ri can help solve the problem.

<think>好的,我现在需要帮助用户理解如何通过requirements.txt文件安装Python依赖。用户之前询问了关于导入BGE模型的代码问题,现在他们可能正在设置项目环境,需要确保所有依赖正确安装。 首先,我要回想一下用户之前的对话,他们处理的是自然语言处理模型,特别是使用sentence-transformers库加载BGE模型。所以,他们的requirements.txt可能包含像sentence-transformers、torch、transformers等库。 接下来,用户可能想知道如何正确创建和使用requirements.txt文件。我应该解释这个文件的作用,如何生成它,以及安装依赖的命令。同时,用户可能会遇到常见问题,比如版本冲突或依赖缺失,需要给出解决方法。 还要考虑用户可能的操作环境,比如是否使用虚拟环境,是否使用不同的包管理工具如pip或conda。需要涵盖这些方面,但保持简洁,避免信息过载。 另外,用户可能对requirements.txt的结构不太清楚,比如指定版本号的重要性,或者如何确保依赖的兼容性。需要举例说明,并给出最佳实践建议,比如使用虚拟环境。 最后,需要检查是否有遗漏的步骤,比如安装依赖后如何验证,或者如何处理平台特定的问题,比如CUDA版本的兼容性。不过,可能不需要深入,除非用户提到相关问题。 总结下来,回答应包括: 1. requirements.txt的作用和结构 2. 生成requirements.txt的方法 3. 安装依赖的命令 4. 常见问题及解决方法 5. 最佳实践建议 需要保持语言简洁,步骤清晰,示例明确,帮助用户顺利安装所需依赖,避免他们遇到常见的陷阱。</think>--- ### 一、`requirements.txt` 的作用 `requirements.txt` 是 Python 项目依赖库的清单文件,用于**批量管理第三方库的安装**。通过该文件可以: 1. 确保不同环境(开发/生产)的依赖一致性 2. 快速复现项目运行环境 3. 避免手动逐个安装的繁琐操作 --- ### 二、文件格式规范 ```txt # 标准语法(库名==版本号) sentence-transformers==2.3.1 torch==2.0.1 transformers==4.30.2 # 允许范围版本 numpy>=1.21.0 # 最小版本 pandas<1.5.0 # 最大版本 # 指定安装源 -i https://pypi.tuna.tsinghua.edu.cn/simple # 使用清华镜像源 ``` --- ### 三、生成依赖文件 #### 方式1:自动生成(推荐) ```bash # 导出当前环境所有已安装的包(包含版本) pip freeze > requirements.txt # 仅导出项目直接依赖(需配合 pip-autoremove) pip install pip-autoremove pip-autoremove -L > requirements.txt ``` #### 方式2:手动编写 根据项目实际使用的库手动创建文件,例如: ```txt sentence-transformers>=2.2.0 torch>=1.13.0 --extra-index-url https://download.pytorch.org/whl/cu117 # 指定CUDA版本 ``` --- ### 四、安装依赖 #### 基础安装命令 ```bash pip install -r requirements.txt ``` #### 高级用法 ```bash # 使用清华镜像加速 pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple # 指定虚拟环境安装 conda create -n myenv python=3.8 conda activate myenv pip install -r requirements.txt # 导出为conda环境(需pip2conda工具) pip2conda requirements.txt > environment.yml ``` --- ### 五、常见问题解决 #### 1. 版本冲突 ```bash ERROR: Cannot install packageA==1.0 and packageB==2.0 because these package versions have conflicting dependencies. ``` **解决方案**: - 使用虚拟环境隔离项目 - 手动调整版本号(通过 `pip install package==version` 测试兼容性) #### 2. 找不到包 ```bash ERROR: Could not find a version that satisfies the requirement unknown-package ``` **解决方案**: - 检查包名拼写(区分大小写) - 确认是否需额外索引源(如 `--extra-index-url`) #### 3. 系统兼容性问题 ```bash ERROR: Failed building wheel for tokenizers # 常见于需要编译的库 ``` **解决方案**: - 安装编译工具(如 `sudo apt-get install build-essential`) - 使用预编译版本(如 `pip install --pre torch -f https://download.pytorch.org/whl/nightly/cu117/torch_nightly.html`) --- ### 六、最佳实践 1. **虚拟环境优先** ```bash python -m venv .venv # 创建 source .venv/bin/activate # 激活(Linux/Mac) .venv\Scripts\activate # 激活(Windows) ``` 2. **版本锁定策略** - 开发环境:宽松版本(`numpy>=1.21.0`) - 生产环境:严格版本(`numpy==1.23.5`) 3. **分层依赖管理** ```txt # requirements-core.txt(基础库) numpy==1.24.3 pandas==2.0.1 # requirements-dev.txt(开发工具) -r requirements-core.txt pytest==7.3.1 black==23.3.0 ``` --- ### 七、典型示例文件 ```txt # NLP项目示例 sentence-transformers==2.3.1 torch==2.0.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117 transformers==4.30.2 nltk==3.8.1 scikit-learn==1.2.2 -i https://pypi.tuna.tsinghua.edu.cn/simple ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值