Codeforces Round #345 (Div. 2) A. Joysticks dp

本文介绍了一个关于两个游戏控制器和一个充电器的问题。每个控制器在连接到充电器时每分钟增加1%的电量,在未连接时则减少2%的电量。文章提供了一种使用动态规划求解的方法,并给出了一段C++代码实现。

A. Joysticks

题目连接:

http://www.codeforces.com/contest/651/problem/A

Description

Friends are going to play console. They have two joysticks and only one charger for them. Initially first joystick is charged at a1 percent and second one is charged at a2 percent. You can connect charger to a joystick only at the beginning of each minute. In one minute joystick either discharges by 2 percent (if not connected to a charger) or charges by 1 percent (if connected to a charger).

Game continues while both joysticks have a positive charge. Hence, if at the beginning of minute some joystick is charged by 1 percent, it has to be connected to a charger, otherwise the game stops. If some joystick completely discharges (its charge turns to 0), the game also stops.

Determine the maximum number of minutes that game can last. It is prohibited to pause the game, i. e. at each moment both joysticks should be enabled. It is allowed for joystick to be charged by more than 100 percent.

Input

The first line of the input contains two positive integers a1 and a2 (1 ≤ a1, a2 ≤ 100), the initial charge level of first and second joystick respectively.

Output

Output the only integer, the maximum number of minutes that the game can last. Game continues until some joystick is discharged.

Sample Input

3 5

Sample Output

6

Hint

题意

你有两个手机,和一个充电器,如果手机插上充电器,每秒涨%1的电,如果不插充电器,每秒掉%2的电

问你最多能维持多久两个手机都有电。

可以超过100%

题解:

我写的是dp,dp[i][j]表示第一个手机有i的电,第二个手机有j的电最长能够坚持多久

然后特判一下1 1的情况就好了。

代码

#include<bits/stdc++.h>
using namespace std;

int a1,a2;
int dp[320][320];
int vis[320][320];
int solve(int x,int y)
{
    if(x>y)swap(x,y);
    if(x<=0)return 0;
    if(vis[x][y])return dp[x][y];
    vis[x][y]=1;
    dp[x][y]=max(solve(x-2,y+1),solve(x+1,y-2))+1;
    return dp[x][y];
}
int main()
{
    cin>>a1>>a2;
    if(a1==1&&a2==1)return puts("0"),0;
    cout<<solve(a1,a2)<<endl;
}

转载于:https://www.cnblogs.com/qscqesze/p/5253074.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值