CF1130E Wrong Answer
作者:互联网
构造题+数学
题意是要求一个序列中最大的∑(i>=l且i<=r) (r-l+1)*a[i]的值,题目给出代码,要你hack这份代码,使他的答案与正确答案相差k
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
考虑在第一个数放一个负数-1,之后的1999个数之和为sum,
则他代码的答案为 sum*1999
正确答案为 (sum-1)*2000
所以 (sum-1)*2000-sum*1999=k
化简得 sum=k+2000
之后将sum均分个之后的1999个数即可
#include <bits/stdc++.h> using namespace std; int k,sum; int main() { scanf("%d",&k); sum=k+2000; printf("2000\n"); int t,h; t=sum/1998; h=sum-1998*t; printf("-1 "); for (int i=1;i<=1998;i++) printf("%d ",t); printf("%d\n",h); }
标签:cur,int,res,sum,Wrong,CF1130E,1999,2000,Answer 来源: https://www.cnblogs.com/huangchenyan/p/10500273.html