534B - Covered Path - CodeForces Solution


dp greedy math *1400

Please click on ads to support us..

Python Code:

import sys

def get_int():
    return int(sys.stdin.readline().strip())

def get_ints():
    return map(int,sys.stdin.readline().strip().split())

def get_list():
    return list(map(int,sys.stdin.readline().strip().split()))

def get_string():
    return sys.stdin.readline().strip()


v1,v2 = get_ints()
t,d = get_ints()


    
    

dp = [[float('-inf')]*(1001) for _ in range(t+1)]

for j in range(1000,0,-1):
    if abs(v2-j)<=d:
        dp[t][j] = v2

for i in range(t-1,0,-1):
    for j in range(1000,0,-1):
        for k in range(-d,d+1):
            if j+k>=0 and j+k<=1000:
                dp[i][j] = max(dp[i][j], j+k + dp[i+1][j+k])


print(v1 + dp[2][v1])

C++ Code:

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

#define MX -999999
int step,d,endi,starti;

//struct direct{
//    int s, t;
//    direct(){
//    }
//
//    direct(int _s, int _t){
//        s=_s;
//        t=_t;
//    }
//
//}dir[10000][10000];

int dp[1000][1000];

int call(int start, int t){

    if(start-(step-t)*d>endi)
        return MX;

    if(dp[start][t]!=-1)
        return dp[start][t];

    int maxi=MX;

    if(t>=step && start==endi)
        return 0;
    if(t>=step)
        return MX;


    for(int i=start;i<=start+d;i++){
        if(i+call(i,t+1)>maxi){
            maxi=i+call(i,t+1);
//            dir[start][t]= direct(i,t+1);
        }
    }

    for(int i=start;i>=start-d;i--){
        if(i>0){
            if(i+call(i,t+1)>maxi){
                maxi=i+call(i,t+1);
//                dir[start][t]= direct(i,t+1);
            }
        }
    }

    return dp[start][t]=maxi;

}

//void print_path(int s,int t){
//    if(dir[s][t].s==-1)
//        return;
//
//    int next_s= dir[s][t].s;
//    int next_i=dir[s][t].t;
//
//    cout<<next_s<<endl;
//
//    print_path(next_s, next_i);
//
//
//}

main(){
    cin>> starti>> endi>>step>>d;
//    memset(dir, -1, sizeof dir);
    memset(dp, -1, sizeof dp);

    int dist= starti+ call(starti,1);


//    cout<<starti<<endl;
//    print_path(starti,1);
    cout<< dist<< endl;


}


Comments

Submit
0 Comments
More Questions

901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract