Wrong Answer

Wrong Answer

Created by LXC on Wed Apr 10 14:40:13 2024

https://codeforces.com/problemset/problem/1129/B

ranting: 2000

tag: constructive algorithms

problem

Alice做到了这样一道题:

给定一个序列$a$,有$n$个元素,编号从$0$到$n-1$。求$\max\limits_{0 \leq l \leq r \leq n-1} \sum\limits_{l \leq i \leq r} (r-l+1) \cdot a_i$。

$|a_i| \leq 10^6,n \leq 2000$

Alice觉得这题太水了,很快就给出了解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
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

聪明的你一定发现了,Alice的解法是错误的。例如对于序列$a = [6, -8, 7, -42]$,Alice的程序得出的结果是$7$,而正确答案应该是$3 \cdot (6-8+7) = 15$。

于是你决定HACK掉Alice的解法。

请给出一组数据,使得正确答案-Alice的答案=$k$,你的数据必须满足$n \leq 2000,|a_i| \leq 10^6$。

$k \leq 10^9$

solution

我们发现当前缀和出现负数,则Alice的做法是直接舍弃。

我们可以构造$a_1=-1, a_2, a_3, a_{2000}$。这样Alice的答案是$(n-1)(\sum a_i - a_1)$,真实的答案是$n\sum a_i$
相差为k,$n\sum a_i - (n-1)(\sum a_i - a_1) = k$,均匀的分配给$a_i, i>1$

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <bits/stdc++.h>
#define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
#define MOD 998244353
using namespace std;

random_device seed;
ranlux48 engine(seed());
int random(int l, int r) {
uniform_int_distribution<> distrib(l, r);
return distrib(engine);
}
template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}


void sol() {
ll k;
cin >> k;
ll t = (k+2000)/1999;
vector<int> ans;
ans.push_back(-1);
for (int i=0; i<1999; i++) {
if (i<(k+2000)%1999) {
ans.push_back(t+1);
} else {
ans.push_back(t);
}
}
cout << ans.size() << "\n";
for (int i:ans) {
cout << i << " ";
}
cout << "\n";
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}