NC229641. Problem B. sophistry
描述
Now, Xiao T wants to maximize the damage to Xiao K, but Xiao T is too bad to solve this problem, so he found you. hope you can help him solve this problem. You only need to find out the maximum damage caused by Xiao T to Xiao K.
输入描述
The first line contains three positive integersThe second line contains positive integers 。, ,
输出描述
Only one number per line is output, indicating the biggest damage caused by Xiao T to Xiao K。
示例1
输入:
5 2 11 8 10 15 23 5
输出:
41
示例2
输入:
20 2 16 20 5 8 2 18 16 2 16 16 1 5 16 2 13 6 16 4 17 21 7
输出:
156
Python3 解法, 执行用时: 136ms, 内存消耗: 18848K, 提交时间: 2023-05-07 16:43:14
from sys import * n,d,m = map(int,stdin.readline().split()) l = n+n+10 ls = [0] * l ls[1:n+1] = map(int,stdin.readline().split()) dp = [0] * l for i in range(n,0,-1): if ls[i] > m: dp[i] = max(dp[i+1],dp[i+d+1]+ls[i]) else: dp[i] = ls[i]+dp[i+1] print(dp[1])
C++(clang++ 11.0.1) 解法, 执行用时: 38ms, 内存消耗: 1944K, 提交时间: 2023-03-25 13:54:13
#include<iostream> using namespace std; int n,d; long long k,a[100005],f[200005]; int main(){ cin>>n>>d>>k; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=n;i>0;i--){ if(a[i]<=k)f[i]+=a[i]+f[i+1]; else f[i]=max(f[i+1],a[i]+f[i+d+1]); } cout<<f[1]; return 0; }