Skip to content

Commit 84a9d68

Browse files
committed
Added more problems
1 parent 2edfc25 commit 84a9d68

File tree

8 files changed

+358
-2
lines changed

8 files changed

+358
-2
lines changed

DP1(Very important)/Hasanandtrip.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Hasan has finally finished his final exams and he decided to go in a trip among cities in Syria.
11+
There are N cities in Syria and they are numbered from 1 to N, each city has coordinates on plane, i-th city is in (Xi, Yi).
12+
Hasan is in first city and he wants to visit some cities by his car in the trip but the final destination should be N-th city and the sequence of cities he will visit should be increasing in index (i.e. if he is in city i he can move to city j if and only if i < j ).
13+
Visiting i-th city will increase Hasan's happiness by Fi units (including first and last cities), also Hasan doesn't like traveling too much, so his happiness will decrease by total distance traveled by him.
14+
Help Hasan by choosing a sequence of cities to visit which maximizes his happiness.
15+
Input format:
16+
First line contain integer N.
17+
Next N lines contains three integers each, i-th line contains coordinates of i-th city Xi, Yi and Fi.
18+
Output format:
19+
Output one number rounded to 6 digits after floating point, the maximum possible happiness Hasan can get. Note: If answer is 2 print 2.000000
20+
Constraints:
21+
1 <= N <= 3,000
22+
0 <= Xi, Yi, Fi <= 100,000
23+
Sample Input
24+
3
25+
0 0 1
26+
3 1 1
27+
6 0 9
28+
Sample Output
29+
4.675445
30+
31+
*/
32+
#include <bits/stdc++.h>
33+
34+
using namespace std;
35+
36+
class city{
37+
public:
38+
int x;
39+
int y;
40+
int happiness;
41+
};
42+
double distance(city a, city b){
43+
double ans = 0;
44+
long long X = (long long)pow(a.x-b.x, 2);
45+
long long Y = (long long)pow(a.y-b.y, 2);
46+
47+
ans = (double)sqrt(X+Y);
48+
return ans;
49+
}
50+
double go(vector<city> v){
51+
int n = v.size();
52+
vector<double> dp(n, 0);
53+
dp[0] = v[0].happiness;
54+
55+
for (int i = 1; i < n; ++i)
56+
{
57+
double temp = -1e9;
58+
for (int j = 0; j <i; ++j)
59+
{
60+
temp = max(temp, dp[j]+v[i].happiness-distance(v[i], v[j]));
61+
}
62+
dp[i] = temp;
63+
//cout << "dp[i] "<<i<<" "<<dp[i] << '\n';
64+
65+
66+
//dp[i] = max(dp[0]+v[i].happiness-distance(v[0],v[i]), dp[i-1]+v[i].happiness-distance(v[i-1], v[i]));
67+
}
68+
69+
return dp[n-1];
70+
}
71+
72+
int main( int argc , char ** argv )
73+
{
74+
ios_base::sync_with_stdio(false) ;
75+
cin.tie(NULL) ;
76+
77+
int n;
78+
cin>>n;
79+
std::vector<city> v;
80+
for (int i = 0; i < n; ++i)
81+
{
82+
int a;
83+
int b;
84+
int c;
85+
cin>>a>>b>>c;
86+
city temp;
87+
temp.x = a;
88+
temp.y = b;
89+
temp.happiness = c;
90+
v.push_back(temp);
91+
}
92+
93+
//cout << distance(v[0], v[1]) << '\n';
94+
95+
cout <<fixed << setprecision(6)<< go(v) << '\n';
96+
97+
98+
return 0 ;
99+
100+
101+
102+
}

DP1(Very important)/Test.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
8
2-
1 3 3 4 5 5 6 6
1+
3
2+
1 2 3

DP1(Very important)/Vanya and gcd

36.2 KB
Binary file not shown.

DP1(Very important)/Vanya and gcd.cpp

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Vanya has been studying all day long about sequences and other Complex Mathematical Terms. She thinks she has now become really good at it. So, her friend Vasya decides to test her knowledge and keeps the following challenge it front of her:
11+
Vanya has been given an integer array A of size N. Now, she needs to find the number of increasing sub-sequences of this array with length ≥1 and GCD=1. A sub-sequence of an array is obtained by deleting some (or none) elements and maintaining the relative order of the rest of the elements. As the answer may be large, print it Modulo 109+7
12+
She finds this task really easy, and thinks that you can do it too. Can you?
13+
Input Format:
14+
The first line contains a single integer N denoting size of array A. The next line contains N space separated integers denoting the elements of array A
15+
Output Format:
16+
Print the required answer Modulo 10^9+7 on a single line.
17+
Constraints:
18+
1≤N≤500
19+
20+
1≤A[i]≤100
21+
Sample Input
22+
3
23+
1 2 3
24+
Sample Output
25+
5
26+
*/
27+
28+
#include <bits/stdc++.h>
29+
30+
using namespace std;
31+
int go(vector<int> v){
32+
int n = v.size();
33+
vector<vector<int>> dp(101, vector<int>(n, 0));
34+
35+
for (int i = 0; i < n; ++i)
36+
{
37+
dp[v[i]][i] = 1;
38+
}
39+
40+
for (int i = 0; i < n; ++i)
41+
{
42+
for (int j = 0; j < i; ++j)
43+
{
44+
if (v[i]>v[j])
45+
{
46+
for (int k = 1; k < 101; ++k)
47+
{
48+
int gcn = __gcd(k, v[i]);
49+
dp[gcn][i] = (dp[gcn][i]+dp[k][j])%1000000007;
50+
}
51+
}
52+
}
53+
}
54+
55+
int ans = 0;
56+
57+
// for (int i = 1; i < 101; ++i)
58+
// {
59+
// for (int j = 0; j < n; ++j)
60+
// {
61+
// cout<<dp[i][j]<<" ";
62+
// }
63+
// cout <<'\n';
64+
// }
65+
66+
for (int i = 0; i < n; ++i)
67+
{
68+
ans=(ans+ dp[1][i])%1000000007;
69+
}
70+
71+
return ans%1000000007;
72+
73+
74+
75+
76+
}
77+
78+
int main( int argc , char ** argv )
79+
{
80+
ios_base::sync_with_stdio(false) ;
81+
cin.tie(NULL) ;
82+
83+
84+
int n;
85+
cin>>n;
86+
vector<int> v(n, 0);
87+
88+
for (int i = 0; i < n; ++i)
89+
{
90+
cin>>v[i];
91+
}
92+
93+
cout << go(v) << '\n';
94+
95+
96+
97+
return 0 ;
98+
99+
100+
101+
}

