Sherlock and Cost
Problem Statement :
In this challenge, you will be given an array B and must determine an array A. There is a special rule: For all i, A[i] <= B[i]. That is, A[i] can be any number you choose such that 1 <= A[i] <= B[i]. Your task is to select a series of A[i] given B[i] such that the sum of the absolute difference of consecutive pairs of A is maximized. This will be the array's cost, and will be represented by the variable S below. The equation can be written: S = (E^N )i=2 |A[i]-A[i-1]| For example, if the array B = [1, 2, 3], we know that 1 <= A[1] <= 1, 1 <= A[2] <= 2, and 1 <= A[3] <= 3. Arrays meeting those guidelines are: [1,1,1], [1,1,2], [1,1,3] [1,2,1], [1,2,2], [1,2,3] Our calculations for the arrays are as follows: |1-1| + |1-1| = 0 |1-1| + |2-1| = 1 |1-1| + |3-1| = 2 |2-1| + |1-2| = 2 |2-1| + |2-2| = 1 |2-1| + |3-2| = 2 The maximum value obtained is 2. Function Description Complete the cost function in the editor below. It should return the maximum value that can be obtained. cost has the following parameter(s): B: an array of integers Input Format The first line contains the integer t, the number of test cases. Each of the next t pairs of lines is a test case where: - The first line contains an integer n, the length of B - The next line contains n space-separated integers B[i] Constraints 1 <= t <= 20 1 <= n <= 10^5 1 <= B[i] <= 100 Output Format For each test case, print the maximum sum on a separate line.
Solution :
Solution in C :
In C++ :
#include<math.h>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<map>
#include<ext/hash_map>
#include<ext/hash_set>
#include<set>
#include<string>
#include<assert.h>
#include<vector>
#include<time.h>
#include<queue>
#include<deque>
#include<sstream>
#include<stack>
#include<sstream>
#define MA(a,b) ((a)>(b)?(a):(b))
#define MI(a,b) ((a)<(b)?(a):(b))
#define AB(a) (-(a)<(a)?(a):-(a))
#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ep 0.0000000001
#define pi 3.1415926535897932384626433832795
using namespace std;
using namespace __gnu_cxx;
const int N=1000111;
const int INF=1000000008;
int n,m,i,j,k,l,r,p;
int a[1000],b[1000];
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
cin>>k;
for (i=1;i<=k;i++) a[i]=0;
for (i=k+1;i<=100;i++) a[i]=-INF;
n--;
// for (i=1;i<=15;i++) cout<<a[i]<<" ";cout<<endl;
while (n--)
{
scanf("%d",&k);
p=-1;
for (i=1;i<=k;i++)
{
p=MA(p+1,a[i]);
b[i]=p;
}
p=-INF;
for (i=100;i>=1;i--)
{
p=MA(p+1,a[i]);
b[i]=MA(b[i],p);
}
for (j=k+1;j<=100;j++) b[j]=-INF;
for (j=1;j<=100;j++) a[j]=b[j];
// for (i=1;i<=15;i++) cout<<a[i]<<" ";cout<<endl;
}
p=0;
for (i=1;i<=k;i++) p=MA(p,a[i]);
cout<<p<<endl;
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
public class Solution {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
void solve() throws IOException {
int n = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int dpLow = 0;
int dpHigh = 0;
for (int i = 1; i < n; i++) {
int nextLow = Math.max(dpLow, dpHigh + a[i - 1] - 1);
int nextHigh = Math.max(dpLow + a[i] - 1,
dpHigh + Math.abs(a[i] - a[i - 1]));
dpLow = nextLow;
dpHigh = nextHigh;
// System.err.println(dpLow + " " + dpHigh);
}
out.println(Math.max(dpLow, dpHigh));
}
Solution() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int t = nextInt();
while (t-- > 0) {
solve();
}
out.close();
}
public static void main(String[] args) throws IOException {
new Solution();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
}
In C :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
int main() {
int T,N,B,L,R,ML,MR,X,Y,P,Q;
scanf("%d",&T);
for(int i = 0; i < T; i++) {
scanf("%d",&N);
for(int j = 0; j < N; j++) {
scanf("%d",&B);
if(j) {
X = L - 1 + ML;
Y = R - 1 + MR;
P = abs(L - B) + ML;
Q = abs(R - B) + MR;
ML = (X > Y ? X : Y);
MR = (P > Q ? P : Q);
} else {
ML = MR = 0;
}
L = 1;
R = B;
}
printf("%d\n", (ML > MR ? ML : MR));
}
return 0;
}
In Python3 :
def cost(B) :
c0,c1 = 0,0
for i in range(1,len(B)) :
c0,c1 = max(c0,c1+B[i-1]-1), max(c0+B[i]-1,c1+abs(B[i]-B[i-1]))
return max(c0,c1)
T = int(input())
for _ in range(T) :
N = int(input())
B = [int(_) for _ in input().strip().split()]
print(cost(B))
View More Similar Problems
Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →