Given a rod of length n inches and a table of prices pi for
i = 1, 2,... n, determine the maximum revenue rn obtainable by cutting up
the rod and selling the pieces.
Bottom_Up_Cut_Rod(p, n)
1 let r[0...n] be a new array
2 r[0] = 0
3 for j = 1 to n
4 q = -infinity
5 for i = 1 to j
6 q = max(q; p[i] + r[j - i])
7 r[j] = q
8 return r[n]
Implementation
#include <iostream>
#include <algorithm>
using namespace std;
int RodCut(long long P[],long long n)
{
long long r[n];
r[0]=0;
for(long long j=0;j<n;j++)
{
long long q = -100000;
for(long long i=0;i<j;i++)
{
q = max(q , P[i] + r[j-i]);
}
r[j] = q;
}
return r[n];
}
int main()
{
long long num;
long long N;
long long K;
cin>>N;
long long a[N];
for (long long i = 0; i < N; i++)
{
cin>>num;
a[i] = num;
}
int res = 0;
res = RodCut(a,N);
cout<<"Answer : "<<res;
return 0;
}
My input is 1 5 8 9 10 17 17 20 24 30, but output is 2686348.
What is wrong with my code?
There are several issues. You want the main loop to go from j = 1 to n, because it represents the best you can do using j elements.
You should stick to using either ints or long longs.
int r[n+1];
r[0]=0;
// Calculate best we can do with j elements
for(int j=1;j<=n;j++) {
int q = -100000;
for(int i=0;i<j;i++) {
q = max(q , P[i] + r[j-i-1]);
}
r[j] = q;
}
return r[n];
This seems to give me the right solution for a variety of inputs.
There are two things. One is returning r[n], which should be r[n-1]. Second, start j from 1 to n, since r[0] is getting replaced with -100000 in the first round.
Also, r[0] should be P[0]; i.e. you'll atleast make P[0] money given a rod with length 1.
Also, note that q should be P[j], thats the minimum you'll make.
So assuming the array is P[0..n] // not including n
and r[0..n] is your memo for applying DP
foreach index from (0..n] // not including n
r[index] = P[index]
foreach splitIndex from (0..index] // not including index
r[index] = max(r[index], P[splitIndex] + r[index-splitIndex-1]
return r[n-1]
Related
Given an array of N numbers (not necessarily sorted). We can merge any two numbers into one and the cost of merging the two numbers is equal to the sum of the two values. The task is to find the total minimum cost of merging all the numbers.
Example:
Let the array A = [1,2,3,4]
Then, we can remove 1 and 2, add both of them and keep the sum back in array. Cost of this step would be (1+2) = 3.
Now, A = [3,3,4], Cost = 3
In second step, we can 3 and 3, add both of them and keep the sum back in array. Cost of this step would be (3+3) = 6.
Now, A = [4,6], Cost = 6
In third step, we can remove both elements from the array and keep the sum back in array again. Cost of this step would be (4+6) = 6.
Now, A = [10], Cost = 10
So, total cost turns out to be 19 (10+6+3).
We will have to pick the 2 smallest elements to minimize our total cost. A simple way to do this is using a min heap structure. We will be able to get the minimum element in O(1) and insertion will be O(log n).
The time complexity of this approach is O(n log n).
But I tried another approach, and wasn't able to find the cases where it fails. The basic idea was that the sum of two smallest elements that we will choose at any time will always be greater than the sum of the pair of elements chosen before. So the "temp" array will always be sorted, and we will be able to access the minimum elements in O(1).
As I am sorting the input array and then simply traversing the array, the complexity of my approach is O(n log n).
int minCost(vector<int>& arr) {
sort(arr.begin(), arr.end());
// temp array will contain the sum of all the pairs of minimum elements
vector<int> temp;
// index for arr
int i = 0;
// index for temp
int j = 0;
int cost = 0;
// while we have more than 1 element combined in both the input and temp array
while(arr.size() - i + temp.size() - j > 1) {
int num1, num2;
// selecting num1 (minimum element)
if(i < arr.size() && j < temp.size()) {
if(arr[i] <= temp[j])
num1 = arr[i++];
else
num1 = temp[j++];
}
else if(i < arr.size())
num1 = arr[i++];
else if(j < temp.size())
num1 = temp[j++];
// selecting num2 (second minimum element)
if(i < arr.size() && j < temp.size()) {
if(arr[i] <= temp[j])
num2 = arr[i++];
else
num2 = temp[j++];
}
else if(i < arr.size())
num2 = arr[i++];
else if(j < temp.size())
num2 = temp[j++];
// appending the sum of the minimum elements in the temp array
int sum = num1 + num2;
temp.push_back(sum);
cost += sum;
}
return cost;
}
Is this approach correct? If not, please let me know what I am missing, and the test cases in which this algorithm fails.
SPOJ Link for the same problem
The logic seems very solid to me... all the computed sums will never be decreasing and therefore you only need to add up either oldest two computed sums, next two elements or oldest sum and next element.
I would just simplify the code:
#include <vector>
#include <algorithm>
#include <stdio.h>
int hsum(std::vector<int> arr) {
int ni = arr.size(), nj = 0, i = 0, j = 0, res = 0;
std::sort(arr.begin(), arr.end());
std::vector<int> temp;
auto get = [&]()->int {
if (j == nj || (i < ni && arr[i] < temp[j])) return arr[i++];
return temp[j++];
};
while ((ni-i)+(nj-j)>1) {
int a = get(), b = get();
res += a+b;
temp.push_back(a + b); nj++;
}
return res;
}
int main() {
fprintf(stderr, "%i\n", hsum(std::vector<int>{1,4,2,3}));
return 0;
}
Very nice idea!
Another improvement is noting that the cumulative length of the two arrays being processed (the original one and the temporary one holding the sums) will decrease at every step.
Since the first step will use two input elements, the fact that the temporary array grows one element at each step will still not be enough for a "walking queue" allocated in the array itself to reach the reading pointer.
This means that there is no need of a temporary array and the space for the sums can be found in the array itself...
int hsum(std::vector<int> arr) {
int ni = arr.size(), nj = 0, i = 0, j = 0, res = 0;
std::sort(arr.begin(), arr.end());
auto get = [&]()->int {
if (j == nj || (i < ni && arr[i] < arr[j])) return arr[i++];
return arr[j++];
};
while ((ni-i)+(nj-j)>1) {
int a = get(), b = get();
res += a+b;
arr[nj++] = a + b;
}
return res;
}
About the error on SPOJ... I tried briefly to search for the problem but I didn't succeed. I tried however generating random arrays of random lengths and checking this solution with what finds a "brute-force" one implemented directly from the specs and I'm reasonably confident that the algorithm is correct.
I know at least one programming arena (Topcoder) where sometimes the problems are carefully crafted so that the computation gives correct results if using unsigned but not if using int (or if using unsigned long long but not if using long long) because of integer overflow.
I don't know if SPOJ also does this kind of nonsense(1)... may be that is the reason some hidden test case fails...
EDIT
Checking with SPOJ the algorithm passes if using long long values... this is the entry I used:
#include <stdio.h>
#include <algorithm>
#include <vector>
int main(int argc, const char *argv[]) {
int n;
scanf("%i", &n);
for (int testcase=0; testcase<n; testcase++) {
int sz; scanf("%i", &sz);
std::vector<long long> arr(sz);
for (int i=0; i<sz; i++) scanf("%lli", &arr[i]);
int ni = arr.size(), nj = 0, i = 0, j = 0;
long long res = 0;
std::sort(arr.begin(), arr.end());
auto get = [&]() -> long long {
if (j == nj || (i < ni && arr[i] < arr[j])) return arr[i++];
return arr[j++];
};
while ((ni-i)+(nj-j)>1) {
long long a = get(), b = get();
res += a+b;
arr[nj++] = a + b;
}
printf("%lli\n", res);
}
return 0;
}
PS: This very kind of computation is also what is needed to build an Huffman tree for entropy coding given the symbols frequency table and thus it's not a mere random exercise but it has practical applications.
(1) I'm saying "nonsense" because in Topcoder they never give problems that require 65 bits; thus it's not a genuine care about overflows, but just setting traps for novices.
Another that I think is a bad practice I saw on TC is that some problems are carefully designed so that the correct algorithm if using C++ will barely fit in the timeout limit: just use another language (and get e.g. a 2× slowdown) and you cannot solve the problem.
First of all, think simple!
When using a priority queue, the problem is easy!
In the first test case :
1 6 3 20
// after pushing to Q
1 3 6 20
// and sum two top items and pop and push!
(1 + 3) 6 20 cost = 4
(4 + 6) 20 cost = 10 + 4
(10 + 20) cost = 30 + 14
30 cost = 44
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
priority_queue<long long int, vector<long long int>, greater<long long int>> q;
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
q.push(k);
}
long long int sum = 0;
while (q.size() > 1) {
long long int a = q.top();
q.pop();
long long int b = q.top();
q.pop();
q.push(a + b);
sum += a + b;
}
cout << sum << "\n";
}
}
Basically we need to sort the list in desc order and then find its cost like this.
A.sort(reverse=True)
cost = 0
for i in range(len(A)):
cost += A[i] * (i+1)
return cost
It's showing the wrong answer. Can anybody please tell me which test case I am missing ?
Without Adjacent
Given an array arr[] of N positive integers. The task is to find a subsequence with maximum sum such that there should be no adjacent elements from the array in the subsequence.
Input:
First line of input contains number of testcases T. For each testcase, first line of input contains size of array N. Next line contains N elements of the array space seperated.
Output:
For each testcase, print the maximum sum of the subsequence.
Constraints:
1 <= T <= 100
1 <= N <= 10^6
1 <= arr[i] <= 10^6
Example:
Input:
2
3
1 2 3
3
1 20 3
Output:
4
20
Explanation:
Testcase 1: Elements 1 and 3 form a subsequence with maximum sum and no elements in the subsequence are adjacent in the array.
Testcase 2: Element 20 from the array forms a subsequence with maximum sum.
I tried using below test cases also
Input:
3
9
1 2 9 4 5 0 4 11 6
1
0
1
1
Output:
26
0
1
It worked fine but while submitting it was giving "wrong answer" I don't know for which test case it was talking about
Here is my solution:
#include<iostream>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
int sum1,sum2,sum_even=0,sum_odd=0;
for(int i=0;i<n;i+=2)
sum_even+=arr[i];
for(int i=1;i<n;i+=2)
sum_odd+=arr[i];
if(n>=1)
sum1 = arr[0];
else
sum1 = -1;
if(n>=2)
sum2 = arr[1];
else
sum2 = -1;
int new_sum,i;
for(i=2; i<n; i+=2)
{
if((i+1)!=n && arr[i+1]>arr[i])
{
i++;
sum1+=arr[i];
}
else if(i+1==n)
{
sum1+=arr[i];
}
else
{
sum1+=arr[i];
}
}
for(i=3; i<n; i+=2)
{
if((i+1)!=n && arr[i+1]>arr[i])
{
i++;
sum2+=arr[i];
}
else if(i+1 ==n)
{
sum2+=arr[i];
}
else
{
sum2+=arr[i];
}
}
int sum = sum1>sum2 ? sum1 : sum2;
sum = sum>sum_odd ? sum : sum_odd;
sum = sum>sum_even ? sum : sum_even;
cout<<sum<<endl;
}
return 0;
}
The issue is that you seem to made some guesses on the structure on any solution.
Your code is rather complex and it is difficult effectively to find a counter example by hand.
I made a random generation of arrays and compare your result with the optimal one.
I finally obtained this counter example : [14 18 8 19 22 1 20 23]. Your code gives a result of 64, while the optimum sum is equal to 67.
A simple optimum solution is to iteratively calculate two sums, both corresponding to a maximum up to the current index i,
the first sum (sum0) assuming current value arr[i] is not used, the second sum (sum1) assuming the current value arr[i] is used.
#include <iostream>
#include <vector>
#include <algorithm>
int max_sum (const std::vector<int>& arr) {
int sum0 = 0;
int sum1 = arr[0];
int n = arr.size();
for (int i = 1; i < n; ++i) {
int temp = sum0;
sum0 = std::max (sum0, sum1);
sum1 = temp + arr[i];
}
return std::max (sum0, sum1);
}
int main() {
int t;
std::cin >> t;
while(t--) {
int n;
std::cin >> n;
std::vector<int> arr(n);
for(int i = 0; i < n; i++)
std::cin >> arr[i];
int sum = max_sum (arr);
std::cout << sum << '\n';
}
}
I am getting a timeout for several test cases of a programming challenge. Any help will be appreciated.
That's the exercise:
A palindromic number reads the same both ways. The smallest 6 digit palindrome made from the product of two 3-digit numbers is 101101 = 143 * 707.
Find the largest palindrome made from the product of two 3-digit numbers which is less than N (any input greater than 101101 and less than 1000000).
What I have is this:
#include<bits/stdc++.h>
using namespace std;
bool check_palindrome(unsigned int a)
{
unsigned int temp = a;
unsigned int digit ; //used for reversing
unsigned int rev_a = 0; //final reversed number
int power = 5; //used for reversing
unsigned int modulo; //used for reversing
while(temp > 0)
{
digit = temp / int(pow(10,power));
temp = temp % int(pow(10 , power));
rev_a = rev_a + digit * (pow(10 , 5 - power));
power--;
}
return (a == rev_a) ? true : false ;
}
int main()
{
int T;
unsigned int n;
scanf("%d" , &T);
for(int i = 0 ; i < T ; i++) //for entering number of test cases
{
unsigned int max_palindrome=0;
scanf("%d" , &n); //Input the number
for(int p = 101 ; p <= 999 ; p++)
{
int m ;
int other_number = int(n/p);
if(other_number > 999 )
m = 999;
else
m = other_number;
for( int q = m ; q >100 ; q--)
{
if( p*q < 101101)
break;
bool palindrome = check_palindrome(p*q);
if(palindrome)
{
if(p*q > max_palindrome)
max_palindrome = p*q;
break;
}
}
}
printf("%d\n" , max_palindrome);
}
return 0;
}
Rather than trying all reasonable pairs of factors you could start from the given number and count down. Each time you come to a palindromic number, see if it can be expressed as a suitable product (checking all possible factors from sqrt(palindrome) down to 101).
The advantage is that as soon as you find a suitable palindrome you're done, you don't have to keep searching.
EDIT: You don't even have to search for palindromes, you can just enumerate them by working through all possible front halves and computing the corresponding back halves.
As #AlanStokes said, you're checking more values than you need. In addition, here's a more reasonable palindrome check.
bool is_palindrome(unsigned n) {
unsigned rn = 0;
for (unsigned x = n; x; x /= 10)
rn = 10 * rn + x % 10;
return rn == n;
}
Here is the question:
The sum of the primes below 10 is 2+3+5+7=17.
Find the sum of all the primes not greater than given N.
Input Format :
The first line contains an integer T i.e. number of the test cases.
The next T lines will contains an integer N.
Output Format :
Print the value corresponding to each test case in seperate line.
Constraints :
1≤T≤104
1≤N≤106
https://www.hackerrank.com/contests/projecteuler/challenges/euler010
This is the link to the question.
So, i attempted to solve this question using sieve of Eratosthenes.
I pre calculated all primes below 10^6 which is the given limit for N.
6 out of the 7 test cases were accepted but the last test case give Timeout(TLE) .
I read the discussion forum and there they say that in order to solve the question we need to pre-calculate the sums of primes also.
So, i tried making an array of long long ints and tried storing all the sums in it. But this is giving me a segmentation fault.
So, how am I supposed to precalculate the sums of the primes?
Here is my code:
#include "header.h" //MAX is defined to be 1000000
bool sieve[MAX + 1]; // false = prime, true = composite
int main(void){
//0 and 1 are not primes
sieve[0] = sieve[1] = true;
//input limiting value
int n = MAX;
//cross out even numbers
for(int i = 4; i <= n; i += 2){
sieve[i] = true;
}
//use sieve of eratosthenes
for(int i = 3; i <= static_cast<int>(sqrt(n)); i += 2){
if(sieve[i] == false){
for(int j = i * i; j <= n; j += i)
sieve[j] = true;
}
}
long long p, ans = 0;
int t;
std::cin >> t;
while(t--){
std::cin >> p;
for(int i = 0; i <= p; ++i)
if(sieve[i] == false)
ans += i;
std::cout << ans << std::endl;
ans = 0;
}
return 0;
}
Given an array of primes prime[N], precomputing sums of primes can be done in a single for loop like this:
int sum[N];
sum[0] = primes[0];
for (int i = 1 ; i < N ; i++) {
sum[i] = prime[i]+sum[i-1];
}
You can use this array together with primes[] by running a binary search on primes, and picking the sum at the same position if the number being searched is prime, or at the prior position if the number is not prime.
Given a sequence of n positive integers we need to count consecutive sub-sequences whose sum is divisible by k.
Constraints : N is up to 10^6 and each element up to 10^9 and K is up to 100
EXAMPLE : Let N=5 and K=3 and array be 1 2 3 4 1
Here answer is 4
Explanation : there exists, 4 sub-sequences whose sum is divisible by 3, they are
3
1 2
1 2 3
2 3 4
My Attempt :
long long int count=0;
for(int i=0;i<n;i++){
long long int sum=0;
for(int j=i;j<n;j++)
{
sum=sum+arr[j];
if(sum%k==0)
{
count++;
}
}
}
But obviously its poor approach. Can their be better approach for this question? Please help.
Complete Question: https://www.hackerrank.com/contests/w6/challenges/consecutive-subsequences
Here is a fast O(n + k) solution:
1)Lets compute prefix sums pref[i](for 0 <= i < n).
2)Now we can compute count[i] - the number of prefixes with sum i modulo k(0 <= i < k).
This can be done by iterating over all the prefixes and making count[pref[i] % k]++.
Initially, count[0] = 1(an empty prefix has sum 0) and 0 for i != 0.
3)The answer is sum count[i] * (count[i] - 1) / 2 for all i.
4)It is better to compute prefix sums modulo k to avoid overflow.
Why does it work? Let's take a closer a look at a subarray divisible by k. Let's say that it starts in L position and ends in R position. It is divisible by k if and only if pref[L - 1] == pref[R] (modulo k) because their differnce is zero modulo k(by definition of divisibility). So for each fixed modulo, we can pick any two prefixes with this prefix sum modulo k(and there are exactly count[i] * (count[i] - 1) / 2 ways to do it).
Here is my code:
long long get_count(const vector<int>& vec, int k) {
//Initialize count array.
vector<int> cnt_mod(k, 0);
cnt_mod[0] = 1;
int pref_sum = 0;
//Iterate over the input sequence.
for (int elem : vec) {
pref_sum += elem;
pref_sum %= k;
cnt_mod[pref_sum]++;
}
//Compute the answer.
long long res = 0;
for (int mod = 0; mod < k; mod++)
res += (long long)cnt_mod[mod] * (cnt_mod[mod] - 1) / 2;
return res;
}
That have to make your calculations easier:
//Now we will move all numbers to [0..K-1]
long long int count=0;
for(int i=0;i<n;i++){
arr[i] = arr[i]%K;
}
//Now we will calculate cout of all shortest subsequences.
long long int sum=0;
int first(0);
std::vector<int> beg;
std::vector<int> end;
for(int i=0;i<n;i++){
if (arr[i] == 0)
{
count++;
continue;
}
sum += arr[i];
if (sum == K)
{
beg.push_back(first);
end.push_back(i);
count++;
}
else
{
while (sum > K)
{
sum -= arr[first];
first++;
}
if (sum == K)
{
beg.push_back(first);
end.push_back(i);
count++;
}
}
}
//this way we found all short subsequences. And we need to calculate all subsequences that consist of some short subsequencies.
int party(0);
for (int i = 0; i < beg.size() - 1; ++i)
{
if (end[i] == beg[i+1])
{
count += party + 1;
party++;
}
else
{
party = 0;
}
}
So, with max array size = 10^6 and max size of rest = 99, you will not have overflow even if you will need to summ all numbers in simple int32.
And time you will spend will be around O(n+n)