-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRajuRastogi.cpp
More file actions
60 lines (58 loc) · 1.75 KB
/
RajuRastogi.cpp
File metadata and controls
60 lines (58 loc) · 1.75 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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,x;
cin>>n>>x;
int a[n];
int min=INT_MAX;
int max=INT_MIN;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]<min)
{
min=a[i];
}
if(a[i]>max)
{
max=a[i];
}
}
int ans=0;
while(min<max)
{
int mid=min+(max-min)/2;
int sum=0;
for(int i=0;i<n;i++)
{
int diff=a[i]-mid;
//a[i] means the number of free hours on the ith day
//mid represents number of movie hours. So a[i] - mid represents the number of study hours = no. of questions per day.
if(diff>=0)
//that is, if a[i] >= mid, means that number of free hours is greater than equal to the supposed watch time of movie,
//hence this surplus can be utilised for solving questions. The surplus time is added to sum.
{
sum=sum+diff;
}
}
if(sum<x)
//If the surplus hours available for study are less than the total hours required for study, we need to reduce the movie time
//that is, max = mid, therefore mid = (max+min)/2 is reduced
{
max=mid;
}
if(sum>=x)
//If number of hours available for study is greater than number of hours required, some more time can be allocated to movies,
//therefore, min = mid + 1, therefore mid increases.
{
min=mid+1;
if(mid>ans)
//We have to find the maximum possible mid, which we store in ans, and update it as and when a higher value is found possible.
{
ans=mid;
}
}
}
cout<<ans;
}