Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2aa56b4

Browse files
Added more stuff
1 parent e7bc4de commit 2aa56b4

File tree

2 files changed

+127
-82
lines changed

2 files changed

+127
-82
lines changed

‎DP and bitmasking/Candy.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
3+
Name: Mehul Chaturvedi
4+
IIT-Guwahati
5+
6+
*/
7+
8+
/*
9+
PROBLEM STATEMENT
10+
Gary is a teacher at XYZ school. To reward his N students he bought a packet of N candies all with different flavours. But the problem is some students like certain flavour while some doesn't. Now Gary wants to know the number of ways he can distribute these N candies to his N students such that every student gets exactly one candy he likes.
11+
Input Format :
12+
Line 1 : An integer N (1<= N <= 16) denoting number of students and candies.
13+
Next N lines : N integers describing the preferences of one student. 1 at i'th (0 <= i < N) position denotes that this student likes i'th candy , 0 means he doesn't.
14+
Assume input to be 0-indexed based.
15+
Output Format :
16+
Return the number of ways Gary can distribute these N candies to his N students such that every student gets exactly one candy he likes.``
17+
Sample Input:
18+
3
19+
1 1 1
20+
1 1 1
21+
1 1 1
22+
Sample Output:
23+
6
24+
*/
25+
26+
#include <bits/stdc++.h>
27+
using namespace std;
28+
29+
typedef long long ll;
30+
typedef unordered_map<int, int> umapii;
31+
typedef unordered_map<int, bool> umapib;
32+
typedef unordered_map<string, int> umapsi;
33+
typedef unordered_map<string, string> umapss;
34+
typedef map<string, int> mapsi;
35+
typedef map<pair<int, int>, int> mappiii;
36+
typedef map<int, int> mapii;
37+
typedef pair<int, int> pii;
38+
typedef pair<long long, long long> pll;
39+
typedef unordered_set<int> useti;
40+
41+
#define uset unordered_set
42+
#define it iterator
43+
#define mp make_pair
44+
#define pb push_back
45+
#define all(x) (x).begin(), (x).end()
46+
#define f first
47+
#define s second
48+
#define MOD 1000000007
49+
50+
51+
int main( int argc , char ** argv )
52+
{
53+
ios_base::sync_with_stdio(false) ;
54+
cin.tie(NULL) ;
55+
56+
57+
58+
59+
return 0 ;
60+
61+
62+
63+
}

‎Greedy/Weightedjob.cpp‎

Lines changed: 64 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -35,91 +35,73 @@ Sample Output
3535

3636

3737
#include <bits/stdc++.h>
38-
3938
using namespace std;
39+
typedef long long ll;
4040

41-
// A job has start time, finish time and profit.
42-
class Job
43-
{public:
44-
int start, finish, profit;
45-
};
46-
47-
// A utility function that is used for sorting events
48-
// according to finish time
49-
bool jobComparataor(Job s1, Job s2)
50-
{
51-
return (s1.finish < s2.finish);
52-
}
53-
54-
// Find the latest job (in sorted array) that doesn't
55-
// conflict with the job[i]. If there is no compatible job,
56-
// then it returns -1.
57-
int latestNonConflict(Job arr[], int i)
58-
{
59-
for (int j=i-1; j>=0; j--)
60-
{
61-
if (arr[j].finish <= arr[i-1].start)
62-
return j;
63-
}
64-
return -1;
65-
}
66-
67-
// A recursive function that returns the maximum possible
68-
// profit from given array of jobs. The array of jobs must
69-
// be sorted according to finish time.
70-
int findMaxProfitRec(Job arr[], int n)
71-
{
72-
// Base case
73-
if (n == 1) return arr[n-1].profit;
74-
75-
// Find profit when current job is inclueded
76-
int inclProf = arr[n-1].profit;
77-
int i = latestNonConflict(arr, n);
78-
if (i != -1)
79-
inclProf += findMaxProfitRec(arr, i+1);
80-
81-
// Find profit when current job is excluded
82-
int exclProf = findMaxProfitRec(arr, n-1);
83-
84-
return max(inclProf, exclProf);
85-
}
86-
87-
// The main function that returns the maximum possible
88-
// profit from given array of jobs
89-
int findMaxProfit(Job arr[], int n)
90-
{
91-
// Sort jobs according to finish time
92-
sort(arr, arr+n, jobComparataor);
93-
94-
return findMaxProfitRec(arr, n);
95-
}
96-
int main( int argc , char ** argv )
97-
{
98-
ios_base::sync_with_stdio(false) ;
99-
cin.tie(NULL) ;
100-
101-
int n;
102-
cin>>n;
103-
104-
Job* arr = new Job[n];
105-
for (int i = 0; i < n; ++i)
106-
{
107-
int a,b,c;
108-
cin>>a>>b>>c;
109-
Job temp;
110-
temp.start = a;
111-
temp.finish = b;
112-
temp.profit = c;
113-
114-
arr[i] = temp;
115-
}
116-
117-
cout << findMaxProfit(arr, n) << '\n';
118-
119-
120-
121-
return 0 ;
41+
struct job{
42+
ll start, finish, profit;
12243

44+
job(ll s, ll f, ll p){
45+
start = s;
46+
finish = f;
47+
profit = p;
48+
}
49+
};
12350

51+
bool compare(job a, job b){
52+
return a.finish < b.finish;
53+
}
12454

55+
ll search(vector<job> *input, ll limit, ll si, ll ei){
56+
if(si > ei) return -1;
57+
58+
if(si == ei){
59+
if((input->at(si)).finish <= limit) return si;
60+
else return -1;
61+
}
62+
63+
ll mid = (si+ei)/2;
64+
if((input->at(mid)).finish <= limit){
65+
ll answer = search(input, limit, mid+1, ei);
66+
if(answer == -1) return mid;
67+
else return answer;
68+
}
69+
else return search(input, limit, si, mid-1);
12570
}
71+
72+
int main(){
73+
ll n;
74+
cin >> n;
75+
76+
vector<job> input;
77+
for(ll i = 0; i < n; i++){
78+
ll s, f, p;
79+
cin >> s >> f >> p;
80+
input.push_back(job(s, f, p));
81+
}
82+
83+
sort(input.begin(), input.end(), compare);
84+
85+
ll *dp = new ll[n];
86+
dp[0] = input[0].profit;
87+
for(ll i = 1; i < n; i++){
88+
ll include = input[i].profit;
89+
90+
ll id = -1;
91+
id = search(&input, input[i].start, 0, i-1);
92+
// for(ll j = i-1; j >= 0; j--){
93+
// if(input[j].finish <= input[i].start){
94+
// id = j;
95+
// break;
96+
// }
97+
// }
98+
99+
if(id != -1){
100+
include += dp[id];
101+
}
102+
103+
dp[i] = max(dp[i-1], include);
104+
}
105+
106+
cout << dp[n-1] << endl;
107+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /