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>我们遇到了一个错误:当我们尝试使用pip安装requirements.txt文件时,出现了错误提示。 错误信息指出:无效的需求,因为它期望在依赖项说明符的开始是一个包名,而我们给的是一个路径。 提示告诉我们:看起来像是一个路径,且该路径存在。我们提供的参数(.\requirements.txt)似乎是一个需求文件。如果是这种情况,应该使用'-r'标志来安装其中指定的包。 因此,正确的命令应该是: pip install -r .\requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple 解释错误原因: 在pip install命令中,如果要安装一个需求文件(通常命名为requirements.txt)中的所有包,我们需要使用-r选项来指定该文件。 而直接传递文件路径(如.\requirements.txt)会被pip解释为要安装一个名为“requirements.txt”的包,这显然是不存在的,因为包名不能包含点(除了在版本号中)且一般不以.txt结尾。 修正后的命令: pip install -r .\requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple 这样,pip就会从requirements.txt文件中读取所有的包,并使用清华镜像源进行安装。 注意:在Windows系统中,路径分隔符可以是反斜杠,但也可以使用正斜杠。为了更好的可移植性(如在跨平台脚本中),我们可以考虑使用正斜杠,但在Windows命令提示符中,反斜杠也是可以的。 另外,如果当前目录就是requirements.txt所在的目录,我们也可以直接写文件名,但为了明确,使用.\requirements.txt也是可以的。 所以,正确的命令就是上面给出的带有-r标志的命令。 下面我们给出一个Python代码示例,该代码使用subprocess模块来运行这个pip命令,并捕获输出和错误。但请注意,执行pip命令通常不需要通过Python程序,直接在命令行中运行即可。不过,如果你需要在Python脚本中安装依赖,可以这样做: 但请注意:在运行的Python环境中安装包可能会影响当前环境,尤其是如果你在虚拟环境中运行的话。另外,由于安装包需要权限,可能需要以管理员权限运行脚本(不推荐),或者使用虚拟环境。 以下代码示例演示了如何在Python中执行这个pip命令: 注意:在实际应用中,我们应该谨慎地在代码中执行外部命令,特别是安装包的操作,因为这可能会改变环境。 不过,根据问题,我们只是解决命令行中的问题,所以这里提供代码仅作为示例,说明如何用Python调用pip命令来安装requirements.txt中的包。 </think>### 问题分析: 这个错误是因为 `pip install` 命令直接使用了 `.\requirements.txt` 路径作为参数,但 `pip` 要求当安装来自 requirements 文件的依赖时,必须使用 `-r` 标志显式声明。错误信息明确指出路径存在但格式不正确,提示应该使用 `-r` 标志。 ### 正确解决方法: ```bash pip install -r .\requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple ``` ### 错误原因详解: 1. **无效参数格式** `pip install` 后直接跟文件路径会被解释为要安装名为 `requirements.txt` 的包(这不是合法包名) 2. **requirements 文件规范** `requirements.txt` 是包含多个依赖项的清单文件,必须通过 `-r` 标志显式声明: ```bash pip install -r <文件路径> ``` 3. **路径符号建议** Windows 中建议使用: - 正斜杠:`./requirements.txt` - 反斜杠转义:`.\\requirements.txt` - 或直接使用文件名(当文件在当前目录时) ### Python 解决方案(自动安装依赖): 如果需要用 Python 脚本自动安装依赖: ```python import subprocess import sys def install_dependencies(): try: subprocess.run([ sys.executable, "-m", "pip", "install", "-r", "requirements.txt", "-i", "https://pypi.tuna.tsinghua.edu.cn/simple" ], check=True) print("✅ 依赖安装成功") except subprocess.CalledProcessError as e: print(f"❌ 安装失败: {e}") if __name__ == "__main__": install_dependencies() ``` ### 注意事项: 1. 确保 `requirements.txt` 文件在当前工作目录 2. 路径包含空格时需用引号包裹:`-r "path with spaces/requirements.txt"` 3. 检查文件内容有效性(无空行/错误包名) --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值