题意:
告诉你移动左移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大佬的
# | Author | Problem | Lang | Verdict | Time | Memory | Sent | Judged | ||
---|---|---|---|---|---|---|---|---|---|---|
20057525 | Contestant: firehawk | 710E - 19 | GNU C++11 | Accepted | 15 ms | 2036 KB | 2016-08-22 18:02:33 | 2016-08-23 23:49:00 | Compare |
#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;
}