how to find the minimum number of ascending subsequence - algorithm

I meet a problem. http://poj.org/problem?id=1065
The problem is to find the minimum number of ascending subsequences.
I see somebody is to find the length of longest descending subsequences.
I don't know why the two numbers are equal.
#include <iostream>
#include <algorithm>
#include <functional>
#include <memory.h>
/* run this program using the console pauser or add your own getch,
system("pause") or input loop */
using namespace std;
pair<int,int> stick[5000];
int dp[5000];
int main(int argc, char** argv) {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>stick[i].first>>stick[i].second;
}
sort(stick,stick+n);
memset(dp,-1,sizeof(int)*5000);
for(int i=0;i<n;i++){
*lower_bound(dp,dp+n,stick[i].second,greater<int>())=stick[i].second;
}
cout<<lower_bound(dp, dp + n, -1, greater<int>()) - dp<<endl;
}
return 0;
}

It follows immediately from the Dilworth's theorem. It's a standard technique for solving problems like this.

Related

SIGSEGV error, the code is correct but still not giving any output can someone explain

#include <iostream>
#include <bits/stdc++.h>
#include <stack>
#include <algorithm>
#include <vector>
#include <iterator>
#define ll long long
using namespace std;
int main() {
ll t;
cin >> t;
while(t--) {
ll n,k;
cin >>n >>k;
vector<ll> arr;
for(int i=0; i<n; i++) cin >>arr[i];
vector <ll> freq;
for(int i=1;i<=k;i++) freq.push_back(i);
ll min=n,val,diff=0,max=0;
for(int i=0;i<k;i++){
ll c=count(arr.begin(),arr.end(),freq[i]);
if(min>c)
{min=c;
val=freq[i];
}
}
if(min==0) max=n;
else{
vector<ll> diffarr;
diffarr.push_back(0);
for(int i=0;i<n;i++)
{
if(arr[i]==val){
diffarr.push_back(i);
}
}
for(int i=0;i<min;i++)
{
diff=diffarr[i+1]-diffarr[i];
if(max<diff) max=diff;
}
}
cout <<max <<endl;
}
return 0;
}
The code works fine when input is not taken but as soon as I take input it throws a SIGSEGV error
I have debugged it many times to get rid of all segmentation faults and all kinds of errors. Can someone pls look into it, that would be a gr8 help
the code is correct
No, it isn't. Start with these lines:
vector<ll> arr;
for(int i=0; i<n; i++) cin >>arr[i];
You declare an empty vector arr, and immediately try to write to element 0 in that vector.
But the vector is still empty, so there is no element 0 in it. On most STL implementations this will crash by trying to write to address 0.
Reading How to debug small programs is likely to benefit you. So will learning how to use a debugger.

How to sort on the basis of 2nd element if there is a tie on first in vector of tuples

#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using namespace std;
typedef long long ll;
vector < tuple <ll,ll,ll> > a;
int main()
{
ll t;
cin>>t;
ll id,z,p,l,c,s,newz;
while(t--)
{
cin>>id>>z>>p>>l>>c>>s;
newz=p*50+l*5+c*10+s*20;
a.push_back(make_tuple(z-newz,id,newz));
}
sort(a.begin(),a.end());
for(int i=0;i<5;i++)
{
tie(ignore,id,z)=a[i];
cout<<id<<" "<<z<<endl;
}
return 0;
}
I want the sort on the vector to happen on the basis of first element of the tuple but only when there is a tie then the smallest of the second element of the tuple must be chosen to order the elements with the same first value.
Also specify what should be done, if at the time of a tie the order should be maintain on the basis of greater element of the second element of the tuple(instead of the first).
A custom function as a third parameter sorted my way out perfectly.
bool cmp( tuple <ll,ll,ll> const &s, tuple <ll,ll,ll> const &r)
{
if(get<0>(s)==get<0>(r))
{
return (get<1>(s))>(get<1>(r));
}
else
return (get<0>(s))<(get<0>(r));
}
sort(a.begin(),a.end(),cmp);// Call to sort will change like this.

Missing corner cases

