P1353 [USACO08JAN]跑步Running

题目描述

奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1 <= N <= 10,000)分钟的晨跑。在每分钟的开始,贝茜会选择下一分钟是用来跑步还是休息。

贝茜的体力限制了她跑步的距离。更具体地,如果贝茜选择在第i分钟内跑步,她可以在这一分钟内跑D_i(1 <= D_i <= 1,000)米,并且她的疲劳度会增加1。不过,无论何时贝茜的疲劳度都不能超过M(1 <= M <= 500)。如果贝茜选择休息,那么她的疲劳度就会每分钟减少1,但她必须休息到疲劳度恢复到0为止。在疲劳度为0时休息的话,疲劳度不会再变动。晨跑开始时,贝茜的疲劳度为0。

还有,在N分钟的锻炼结束时,贝茜的疲劳度也必须恢复到0,否则她将没有足够的精力来对付这一整天中剩下的事情。

请你计算一下,贝茜最多能跑多少米。

输入格式

第1行: 2个用空格隔开的整数:N 和 M

第2…N+1行: 第i+1为1个整数:D_i

输出格式

输出1个整数,表示在满足所有限制条件的情况下,贝茜能跑的最大距离

输入输出样例

输入 #1

5 2
5
3
4
2
10

输出 #1

9

【AC代码】:

#include <bits/stdc++.h>
#define M(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f
#define MOD 10000007
using namespace std;

inline void read(int &x){
    char ch=getchar(),c=ch;
	x=0;
    while(ch<'0' || ch>'9'){
    	 c=ch;
		 ch=getchar();
	}
    while(ch>='0' && ch<='9'){
    	x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
    if(c=='-')x=-x;
}

int s,i,j,a,b;
int dp[10010],sum[10010];

int main(){
    read(a),read(b);
    b=b<<1;
	sum[0]=0;
    for(i=1;i<=a;i++){
        read(s);
		sum[i]=sum[i-1]+s;                         //求前缀和
    }
    for(i=0;i<=a;i++)                                 //从0开始,0为跑步的起点
    	for(j=i;j<=b+i&&j<=a;j+=2)                        //递加2,判断两个数值
		{
        	dp[j]=max(dp[j],dp[i]+sum[(j+i)>>1]-sum[i]);//此刻刚休息完
        	dp[j+1]=max(dp[j+1],dp[j]);                 //此刻无疲劳且已滞留一分钟
    	}
    printf("%d\n",dp[a]);                                        //输出结束时间的值
	return 0;
}
### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值