File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Dynamic_Programming/2188.Minimum-Time-to-Finish-the-Race Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ using LL = long long ;
2+ class Solution {
3+ static bool cmp (vector<int >&a, vector<int >&b)
4+ {
5+ if (a[1 ]!=b[1 ])
6+ return a[1 ] < b[1 ];
7+ else
8+ return a[0 ] > b[0 ];
9+ }
10+ public:
11+ int minimumFinishTime (vector<vector<int >>& tires, int changeTime, int numLaps)
12+ {
13+ sort (tires.begin (), tires.end (), cmp);
14+ vector<vector<int >>newTires;
15+ for (int i=0 ; i<tires.size (); i++)
16+ {
17+ if (i+1 <tires.size () && tires[i][1 ]==tires[i+1 ][1 ]) continue ;
18+ if (!newTires.empty () && newTires.back ()[0 ] <= tires[i][0 ])
19+ continue ;
20+ newTires.push_back (tires[i]);
21+ }
22+
23+ vector<double >minTime (min (20 , numLaps+1 ), DBL_MAX);
24+ for (int x=1 ; x<minTime.size (); x++)
25+ {
26+ for (int i=0 ; i<newTires.size (); i++)
27+ minTime[x] = min (minTime[x], cal (newTires[i],x));
28+ }
29+
30+ vector<double >dp (numLaps+1 , DBL_MAX);
31+ dp[0 ] = 0 ;
32+ for (int i=1 ; i<=numLaps; i++)
33+ for (int j=i-1 ; i-j<20 && j>=0 ; j--)
34+ {
35+ dp[i] = min (dp[i], dp[j]+minTime[i-j] + (j==0 ?0 :changeTime));
36+ }
37+
38+ return dp[numLaps];
39+ }
40+
41+ double cal (vector<int >tire, int x)
42+ {
43+ double f= tire[0 ], r = tire[1 ];
44+ double ret = f*(pow (r, x)-1 )/(r-1 );
45+ return ret;
46+
47+ }
48+ };
You can’t perform that action at this time.
0 commit comments