Below is the link to problem
https://www.hackerrank.com/challenges/and-xor-or/copy-from/16519519
I have implemented a O(n) solution which seems ok (no TLE and 29 testcaes passed out of 32). But my solution is failing for some testcaes and I am not able to find the error, surely I am missing some corner cases. Any hint would be great help. I have posted my code below which I have ran and submitted.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <stack>
#define ul unsigned long
using namespace std;
ul comp(ul a, ul b){
ul result = ((a&b)^(a|b))&(a^b);
return result;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
vector<ul>ip(n);
for(int i=0;i<n;i++)
cin>>ip[i];
stack<ul>st;
ul result=0;
for(int i=0;i<n;i++){
if(st.empty())
st.push(ip[i]);
else{
result=max(result,comp(st.top(),ip[i]));
while(!st.empty() && st.top()>ip[i]){
result=max(result,comp(st.top(),ip[i]));
st.pop();
}
st.push(ip[i]);
}
}
cout<<result;
return 0;
}

vector accessing non zero elements but output as zero

I' did this program what suppose save pairs of string ,int on one vector and print the strings of the maximum number on vector
but when i try to find this strings don't appears nothing so I try print all values of int's on vector and although was finding the maximum of 10 all values in the vector was printing as 0. Someone can explain was it occurred and how I can access the values , please.
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef vector<pair<string,int>> vsi;
bool paircmp(const pair<string,int>& firste,const pair<string,int>& seconde );
int main(int argc, char const *argv[]) {
vsi v(10);
string s;
int n,t;
cin>>t;
for (size_t i = 0;i < t;i++) {
for (size_t j = 0; j < 10; j++) {
cin>>s>>n;
v.push_back(make_pair(s,n));
}
sort(v.begin(),v.end(),paircmp);
int ma=v[v.size()-1].second;
cout<<ma<<endl;
for (size_t j = 0; j < 10; j++) {
cout << v.at(j).second <<endl;
if(v[j].second == ma)
cout<<v[j].first<<endl;
}
}
return 0;
}
bool paircmp(const pair<string,int>& firste,const pair<string,int>& seconde ){
return firste.second < seconde.second;
}
This line
vsi v(10);
creates you a std::vector filled with 10 default-constructed std::pair<std::string, int>s. That is, an empty string and zero.
You then push_back other values to your vector but they happen to be sorted after those ten initial elements, probably because they all have positive ints in them.
Therefore, printing the first member of the first ten elements prints ten empty strings.
This is all I can guess from what you have provided. I don't know what you are trying to accomplish with this code.
Try something like
for (const auto& item : v)
{
std::cout << "{ first: '" << item.first << "', "
<< "second: " << item.second << " }\n";
}
to print all elements of the vector v.

Implement a random-number generator using only getpid() and gettimeofday()?

I am using gcc compiler to Implement a random-number generator using only getpid() and gettimeofday(). Here is my code
#include <stdio.h>
#include <sys/time.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
struct timeval tv;
int count;
int i;
int INPUT_MAX =10;
int NO_OF_SAMPLES =10;
gettimeofday(&tv, NULL);
printf("Enter Max: \n");
scanf("%d", &INPUT_MAX);
printf("Enter No. of samples needed: \n");
scanf("%d", &NO_OF_SAMPLES);
/*printf("%ld\n",tv.tv_usec);
printf("PID :%d\n", getpid());*/
for (count = 0; count< NO_OF_SAMPLES; count++) {
printf("%ld\n", (getpid() * tv.tv_usec) % INPUT_MAX + 1);
for (i = 0; i < 1000000; ++i)
{
/* code */
}
}
return 0;
}
I gave a inner for loop for delay purpose but the result what i am getting is always same no. like this
./a.out
Enter Max:
10
Enter No. of samples needed:
10
1
1
1
1
1
1
1
1
1
1
Plz correct me what am i doing wrong?
getpid() is constant during the programs execution, so you get constant values, too.
But even if you use gettimeofday() inside the loop, this likely won't help:
gcc will likely optimize away your delay loop.
even it it's not optimized away, the delays will be very similar and your values won't be very random.
I'd suggest you look up "linear congruential generator", for a simple way to generate more random numbers.
Put gettimeofday in the loop. Look if getpid() is divisible by INPUT_MAX + 1 you will get the same answer always. Instead you can add getpid() (not make any sense though()) to tv.tv_usec.

Resources