-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinTimeToDate_solution.cpp
More file actions
90 lines (78 loc) · 1.72 KB
/
MinTimeToDate_solution.cpp
File metadata and controls
90 lines (78 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
// Function to check if it is possible
// to complete atleast totaltask using
// current mid total time
bool timePossible(int mid, int totalTask,vector<int>& timeArr,vector<int>& restTime)
{
// Variable to store total task
int curTask = 0;
// Loop to check if it is possible
// to complete totalTask in mid time
for (int i = 0; i < timeArr.size(); i++) {
int totalTime
= timeArr[i] + restTime[i];
curTask += mid / totalTime;
// Possible condition
if (curTask >= totalTask) {
return true;
}
}
// If possible print true
if (curTask >= totalTask) {
return true;
}
// Else return false
return false;
}
// Function to find minimum time taken
int minimumTimeTaken(vector<int>& timeArr,vector<int>& restTime,int totalTask)
{
// The ranges of time
int st = 1;
int end = INT_MAX;
// Variable to store minimum time
int minimumtime = 0;
while (st <= end) {
int mid = st + (end - st) / 2;
// If this mid value is possible
// try to minimise it
if (timePossible(mid, totalTask,
timeArr, restTime))
end = mid - 1;
else
st = mid + 1;
}
// Print the minimum time as answer
return st;
}
void solve()
{
int n,m;
cin>>n>>m;
vector<int> a(m),b(m);
for(int i=0;i<m;i++)
{
cin>>a[i]>>b[i];
}
int ans=minimumTimeTaken(a,b,n);
cout<<ans<<endl;
}
signed main(){
FastIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
//cin>>t;
for(int i=1;i<=t;i++)
{
//cout<<"Case #"<<i<<": ";
solve();
}
return 0;
}