给定一个容量为 C 升的油箱,它在启动时完全充满。每天的水箱里都装满了 l 升水,如果溢出,多余的水就会被扔掉。现在在第 i 天,取出 i 升水饮用。我们需要找出坦克第一次变空的日期。
例子:
Input : Capacity = 5
l = 2
Output : 4
At the start of 1st day, water in tank = 5
and at the end of the 1st day = (5 - 1) = 4
At the start of 2nd day, water in tank = 4 + 2 = 6
but tank capacity is 5 so water = 5
and at the end of the 2nd day = (5 - 2) = 3
At the start of 3rd day, water in tank = 3 + 2 = 5
and at the end of the 3rd day = (5 - 3) = 2
At the start of 4th day, water in tank = 2 + 2 = 4
and at the end of the 4th day = (4 - 4) = 0
So final answer will be 4
我们可以看到水箱在开始 (l + 1) 天时会充满,因为取出的水少于填充的水。之后,水箱中的水每天将减少 1 升,并且在第 (l + 1 + i) 天 (C – (i)(i + 1) / 2) 升水将留在饮用前。
现在我们需要找到最短的一天 (l + 1 + K),即使在将水箱加满 l 升之后,水箱中的水也少于 l,即在第 (l + 1 + K – 1) 天水箱变空所以我们的目标是找到最小的 K 使得,
C – K(K + 1) / 2 <= l
我们可以使用二分搜索来解决上面的方程,然后 (l + K) 将是我们的答案。解决方案的总时间复杂度为 O(log C)
C++
// C/C++ code to find number of days after which
// tank will become empty
#include
using namespace std;
// Utility method to get sum of first n numbers
int getCumulateSum(int n)
{
return (n * (n + 1)) / 2;
}
// Method returns minimum number of days after
// which tank will become empty
int minDaysToEmpty(int C, int l)
{
// if water filling is more than capacity then
// after C days only tank will become empty
if (C <= l)
return C;
// initialize binary search variable
int lo = 0;
int hi = 1e4;
int mid;
// loop until low is less than high
while (lo < hi) {
mid = (lo + hi) / 2;
// if cumulate sum is greater than (C - l)
// then search on left side
if (getCumulateSum(mid) >= (C - l))
hi = mid;
// if (C - l) is more then search on
// right side
else
lo = mid + 1;
}
// final answer will be obtained by adding
// l to binary search result
return (l + lo);
}
// Driver code to test above methods
int main()
{
int C = 5;
int l = 2;
cout << minDaysToEmpty(C, l) << endl;
return 0;
}
Java
// Java code to find number of days after which
// tank will become empty
public class Tank_Empty {
// Utility method to get sum of first n numbers
static int getCumulateSum(int n)
{
return (n * (n + 1)) / 2;
}
// Method returns minimum number of days after
// which tank will become empty
static int minDaysToEmpty(int C, int l)
{
// if water filling is more than capacity then
// after C days only tank will become empty
if (C <= l)
return C;
// initialize binary search variable
int lo = 0;
int hi = (int)1e4;
int mid;
// loop until low is less than high
while (lo < hi) {
mid = (lo + hi) / 2;
// if cumulate sum is greater than (C - l)
// then search on left side
if (getCumulateSum(mid) >= (C - l))
hi = mid;
// if (C - l) is more then search on
// right side
else
lo = mid + 1;
}
// final answer will be obtained by adding
// l to binary search result
return (l + lo);
}
// Driver code to test above methods
public static void main(String args[])
{
int C = 5;
int l = 2;
System.out.println(minDaysToEmpty(C, l));
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 code to find number of days
# after which tank will become empty
# Utility method to get
# sum of first n numbers
def getCumulateSum(n):
return int((n * (n + 1)) / 2)
# Method returns minimum number of days
# after which tank will become empty
def minDaysToEmpty(C, l):
# if water filling is more than
# capacity then after C days only
# tank will become empty
if (C <= l) : return C
# initialize binary search variable
lo, hi = 0, 1e4
# loop until low is less than high
while (lo < hi):
mid = int((lo + hi) / 2)
# if cumulate sum is greater than (C - l)
# then search on left side
if (getCumulateSum(mid) >= (C - l)):
hi = mid
# if (C - l) is more then
# search on right side
else:
lo = mid + 1
# Final answer will be obtained by
# adding l to binary search result
return (l + lo)
# Driver code
C, l = 5, 2
print(minDaysToEmpty(C, l))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# code to find number
// of days after which
// tank will become empty
using System;
class GFG
{
// Utility method to get
// sum of first n numbers
static int getCumulateSum(int n)
{
return (n * (n + 1)) / 2;
}
// Method returns minimum
// number of days after
// which tank will become empty
static int minDaysToEmpty(int C,
int l)
{
// if water filling is more
// than capacity then after
// C days only tank will
// become empty
if (C <= l)
return C;
// initialize binary
// search variable
int lo = 0;
int hi = (int)1e4;
int mid;
// loop until low is
// less than high
while (lo < hi)
{
mid = (lo + hi) / 2;
// if cumulate sum is
// greater than (C - l)
// then search on left side
if (getCumulateSum(mid) >= (C - l))
hi = mid;
// if (C - l) is more then
// search on right side
else
lo = mid + 1;
}
// final answer will be
// obtained by adding
// l to binary search result
return (l + lo);
}
// Driver code
static public void Main ()
{
int C = 5;
int l = 2;
Console.WriteLine(minDaysToEmpty(C, l));
}
}
// This code is contributed by ajit
Javascript
C++
// C/C++ code to find number of days after which
// tank will become empty
#include
using namespace std;
// Method returns minimum number of days after
// which tank will become empty
int minDaysToEmpty(int C, int l)
{
if (l >= C)
return C;
double eq_root = (std::sqrt(1+8*(C-l)) - 1) / 2;
return std::ceil(eq_root) + l;
}
// Driver code to test above methods
int main()
{
cout << minDaysToEmpty(5, 2) << endl;
cout << minDaysToEmpty(6514683, 4965) << endl;
return 0;
}
Java
// Java code to find number of days
// after which tank will become empty
import java.lang.*;
class GFG {
// Method returns minimum number of days
// after which tank will become empty
static int minDaysToEmpty(int C, int l)
{
if (l >= C) return C;
double eq_root = (Math.sqrt(1 + 8 *
(C - l)) - 1) / 2;
return (int)(Math.ceil(eq_root) + l);
}
// Driver code
public static void main(String[] args)
{
System.out.println(minDaysToEmpty(5, 2));
System.out.println(minDaysToEmpty(6514683, 4965));
}
}
// This code is contributed by Smitha Dinesh Semwal.
Python3
# Python3 code to find number of days
# after which tank will become empty
import math
# Method returns minimum number of days
# after which tank will become empty
def minDaysToEmpty(C, l):
if (l >= C): return C
eq_root = (math.sqrt(1 + 8 * (C - l)) - 1) / 2
return math.ceil(eq_root) + l
# Driver code
print(minDaysToEmpty(5, 2))
print(minDaysToEmpty(6514683, 4965))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# code to find number
// of days after which
// tank will become empty
using System;
class GFG
{
// Method returns minimum
// number of days after
// which tank will become empty
static int minDaysToEmpty(int C,
int l)
{
if (l >= C) return C;
double eq_root = (Math.Sqrt(1 + 8 *
(C - l)) - 1) / 2;
return (int)(Math.Ceiling(eq_root) + l);
}
// Driver code
static public void Main ()
{
Console.WriteLine(minDaysToEmpty(5, 2));
Console.WriteLine(minDaysToEmpty(6514683,
4965));
}
}
// This code is contributed by ajit
PHP
= $C)
return $C;
$eq_root = (int)sqrt(1 + 8 *
($C - $l) - 1) / 2;
return ceil($eq_root) + $l;
}
// Driver code
echo minDaysToEmpty(5, 2), "\n";
echo minDaysToEmpty(6514683,
4965), "\n";
// This code is contributed
// by akt_mit
?>
Javascript
输出:
4
替代解决方案:
它可以用一个简单的公式在数学上解决:
让我们假设 C>L。令 d 是 L th 之后当油箱变空时的天数。在此期间,将有(d-1)次加注和d 次提取。
因此我们需要解这个方程:
所有提款的总和是等差数列的总和,因此:
判别式 = 1+8(CL)>0,因为 C>L。
跳过负根,我们得到以下公式:
因此,最终的alwer是:
C++
// C/C++ code to find number of days after which
// tank will become empty
#include
using namespace std;
// Method returns minimum number of days after
// which tank will become empty
int minDaysToEmpty(int C, int l)
{
if (l >= C)
return C;
double eq_root = (std::sqrt(1+8*(C-l)) - 1) / 2;
return std::ceil(eq_root) + l;
}
// Driver code to test above methods
int main()
{
cout << minDaysToEmpty(5, 2) << endl;
cout << minDaysToEmpty(6514683, 4965) << endl;
return 0;
}
Java
// Java code to find number of days
// after which tank will become empty
import java.lang.*;
class GFG {
// Method returns minimum number of days
// after which tank will become empty
static int minDaysToEmpty(int C, int l)
{
if (l >= C) return C;
double eq_root = (Math.sqrt(1 + 8 *
(C - l)) - 1) / 2;
return (int)(Math.ceil(eq_root) + l);
}
// Driver code
public static void main(String[] args)
{
System.out.println(minDaysToEmpty(5, 2));
System.out.println(minDaysToEmpty(6514683, 4965));
}
}
// This code is contributed by Smitha Dinesh Semwal.
蟒蛇3
# Python3 code to find number of days
# after which tank will become empty
import math
# Method returns minimum number of days
# after which tank will become empty
def minDaysToEmpty(C, l):
if (l >= C): return C
eq_root = (math.sqrt(1 + 8 * (C - l)) - 1) / 2
return math.ceil(eq_root) + l
# Driver code
print(minDaysToEmpty(5, 2))
print(minDaysToEmpty(6514683, 4965))
# This code is contributed by Smitha Dinesh Semwal.
C#
// C# code to find number
// of days after which
// tank will become empty
using System;
class GFG
{
// Method returns minimum
// number of days after
// which tank will become empty
static int minDaysToEmpty(int C,
int l)
{
if (l >= C) return C;
double eq_root = (Math.Sqrt(1 + 8 *
(C - l)) - 1) / 2;
return (int)(Math.Ceiling(eq_root) + l);
}
// Driver code
static public void Main ()
{
Console.WriteLine(minDaysToEmpty(5, 2));
Console.WriteLine(minDaysToEmpty(6514683,
4965));
}
}
// This code is contributed by ajit
PHP
= $C)
return $C;
$eq_root = (int)sqrt(1 + 8 *
($C - $l) - 1) / 2;
return ceil($eq_root) + $l;
}
// Driver code
echo minDaysToEmpty(5, 2), "\n";
echo minDaysToEmpty(6514683,
4965), "\n";
// This code is contributed
// by akt_mit
?>
Javascript
输出 :
4
8573
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。