CF1129B.Wrong Answer
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Consider the following problem: given an array a containing n integers (indexed from 0 to n−1), find 0≤l≤r≤n−1maxl≤i≤r∑(r−l+1)⋅ai. In this problem, 1≤n≤2000 and ∣ai∣≤106.
In an attempt to solve the problem described, Alice quickly came up with a blazing-fast greedy algorithm and coded it. Her implementation in pseudocode is as follows:
function find_answer(n, a)
Assumes n is an integer between 1 and 2000, inclusive
Assumes a is a list containing n integers: a[0], a[1], ..., a[n-1]
res = 0
cur = 0
k = -1
for i = 0 to i = n-1
cur = cur + a[i]
if cur < 0
cur = 0
k = i
res = max(res, (i-k)*cur)
return res
Also, as you can see, Alice's idea is not entirely correct. For example, suppose n=4 and a=[6,−8,7,−42]. Then, find_answer(n, a) would return 7, but the correct answer is 3⋅(6−8+7)=15.
You told Alice that her solution is incorrect, but she did not believe what you said.
Given an integer k, you are to find any sequence a of n integers such that the correct answer and the answer produced by Alice's algorithm differ by exactly k. Note that although the choice of n and the content of the sequence is yours, you must still follow the constraints earlier given: that 1≤n≤2000 and that the absolute value of each element does not exceed 106. If there is no such sequence, determine so.
考虑如下问题:给定一个包含 n 个整数的数组 a(下标从 0 到 n−1),求
0≤l≤r≤n−1maxl≤i≤r∑(r−l+1)⋅ai.
本题中,1≤n≤2000,且 ∣ai∣≤106。
为解决上述问题,Alice 迅速提出了一种“极快”的贪心算法并实现了它。其伪代码如下:
function find_answer(n, a)
假设 n 是介于 1 和 2000(含)之间的整数
假设 a 是一个包含 n 个整数的列表:a[0],a[1],…,a[n−1]
res = 0
cur = 0
k = -1
for i = 0 to i = n-1
cur = cur + a[i]
if cur < 0
cur = 0
k = i
res = max(res, (i-k)*cur)
return res
如你所见,Alice 的思路并不完全正确。例如,设 n=4 且 a=[6,−8,7,−42],则 find_answer(n, a) 将返回 7,但正确答案是 3⋅(6−8+7)=15。
你告诉 Alice 她的解法是错误的,但她并不相信你的话。
现给定一个整数 k,你需要构造任意一个长度为 n 的整数序列 a,使得该问题的正确答案与 Alice 算法输出的答案之差恰好为 k。注意:虽然你可以自由选择 n 的值以及序列 a 的具体内容,但仍须满足前述约束条件,即 1≤n≤2000,且每个元素的绝对值不超过 106。若不存在满足条件的序列,请判定这一点。
输入格式
The first and only line contains one integer k (1≤k≤109).
第一行且唯一一行包含一个整数 k(1≤k≤109)。
输出格式
If there is no sought sequence, print "-1".
Otherwise, in the first line, print one integer n (1≤n≤2000), denoting the number of elements in the sequence.
Then, in the second line, print n space-separated integers: a0,a1,…,an−1 (∣ai∣≤106).
如果不存在所求序列,则输出 -1。
否则,第一行输出一个整数 n(1≤n≤2000),表示序列中元素的个数。
然后,在第二行输出 n 个以空格分隔的整数:a0,a1,…,an−1(∣ai∣≤106)。
输入输出样例
输入#1
8
输出#1
4 6 -8 7 -42
输入#2
612
输出#2
7 30 -12 -99 123 -2 245 -300
说明/提示
The first sample corresponds to the example given in the problem statement.
In the second sample, one answer is n=7 with a=[30,−12,−99,123,−2,245,−300], in which case find_answer(n, a) returns 1098, while the correct answer is 1710.
第一个样例对应题目描述中给出的例子。
在第二个样例中,一个可行的答案是 n=7,且 a=[30,−12,−99,123,−2,245,−300],此时 find_answer(n, a) 返回 1098,而正确答案为 1710。
输入解题思路,AI测评打分。不知道怎么写?