DP1(Very important)/a.out

-8.28 KB
Binary file not shown.

DP1(Very important)/minchoc

22.7 KB
Binary file not shown.

DP1(Very important)/minchoc.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Noor is a teacher. She wants to give some chocolates to the students in her class. All the students sit in a line and each of them has a score according to performance. Noor wants to give at least 1 chocolate to each student. She distributes chocolates to them such that If two students sit next to each other then the one with the higher score must get more chocolates. Noor wants to save money, so she wants to minimise the total number of chocolates.
11+
Note that when two students have equal score they are allowed to have different number of chocolates.
12+
Input Format:
13+
First Line: Integer N, the number of students in Noor’s class.
14+
Second Line: Each of the student's score separated by spaces.
15+
Output Format:
16+
Output a single line containing the minimum number of chocolates Noor must give.
17+
Input Constraints
18+
1 <= N <= 100000
19+
1 <= score <= 100000
20+
Sample Input:
21+
4
22+
1 4 4 6
23+
sample Output:
24+
6
25+
Sample Input:
26+
3
27+
8 7 5
28+
sample Output:
29+
6
30+
*/
31+
32+
#include <bits/stdc++.h>
33+
34+
using namespace std;
35+
36+
int getMin(int *arr, int n){
37+
//left case
38+
39+
std::vector<int> dp(n, 0);
40+
dp[0] = 1;
41+
for (int i = 1; i < n; ++i)
42+
{
43+
if(arr[i]>arr[i-1])
44+
dp[i] = dp[i-1]+1;
45+
else
46+
dp[i] = 1; // made changes here
47+
}
48+
49+
50+
dp[n-1] = max(dp[n-1], 1);
51+
52+
for (int i = n-2; i >= 0; --i)
53+
{
54+
if(arr[i+1]<arr[i])
55+
dp[i] = max(dp[i], dp[i+1]+1);
56+
// else
57+
// dp[i] = max(dp[i+1]-1, dp[i]);
58+
}
59+
int sum = 0;
60+
61+
for (int i = 0; i < n; ++i)
62+
{
63+
sum+=dp[i];
64+
}
65+
66+
return sum;
67+
68+
}
69+
70+
int main( int argc , char ** argv )
71+
{
72+
ios_base::sync_with_stdio(false) ;
73+
cin.tie(NULL) ;
74+
75+
int n;
76+
cin >> n;
77+
int *arr = new int[n];
78+
for(int i = 0; i < n; i++){
79+
cin >> arr[i];
80+
}
81+
82+
cout << getMin(arr, n);
83+
delete []arr;
84+
85+
86+
87+
return 0 ;
88+
89+
90+
91+
}

DP1(Very important)/mincount.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Given an integer N, find and return the count of minimum numbers, sum of whose squares is equal to N.
11+
That is, if N is 4, then we can represent it as : {1^2 + 1^2 + 1^2 + 1^2} and {2^2}. Output will be 1, as 1 is the minimum count of numbers required.
12+
Note : x^y represents x raise to the power y.
13+
Input Format :
14+
Integer N
15+
Output Format :
16+
Required minimum count
17+
Constraints :
18+
1 <= N <= 1000
19+
Sample Input 1 :
20+
12
21+
Sample Output 1 :
22+
3
23+
Sample Output 1 Explanation :
24+
12 can be represented as :
25+
1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1
26+
1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 1^1 + 2^2
27+
1^1 + 1^1 + 1^1 + 1^1 + 2^2 + 2^2
28+
2^2 + 2^2 + 2^2
29+
As we can see, the output should be 3.
30+
Sample Input 2 :
31+
9
32+
Sample Output 2 :
33+
1
34+
*/
35+
36+
37+
#include <bits/stdc++.h>
38+
39+
using namespace std;
40+
41+
int minCount(int n){
42+
//non Recursive solution
43+
44+
45+
46+
}
47+
48+
int main( int argc , char ** argv )
49+
{
50+
ios_base::sync_with_stdio(false) ;
51+
cin.tie(NULL) ;
52+
53+
int num;
54+
cin >> num;
55+
cout << minCount(num)<<endl;
56+
57+
58+
return 0 ;
59+
60+
61+
62+
}

0 commit comments

Comments
 (0)