Showing posts with label Ternary Search. Show all posts
Showing posts with label Ternary Search. Show all posts

Friday, December 14, 2018

[Codeforces] D. Devu and his Brother

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : D. Devu and his Brother
                    Codeforces Round #251 (Div. 2)
Source            : Codeforces
Category          : Search
Algorithm         : Ternary Search
Verdict           : Accepted 
  1. #include <bits/stdc++.h>  
  2.    
  3. using namespace std;  
  4.    
  5. static const int MAXN = 1e5 + 5;  
  6.    
  7. int n, m;  
  8. long long A[MAXN], B[MAXN];  
  9. long long SA[MAXN], SB[MAXN];  
  10.    
  11. long long f(long long num)  
  12. {  
  13.     int nn = lower_bound(A+1, A+n+1, num) - A;  
  14.     long long changeA = num*(nn-1) - SA[nn-1];  
  15.     int mm = lower_bound(B+1, B+m+1, num+1) - B;  
  16.     long long changeB = 0;  
  17.     if (mm <= m) changeB = SB[m] - SB[mm-1] - num*(m-mm+1);  
  18.     return changeA + changeB;  
  19. }  
  20.    
  21. long long ternary()  
  22. {  
  23.     long long low = 1;  
  24.     long long high = 1e9;  
  25.     while (true)  
  26.     {  
  27.         if (high - low < 3)  
  28.         {  
  29.             long long f1 = f(low);  
  30.             long long f2 = f(low+1);  
  31.             long long f3 = f(high);  
  32.             return min(f1, min(f2, f3));  
  33.         }  
  34.         long long midA = low + (high - low)/3;  
  35.         long long midB = high - (high - low)/3;  
  36.         if (f(midA) <= f(midB)) high = midB;  
  37.         else low = midA;  
  38.     }  
  39. }  
  40.    
  41. int main()  
  42. {  
  43.     cin >> n >> m;  
  44.     for (int i = 1; i <= n; i++) cin >> A[i];  
  45.     sort(A+1, A+n+1);  
  46.     for (int i = 1; i <= n; i++) SA[i] = SA[i-1] + A[i];  
  47.     for (int i = 1; i <= m; i++) cin >> B[i];  
  48.     sort(B+1, B+m+1);  
  49.     for (int i = 1; i <= m; i++) SB[i] = SB[i-1] + B[i];  
  50.     cout << ternary();  
  51. }  

[UVa] 13010 - Galactic taxes

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 13010 - Galactic taxes
Source            : UVa Online Judge
Category          : Graph Theory, Search
Algorithm         : spfa, Ternary Search
Verdict           : Accepted  
  1. #include <bits/stdc++.h>  
  2.    
  3. using namespace std;  
  4.    
  5. #define inf             1e13  
  6.    
  7. static const int maxn = 1000 + 5;  
  8.    
  9. struct node  
  10. {  
  11.     int v, A, B;  
  12.     node() {}  
  13.     node(int v, int A, int B)  
  14.     {  
  15.         this->v = v;  
  16.         this->A = A;  
  17.         this->B = B;  
  18.     }  
  19. };  
  20.    
  21. vector <node> g[maxn];  
  22.    
  23. int N, M;  
  24. double dist[maxn];  
  25. bool inQueue[maxn];  
  26.    
  27. double SPFA(double t)  
  28. {  
  29.     for (int i = 1; i < maxn; i++)  
  30.     {  
  31.         dist[i] = inf;  
  32.         inQueue[i] = 0;  
  33.     }  
  34.     queue <int> PQ;  
  35.     PQ.push(1);  
  36.     dist[1] = 0.0;  
  37.     inQueue[1] = 1;  
  38.     while (!PQ.empty())  
  39.     {  
  40.         int u = PQ.front(); PQ.pop();  
  41.         inQueue[u] = 0;  
  42.         for (auto &it : g[u])  
  43.         {  
  44.             double cost = (double)it.A * t + (double)it.B;  
  45.             int v = it.v;  
  46.             if (dist[v] > dist[u] + cost)  
  47.             {  
  48.                 dist[v] = dist[u] + cost;  
  49.                 if (!inQueue[v])  
  50.                 {  
  51.                     inQueue[v] = 1;  
  52.                     PQ.push(v);  
  53.                 }  
  54.             }  
  55.         }  
  56.     }  
  57.     return dist[N];  
  58. }  
  59.    
  60. void ternary_search()  
  61. {  
  62.     double low = 0.0;  
  63.     double high = 1440.0;  
  64.     double ans = -inf;  
  65.     for (int tol = 0; tol < 300; tol++)  
  66.     {  
  67.         double mid1 = low + (high - low) / 3.0;  
  68.         double mid2 = high - (high - low) / 3.0;  
  69.         double fmid1 = SPFA(mid1);  
  70.         double fmid2 = SPFA(mid2);  
  71.         ans = max(ans, max(fmid1, fmid2));  
  72.         if (fmid1 < fmid2) low = mid1;  
  73.         else high = mid2;  
  74.     }  
  75.     cout << fixed << setprecision(5) << ans << endl;  
  76. }  
  77.    
  78. int main()  
  79. {  
  80.     //freopen("in.txt", "r", stdin);  
  81.    
  82.     while (cin >> N >> M)  
  83.     {  
  84.         for (int i = 0; i < M; i++)  
  85.         {  
  86.             int u, v, A, B;  
  87.             cin >> u >> v >> A >> B;  
  88.             g[u].push_back({v, A, B});  
  89.             g[v].push_back({u, A, B});  
  90.         }  
  91.         ternary_search();  
  92.         for (int i = 0; i < maxn; i++) g[i].clear();  
  93.     }  
  94. }  
  95.