codeforces 710E Generate a String dp or 搜索

题意:
告诉你移动左移or右移一个cost x
翻二倍消耗y 问你到达n的最小花费
解:

一、dp

1.奇数只能从左or右转移过来(默认右的最优是从前面跳过来的)
2.偶数的话(左转移or前面直接跳过来的)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX=10000005;
ll dp[MAX];
 
int main()
{
    ll n,x,y;
    scanf("%I64d%I64d%I64d",&n,&x,&y);
 
    dp[1]=x;
    for(int i=2; i<=n; i++)
        if(i%2)
            dp[i]=min(dp[i-1]+x,dp[i/2+1]+x+y);
        else
            dp[i]=min(dp[i-1]+x,dp[i/2]+y);
 
    printf("%I64d",dp[n]);
    return 0;
}

二、搜索

1.奇数:要么左要么右 一步

2.偶数 :从x/2  (1)要么跳(2)要么一步一步走

CF大佬的

#AuthorProblemLangVerdictTimeMemorySentJudged  
20057525Contestant:
firehawk
710E - 19GNU C++11Accepted15 ms2036 KB2016-08-22 18:02:332016-08-23 23:49:00Add to favouritesCompare
#define INF 1e9
#define MAX 100010
const lld mod = 1e9 + 7;
lld n, x, y;
lld dfs(lld i){
	if(i==0) return 0;
	if(i==1) return x;
	if(i&1){
		return x + min(dfs(i+1), dfs(i-1));
	}
	else{
		return min(y,(i/2)*x) + dfs(i/2);
	}
}
 
main(){
	lld ans = 0;
	
	cin >> n >> x >> y;
	if(n%2!=0){
		ans  = x + min(dfs(n+1), dfs(n-1));
	}
	else{
		ans = dfs(n/2) + min(x*(n/2),y);
	}
	cout << ans << endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值