Confusing logic of dp - c++

I am trying to solve a dynamic programming question on hackerearth.
Even after trying to simulate the logic using pen and paper, I am unable to comprehend the solution (given in the editorial).
Can someone explain the commented lines? Any help would be greatly appreciated. I have been trying to understand it for 3 days...
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5e3+5;
bool vis[MAXN];
int ar[MAXN];
int pre[MAXN];
vector<int> v;
int dp[MAXN];
void sieve() {
v.push_back(2);
for(int i=3;i<MAXN;i+=2) if(!vis[i]) {
v.push_back(i);
for(int j=i*i;j<MAXN;j+=2*i) vis[j]=true;
}
}
int main() {
// freopen("TASK.in","r",stdin);
// freopen("TASK.out","w",stdout);
int n;
cin>>n;
assert(n<=5000);
for(int i=1;i<=n;i++) {
scanf("%d",&ar[i]);
assert(ar[i]<=100000);
pre[i]=pre[i-1]+ar[i];
}
sieve();
dp[0]=dp[1]=0;
for(int i=2;i<=n;i++) {
dp[i]=dp[i-1];
for(int j=0;j<(int)v.size() and v[j]<=i;j++) {
int p=i-v[j]-1;//please explain this line
if(p==-1) dp[i]=max(dp[i],pre[i]);
else dp[i]=max(dp[i],dp[p]+pre[i]-pre[p+1]);// please explain this line
}
}
cout<<dp[n]<<endl;
return 0;
}

Here array prestands for prefix sum, vector v contains prime numbers below MAXN. First line you have commented is
int p=i-v[j]-1;
Here v[j] is the jth prime number starting from 2, dp[i] is the best possible score for first i problems. If you solve v[j] number of consecutive problems (from i and going backward) you are left with i - v[j] problems from the beginning. -1 comes from the fact that you cannot solve the (v[j] - 1) th problem from i (backward) (if you do that you will have solved v[j] + 1 consecutive problems which is not prime as v[j] is prime).
Second line you have commented:
dp[i]=max(dp[i],dp[p]+pre[i]-pre[p+1]);
It follows from above since if you solve v[j] problems from i (backward) you get pre[i]-pre[p+1] points and you add that with dp[p] which is best result you already obtained for p. For example if i = 10 and v[j] = 3 you get dp[10] = max (dp[10],dp[6]+pre[10]-pre[7]) which is what you would expect.

Related

Doing away with Runtime Error(SIGSEGV)

I don't think in my code I tried accessing null pointers, or initialising large arrays, someone help please, I dunno where the Runtime Error(SIGSEGV) is coming from. Question to problem can be found at https://www.codechef.com
/MARCH18B/problems/MINEAT
edit:
I think i found out, NathanOliver was right, v1, because of my code, happens to be sometimes empty. Some answers were actually found out of my loop (above n). Thanks alot. I fixed that and I finally got AC, but just 30 points, my code took an additional 0.01 seconds to run. Can anyone help me optimize it, based on Problem statement, Please.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t; cin>>t; while(t--)
{
int n = 0, h = 0; cin>>n>>h;
vector<int> v; vector<int> v1;
for(int i = 0; i != n; i++){int a; cin>>a; v.push_back(a);}
for(int j = 1; j <= h; j++)
{
int hold = 0;
for(auto k : v)
{
if (j >= k){hold +=1;}
else if (j < k){if(k % j == 0){hold += (k/j);} else{hold += ((k/j)+1);}}
}
if (hold <= h){v1.push_back(j);}
}
cout<<*min_element(v1.begin(),v1.end())<<endl;
}
}
Did you check the min_element function's return value? According to the user input, min_element function returns an iterator to 'last element' which is basically a nullptr. Since you're dereferencing it directly, you get the error.

Alternative algorithm for depth first search

