Category : algorithm

question sample input/output first line is N K A B second line is s[i] for(int i=1;i<=N;i++){ for(int j=1;j<=K;j++){ long long tmp=A*max(0,B-abs(i-dp[i-1][j-1].path[dp[i-1][j-1].path.size()-1]); if(s[i]+dp[i-1][j-1]+tmp>dp[i-1][j]){ dp[i][j]=s[i]+dp[i-1][j-1]+tmp; }else{ dp[i][j]=dp[i-1][j]; } } I solve this problem using dp like 0/1 knapsack problem. But the case A*max(0,B-(xi+1-xi)) makes dp can’t work. I think my substructure of dp is wrong. Is there ..

Read more

Given F(0) = 0, F(1) = 1, F(2) = 2, F(3)=3 and F(n) = (F(n−1)−F(n−3) +F(n−4)))%m. Task is to implement an efficient algorithm to compute F(n) where n is a 10^5 bit number given in the binary representation and m < 10^6 #include<bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { /* input ..

Read more

The first line of the standard input contains the number of elements(N) (1<=N<=100). The next lines contain a number (M) (1<=M<=300), as we know, there can be maximum 100 "M" elements. We need to find the smallest number, that is smaller than 120. In the first line of the standard output, we need to print ..

Read more