Author : Dipu Kumar Mohanto
CSE, Batch - 6
BRUR.
Problem Statement : Help Optimus!
Source : toph.co
Category : Search
Algorithm : Ternary Search
Verdict : Accepted
- #include <bits/stdc++.h>
-
- using namespace std;
-
- #define ll long long
-
- ll D, F, L, K;
-
- inline ll f(ll d)
- {
- ll ans = d*K - (D+F)*d*d + L;
- return ans;
- }
-
- inline ll ternarySearch()
- {
- ll lo = 1;
- ll hi = 1e6;
- while (true)
- {
- if (hi-lo < 3)
- {
- ll max_1 = f(lo);
- ll max_2 = f(lo+1);
- ll max_3 = f(hi);
- ll maxi = max(max_1, max(max_2, max_3));
- if (maxi == max_1) return lo;
- if (maxi == max_2) return lo+1;
- if (maxi == max_3) return hi;
- }
- ll mid1 = lo + (hi-lo)/3;
- ll mid2 = hi - (hi-lo)/3;
- if (f(mid1) < f(mid2)) lo = mid1;
- else hi = mid2;
- }
- }
-
- int main()
- {
-
-
- int tc;
- scanf("%d", &tc);
- for (int tcase = 1; tcase <= tc; tcase++)
- {
- scanf("%lld %lld %lld %lld", &D, &F, &L, &K);
- ll ans = solve();
- printf("%lld\n", ans);
- }
- }
-
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.