A problem involves a depth first search in a directed graph to find all the nodes that can be reached from a particular node. The solution given below is giving a wrong result on codechef. But I cannot find any test case for which this might produce a different result that the usual DFS algorithm would.
I know I can directly implement the correct algorithm to get the right result but I want to learn why my solution was incorrect so that I won't repeat it in future. Please help me identify whats wrong with this solution. The code is commented to explain my approach
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long int lli;
vector <lli> g[1000+5]; // the adjacency list 1 indexed
void dfs(lli j, lli i);
int main(){
lli n, m, k, a, b;
// n = number of nodes
// m = number of relations
// k = multiplication factor
cin >> n >> m >> k;
while(m--){
// a,b means a is dependent upon b (directed graph)
cin >> a >> b;
g[a].push_back(b);
}
for(lli j = 1; j <= n; j++)
for(lli i = 0; i < g[j].size(); i++){
dfs(j, g[j][i]); // adds dependencies of g[j][i]
// to adjacency list of j
}
// ans is the minimum no of nodes dependent on a particular node
lli ans = g[1].size();
for(lli i = 1; i <= n; i++){
if(g[i].size() < ans)
ans = g[i].size();
}
cout << (ans+1)*k <<"\n";
}
void dfs(lli j, lli i){
// adding dependencies of a node to itself
// would result in an infinite loop?
if(i != j){
for(lli k = 0; k < g[i].size(); k++){
// a node is not dependent on itself
if(g[i][k]!=j && find(g[j].begin(), g[j].end(), g[i][k])==g[j].end()){
g[j].push_back(g[i][k]);
dfs(j, g[i][k]);
}
}
}
}`
The link for the problem : problem
link for correct solution: correct solution
Your problem is that you are not aware of multi-edges which are possible with the given problem constrains, otherwise it looks correct. Take a look at this test case:
2 4 1
1 2
1 2
2 1
2 1
Your program will return 3, but there are only 2 vertices!
Having said that, I would like to add, that I disagree with the sample solution: It says the running time would be O(N^2) which is not true, because it starts N dfs every one with costs of O(N+M) thus resulting in O(N*(N+M)) with N=10^3 and M=10^6 there is no change to be in the time limit of 0.01 seconds!
Actually, this problem can be solved in O(N+M) using algorithms for detecting strongly connected components.

N by N matrix diagonal difference

Hi I have this following code snippet for getting the difference in an N by N array but it does not seem to be giving me the correct answers I got the algorithm by drawing the N by N matrix on paper and working it out, It does not give me any bugs but does not give me the answer I expect to receive, what am I missing out on
//includes here
int main(){
int n;
int diagOne{0};
int diagTwo{0};
cin >> n;
vector< vector<int> > a(n,vector<int>(n));
for(int a_i = 0;a_i < n;a_i++){
for(int a_j = 0;a_j < n;a_j++){
cin >> a[a_i][a_j];
if (a_i==a_i){
diagOne+=a[a_i][a_j];
}
else if(a_i+a_j==n-1) {
diagTwo+=a[a_i][a_j];
}
}
int sum =abs(diagOne -diagTwo);
}
return 0;
}
Your if in the second for is always true a_i==a_i.
In addition to #Milos Radosavljevic's answer, your int sum =abs(diagOne -diagTwo); is overwriting itself every outer loop. To fix this move it outside the outer loop. Here is a live demo of the fixed version of your code.
Note that this calculates the difference between the sum of the main diagonal and the anti diagonal (trace minus anti-trace). If however, you were after the element-wise difference total then you could do this.

Count number of ways for choosing two numbers in efficient algorithm

I solved this problem but I got TLE Time Limit Exceed on online judge
the output of program is right but i think the way can be improved to be more efficient!
the problem :
Given n integer numbers, count the number of ways in which we can choose two elements such
that their absolute difference is less than 32.
In a more formal way, count the number of pairs (i, j) (1 ≤ i < j ≤ n) such that
|V[i] - V[j]| < 32. |X|
is the absolute value of X.
Input
The first line of input contains one integer T, the number of test cases (1 ≤ T ≤ 128).
Each test case begins with an integer n (1 ≤ n ≤ 10,000).
The next line contains n integers (1 ≤ V[i] ≤ 10,000).
Output
For each test case, print the number of pairs on a single line.
my code in c++ :
int main() {
int T,n,i,j,k,count;
int a[10000];
cin>>T;
for(k=0;k<T;k++)
{ count=0;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(i!=j)
{
if(abs(a[i]-a[j])<32)
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}
I need help how can I solve it in more efficient algorithm ?
Despite my previous (silly) answer, there is no need to sort the data at all. Instead you should count the frequencies of the numbers.
Then all you need to do is keep track of the number of viable numbers to pair with, while iterating over the possible values. Sorry no c++ but java should be readable as well:
int solve (int[] numbers) {
int[] frequencies = new int[10001];
for (int i : numbers) frequencies[i]++;
int solution = 0;
int inRange = 0;
for (int i = 0; i < frequencies.length; i++) {
if (i > 32) inRange -= frequencies[i - 32];
solution += frequencies[i] * inRange;
solution += frequencies[i] * (frequencies[i] - 1) / 2;
inRange += frequencies[i];
}
return solution;
}
#include <bits/stdc++.h>
using namespace std;
int a[10010];
int N;
int search (int x){
int low = 0;
int high = N;
while (low < high)
{
int mid = (low+high)/2;
if (a[mid] >= x) high = mid;
else low = mid+1;
}
return low;
}
int main() {
cin >> N;
for (int i=0 ; i<N ; i++) cin >> a[i];
sort(a,a+N);
long long ans = 0;
for (int i=0 ; i<N ; i++)
{
int t = search(a[i]+32);
ans += (t -i - 1);
}
cout << ans << endl;
return 0;
}
You can sort the numbers, and then use a sliding window. Starting with the smallest number, populate a std::deque with the numbers so long as they are no larger than the smallest number + 31. Then in an outer loop for each number, update the sliding window and add the new size of the sliding window to the counter. Update of the sliding window can be performed in an inner loop, by first pop_front every number that is smaller than the current number of the outer loop, then push_back every number that is not larger than the current number of the outer loop + 31.
One faster solution would be to first sort the array, then iterate through the sorted array and for each element only visit the elements to the right of it until the difference exceeds 31.
Sorting can probably be done via count sort (since you have 1 ≤ V[i] ≤ 10,000). So you get linear time for the sorting part. It might not be necessary though (maybe quicksort suffices in order to get all the points).
Also, you can do a trick for the inner loop (the "going to the right of the current element" part). Keep in mind that if S[i+k]-S[i]<32, then S[i+k]-S[i+1]<32, where S is the sorted version of V. With this trick the whole algorithm turns linear.
This can be done constant number of passes over the data, and actually can be done without being affected by the value of the "interval" (in your case, 32).
This is done by populating an array where a[i] = a[i-1] + number_of_times_i_appears_in_the_data - informally, a[i] holds the total number of elements that are smaller/equals to i.
Code (for a single test case):
static int UPPER_LIMIT = 10001;
static int K = 32;
int frequencies[UPPER_LIMIT] = {0}; // O(U)
int n;
std::cin >> n;
for (int i = 0; i < n; i++) { // O(n)
int x;
std::cin >> x;
frequencies[x] += 1;
}
for (int i = 1; i < UPPER_LIMIT; i++) { // O(U)
frequencies[i] += frequencies[i-1];
}
int count = 0;
for (int i = 1; i < UPPER_LIMIT; i++) { // O(U)
int low_idx = std::max(i-32, 0);
int number_of_elements_with_value_i = frequencies[i] - frequencies[i-1];
if (number_of_elements_with_value_i == 0) continue;
int number_of_elements_with_value_K_close_to_i =
(frequencies[i-1] - frequencies[low_idx]);
std::cout << "i: " << i << " number_of_elements_with_value_i: " << number_of_elements_with_value_i << " number_of_elements_with_value_K_close_to_i: " << number_of_elements_with_value_K_close_to_i << std::endl;
count += number_of_elements_with_value_i * number_of_elements_with_value_K_close_to_i;
// Finally, add "duplicates" of i, this is basically sum of arithmetic
// progression with d=1, a0=0, n=number_of_elements_with_value_i
count += number_of_elements_with_value_i * (number_of_elements_with_value_i-1) /2;
}
std::cout << count;
Working full example on IDEone.
You can sort and then use break to end loop when ever the range goes out.
int main()
{
int t;
cin>>t;
while(t--){
int n,c=0;
cin>>n;
int ar[n];
for(int i=0;i<n;i++)
cin>>ar[i];
sort(ar,ar+n);
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(ar[j]-ar[i] < 32)
c++;
else
break;
}
}
cout<<c<<endl;
}
}
Or, you can use a hash array for the range and mark occurrence of each element and then loop around and check for each element i.e. if x = 32 - y is present or not.
A good approach here is to split the numbers into separate buckets:
constexpr int limit = 10000;
constexpr int diff = 32;
constexpr int bucket_num = (limit/diff)+1;
std::array<std::vector<int>,bucket_num> buckets;
cin>>n;
int number;
for(i=0;i<n;i++)
{
cin >> number;
buckets[number/diff].push_back(number%diff);
}
Obviously the numbers that are in the same bucket are close enough to each other to fit the requirement, so we can just count all the pairs:
int result = std::accumulate(buckets.begin(), buckets.end(), 0,
[](int s, vector<int>& v){ return s + (v.size()*(v.size()-1))/2; });
The numbers that are in non-adjacent buckets cannot form any acceptable pairs, so we can just ignore them.
This leaves the last corner case - adjacent buckets - which can be solved in many ways:
for(int i=0;i<bucket_num-1;i++)
if(buckets[i].size() && buckets[i+1].size())
result += adjacent_buckets(buckets[i], buckets[i+1]);
Personally I like the "occurrence frequency" approach on the one bucket scale, but there may be better options:
int adjacent_buckets(const vector<int>& bucket1, const vector<int>& bucket2)
{
std::array<int,diff> pairs{};
for(int number : bucket1)
{
for(int i=0;i<number;i++)
pairs[i]++;
}
return std::accumulate(bucket2.begin(), bucket2.end(), 0,
[&pairs](int s, int n){ return s + pairs[n]; });
}
This function first builds an array of "numbers from lower bucket that are close enough to i", and then sums the values from that array corresponding to the upper bucket numbers.
In general this approach has O(N) complexity, in the best case it will require pretty much only one pass, and overall should be fast enough.
Working Ideone example
This solution can be considered O(N) to process N input numbers and constant in time to process the input:
#include <iostream>
using namespace std;
void solve()
{
int a[10001] = {0}, N, n, X32 = 0, ret = 0;
cin >> N;
for (int i=0; i<N; ++i)
{
cin >> n;
a[n]++;
}
for (int i=0; i<10001; ++i)
{
if (i >= 32)
X32 -= a[i-32];
if (a[i])
{
ret += a[i] * X32;
ret += a[i] * (a[i]-1)/2;
X32 += a[i];
}
}
cout << ret << endl;
}
int main()
{
int T;
cin >> T;
for (int i=0 ; i<T ; i++)
solve();
}
run this code on ideone
Solution explanation: a[i] represents how many times i was in the input series.
Then you go over entire array and X32 keeps track of number of elements that's withing range from i. The only tricky part really is to calculate properly when some i is repeated multiple times: a[i] * (a[i]-1)/2. That's it.
You should start by sorting the input.
Then if your inner loop detects the distance grows above 32, you can break from it.
Thanks for everyone efforts and time to solve this problem.
I appreciated all Attempts to solve it.
After testing the answers on online judge I found the right and most efficient solution algorithm is Stef's Answer and AbdullahAhmedAbdelmonem's answer also pavel solution is right but it's exactly same as Stef solution in different language C++.
Stef's code got time execution 358 ms in codeforces online judge and accepted.
also AbdullahAhmedAbdelmonem's code got time execution 421 ms in codeforces online judge and accepted.
if they put detailed explanation to there algorithm the bounty will be to one of them.
you can try your solution and submit it to codeforces online judge at this link after choosing problem E. Time Limit Exceeded?
also I found a great algorithm solution and more understandable using frequency array and it's complexity O(n).
in this algorithm you only need to take specific range for each inserted element to the array which is:
begin = element - 32
end = element + 32
and then count number of pair in this range for each inserted element in the frequency array :
int main() {
int T,n,i,j,k,b,e,count;
int v[10000];
int freq[10001];
cin>>T;
for(k=0;k<T;k++)
{
count=0;
cin>>n;
for(i=1;i<=10000;i++)
{
freq[i]=0;
}
for(i=0;i<n;i++)
{
cin>>v[i];
}
for(i=0;i<n;i++)
{
count=count+freq[v[i]];
b=v[i]-31;
e=v[i]+31;
if(b<=0)
b=1;
if(e>10000)
e=10000;
for(j=b;j<=e;j++)
{
freq[j]++;
}
}
cout<<count<<endl;
}
return 0;
}
finally i think the best approach to solve this kind of problems to use frequency array and count number of pairs in specific range because it's time complexity is O(n).

SPOJ SUMFOUR.....TLE on test case 9

I am trying to solve SPOJ Problem SUMFOUR....I am geting TLE on test case 9 http://www.spoj.com/problems/SUMFOUR/
So,Which part of my code has to be edited and how?Here N<=4000
#include <iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
int main()
{
int a[4005][5],n;
cin>>n;
for(int i=1;i<=n;i++)
for(int j=1;j<=4;j++)
scanf("%d",&a[i][j]);
int k=0;
for(int i=1;i<=n;i++)
{ int p=a[i][1];
for(int j=1;j<=n;j++)
{ b.push_back(p+a[j][2]);
k++;
}
}
k=0;
for(int i=1;i<=n;i++)
{ int p=a[i][3];
for(int j=1;j<=n;j++)
{ c.push_back(p+a[j][4]);
k++;
}
}
sort(b.begin(),b.end());
int cnt=0;
for(int j=0;j<k;j++)
if(find(b.begin(),b.end(),-c[j])!=b.end() )
cnt=cnt+count(b.begin(),b.end(),-c[j]) ;
printf("%d\n",cnt);
return 0;
}
The problem is here:
for(int j=0;j<k;j++)
if(find(b.begin(),b.end(),-c[j])!=b.end() )
cnt=cnt+count(b.begin(),b.end(),-c[j]) ;
for n = 4000, so there are 4000^2 elements in b and c. So, the time complexity for this loop is 4000^4, as find and count time complexity is O(n), which of course will cause you time limit exceed.
So, how you can reduce the time? You can use binary search to faster the count process, which reduce the time complexity of the above loop to O(n^2 log n), as I notice you already sort b.
Or , you can use map to count and store the frequency of each element in b and c.
map<long long, int> b;
map<long long, int> c;
for(int i=1;i<=n;i++)
{ long long p=a[i][1];
for(int j=1;j<=n;j++)
{
long long tmp =p + a[j][2];
b[tmp] = b[tmp] + 1;
}
}
// Similar for c
map <long long, int>::iterator it;
long long result;
for (it = c.begin(); it != c.end(); ++it)
result += c[it->first]*b[-(it->first)];
For your new update, please change this:
for(int j=1;j<=n;j++)
{ if( b.count(a[i][1]+a[j][2]) )
{ b[a[i][1]+a[j][2]]+=1;
c[a[i][3]+a[j][4]]+=1;
}
else
{ b[a[i][1]+a[j][2]]=1;
c[a[i][3]+a[j][4]]=1;
}
}
into this:
for(int j=1;j<=n;j++)
{
b[a[i][1]+a[j][2]]+=1;
c[a[i][3]+a[j][4]]+=1;
}
The condition check if( b.count(a[i][1]+a[j][2]) ) is for b only, and you use it for c, which make c incorrect.
Update: After trying to get accepted in SPOJ, it turns out that map is not fast enough, so I make a change into binary search, and got accepted.
My accepted code
Please Don't Use Map as its worst Case Complexity Can be O(log(n)) .
SO instead You can just Use two sorted arrays and for every element as in the
first array , Binary Search for its -ve agent in the Second Cumulative array .
Just Change the find method in Last lines to Binary search(c.begin(),c.end(),key) and find the repititons till the end with that index as it gives the lower_bound index .
That Total Sum gives the answer and its expected Complexity is
O(n^2log(n)).