File tree Expand file tree Collapse file tree 3 files changed +174
-0
lines changed Expand file tree Collapse file tree 3 files changed +174
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < conio.h>
3
+ #include < stdlib.h>
4
+ #include < bits/stdc++.h>
5
+
6
+ #define int long long int
7
+ #define pb push_back
8
+ #define ps (x,y ) fixed<<setprecision(y)<<x
9
+ #define mod 1000000007
10
+ #define w (x ) int x; cin>>x; while (x--)
11
+ using namespace std ;
12
+
13
+
14
+ int32_t main () {
15
+
16
+ w (T)
17
+ {
18
+ int n,c,ans;
19
+ cin>>n>>c;
20
+ int arr[n];
21
+ for (int i=0 ;i<n;i++) cin>>arr[i];
22
+
23
+ sort (arr,arr+n);
24
+ int low=arr[0 ],high=arr[n-1 ]-arr[0 ];
25
+
26
+ while (low<=high)
27
+ {
28
+ int prev=arr[0 ],mid=(low+high)>>1 ;
29
+ int cow_count=1 ;
30
+ for (int i=1 ;i<n;i++)
31
+ {
32
+ if (arr[i]-prev >= mid)
33
+ {
34
+ cow_count++; prev=arr[i];
35
+ if (c==cow_count)break ;
36
+ }
37
+ }
38
+ if (cow_count>=c)
39
+ {
40
+ low=mid+1 ;ans=mid;
41
+ }
42
+ else
43
+ {
44
+ high=mid-1 ;
45
+ }
46
+
47
+ }
48
+ cout<<ans<<endl;
49
+
50
+ }
51
+
52
+
53
+ return 0 ;
54
+ }
55
+
56
+
57
+
58
+
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < stdlib.h>
3
+ #include < bits/stdc++.h>
4
+
5
+ #define int long long int
6
+ #define pb push_back
7
+ #define ps (x,y ) fixed<<setprecision(y)<<x
8
+ #define mod 1000000007
9
+ #define w (x ) int x; cin>>x; while (x--)
10
+ using namespace std ;
11
+
12
+ int mid_wood (int arr[],int m,int mid,int n)
13
+ {int wood=0 ;
14
+ cout<<" Inside fn" ;
15
+ for (int i=0 ;i<n;i++)
16
+ {
17
+ if (arr[i]-mid>0 )
18
+ {
19
+ wood=wood+arr[i]-mid;
20
+ }
21
+ }
22
+ return wood;
23
+ }
24
+
25
+ int32_t main () {
26
+
27
+ w (T)
28
+ {
29
+ int n,m,ans;
30
+ cin>>n>>m;
31
+ int arr[n];
32
+ for (int i=0 ;i<n;i++) cin>>arr[i];
33
+
34
+ sort (arr,arr+n);
35
+ int low=0 ,high=arr[n-1 ];
36
+ while (low<=high)
37
+ {cout<<" inside while" ;
38
+ int mid=(low+high)/2 ;6
39
+ if (m<=mid_wood (arr,m,mid,n))
40
+ {
41
+ ans=mid;low=mid+1 ;
42
+ }
43
+ else high=mid-1 ;
44
+ }
45
+ cout<<ans<<endl;
46
+
47
+ }
48
+
49
+
50
+ return 0 ;
51
+ }
52
+
53
+
54
+
55
+
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < stdlib.h>
3
+ #include < bits/stdc++.h>
4
+
5
+ #define int long long int
6
+ #define pb push_back
7
+ #define ps (x,y ) fixed<<setprecision(y)<<x
8
+ #define mod 1000000007
9
+ #define w (x ) int x; cin>>x; while (x--)
10
+ using namespace std ;
11
+
12
+
13
+ int findmax (int arr[100 ],int n)
14
+ {
15
+ int maxi=arr[0 ];
16
+ for (int i=1 ;i<n;i++)
17
+ {
18
+ maxi=max (arr[i],maxi);
19
+ }
20
+ return maxi;
21
+ }
22
+
23
+ bool isParatha (int mid,int paratha,int arr[100 ],int n)
24
+ {
25
+ int time,count=0 ;
26
+ for (int i=0 ;i<n;i++)
27
+ {
28
+ int r=arr[i];time=r;int j=2 ;
29
+ while (time<=mid)
30
+ {
31
+ count++;time=time+(arr[i]*j);
32
+ j++;
33
+ }
34
+ }
35
+ return paratha<=count;
36
+ }
37
+ int32_t main ()
38
+ {
39
+
40
+ w (t)
41
+ {
42
+ int n,chef,arr[100 ];
43
+ cin>>n>>chef;
44
+ for (int i=0 ;i<chef;i++)cin>>arr[i];
45
+
46
+ int low=0 ,x=findmax (arr,chef),ans=0 ;
47
+ int high=(n/2 )*((2 *x)+(n-1 )*x);
48
+
49
+ while (low<=high)
50
+ {
51
+ int mid=(low+high)/2 ;
52
+ if (isParatha (mid,n,arr,chef))
53
+ {
54
+ ans=mid;high=mid-1 ;
55
+ }
56
+ else low=mid+1 ;
57
+ }
58
+ cout<<ans<<" \n " <<endl;
59
+ }
60
+ }
61
+
You can’t perform that action at this time.
0 commit comments