@@ -39,68 +39,55 @@ that it's optimal for every machine to work simultaneously. Then, in $ans$ time,
39
39
machine $i$ can create $\lfloor \frac{ ans }{ k_i } \rfloor$ products.
40
40
41
41
Overall, the $n$ machines can create
42
- $\sum_ { i = 1 } ^{ n } \lfloor \frac{ ans }{ k_i } \rfloor$ products. If this sum
42
+ $\sum_ { i = 1 } ^{ n } \lfloor \frac{ ans }{ k_i } \rfloor$ products. If this sum
43
43
$\geq t$, then $ans$ is valid.
44
44
45
45
## Implementation
46
46
47
- ** Time Complexity:** $\mathcal{ O } (n \log t )$, where $t $ is the largest time we need to consider.
47
+ ** Time Complexity:** $\mathcal{ O } (N \log T )$, where $T $ is the largest time we need to consider.
48
48
49
49
<LanguageSection >
50
50
<CPPSection >
51
51
52
52
``` cpp
53
- #include < bits/stdc++.h>
54
- using namespace std ;
55
- using ll = long long ;
56
- using vi = vector<int >;
57
- #define pb push_back
58
- #define rsz resize
59
- #define all(x) begin(x), end(x)
60
- #define sz(x) (int)(x).size()
61
- using pi = pair<int, int >;
62
- #define f first
63
- #define s second
64
- #define mp make_pair
65
- void setIO(string name = "") { // name is nonempty for USACO file I/O
66
- ios_base ::sync_with_stdio (0 );
67
- cin .tie (0 ); // see Fast Input & Output
68
- if (sz (name )) {
69
- freopen((name + " .in" ).c_str(), " r" , stdin); // see Input & Output
70
- freopen((name + " .out" ).c_str(), " w" , stdout);
71
- }
72
- }
53
+ #include < climits>
54
+ #include < iostream>
55
+ #include < vector>
56
+
57
+ using std::vector;
73
58
74
59
int main () {
75
- setIO ();
76
60
int n;
77
- ll t, mn = INT_MAX;
78
- cin >> n >> t;
79
- vector<ll > k(n);
80
- for (int i = 0; i < n; i++) {
81
- cin >> k [i ];
82
- mn = min (mn , k [i ]);
61
+ long long t;
62
+ std::cin >> n >> t;
63
+ vector<int > k(n);
64
+
65
+ int mn = INT_MAX;
66
+ for (int &x : k) {
67
+ std ::cin >> x ;
68
+ mn = std ::min (mn , x );
83
69
}
84
- ll lo = 0;
85
- ll hi = mn * t;
86
- ll ans = 0;
70
+
71
+ long long lo = 0;
72
+ long long hi = mn * t;
73
+ long long res = 0;
87
74
while (lo <= hi) {
88
- ll mid = (lo + hi ) / 2 ;
89
- ll sum = 0 ;
75
+ long long mid = (lo + hi ) / 2 ;
76
+ long long sum = 0 ;
90
77
for (int i = 0 ; i < n ; i ++ ) {
91
78
sum += (mid / k[i ]);
92
- if (sum >= t) { // deal with overflow
93
- break;
94
- }
79
+ if (sum >= t) { break; }
95
80
}
81
+
96
82
if (sum >= t) {
97
- ans = mid;
83
+ res = mid;
98
84
hi = mid - 1;
99
85
} else {
100
86
lo = mid + 1;
101
87
}
102
88
}
103
- cout << ans << "\n";
89
+
90
+ std::cout << res << std ::endl ;
104
91
}
105
92
` ` `
106
93
@@ -157,7 +144,7 @@ _, goal = map(int, input().split())
157
144
machines = list (map (int , input ().split ()))
158
145
159
146
lo = 0
160
- hi = int(1e18)
147
+ hi = 10 ** 18
161
148
ans = 0
162
149
while lo <= hi :
163
150
mid = (lo + hi ) // 2
@@ -171,6 +158,7 @@ while lo <= hi:
171
158
hi = mid - 1
172
159
else :
173
160
lo = mid + 1
161
+
174
162
print (ans )
175
163
` ` `
176
164
0 commit comments