连续有无数人从1开始索引。索引为i的人的力量为i 2 。您拥有力量P ,而任务是告诉您可以利用力量P杀死的最大人数是多少。
如果P≥X ,您只能杀死具有X强度的人,并且杀死他之后,您的强度会降低X。
例子:
Input: P = 14
Output: 3
First person will have strength 12 = 1 which is < P
P gets reduced to 13 after the first kill.
Second kill, P = 13 – 22 = 9
Third kill, P = 9 – 32 = 0
Input: P = 58
Output: 5
幼稚的方法:检查从1开始的每一次杀戮,直到力量P大于或等于被杀者的力量。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum
// number of people that can be killed
int maxPeople(int p)
{
int tmp = 0, count = 0;
// Loop will break when the ith person
// cannot be killed
for (int i = 1; i * i <= p; i++) {
tmp = tmp + (i * i);
if (tmp <= p)
count++;
else
break;
}
return count;
}
// Driver code
int main()
{
int p = 14;
cout << maxPeople(p);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to return the maximum
// number of people that can be killed
static int maxPeople(int p)
{
int tmp = 0, count = 0;
// Loop will break when the ith person
// cannot be killed
for (int i = 1; i * i <= p; i++)
{
tmp = tmp + (i * i);
if (tmp <= p)
count++;
else
break;
}
return count;
}
// Driver code
public static void main(String args[])
{
int p = 14;
System.out.println(maxPeople(p));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
from math import sqrt
# Function to return the maximum
# number of people that can be killed
def maxPeople(p) :
tmp = 0; count = 0;
# Loop will break when the ith person
# cannot be killed
for i in range(1, int(sqrt(p)) + 1) :
tmp = tmp + (i * i);
if (tmp <= p) :
count += 1;
else :
break;
return count;
# Driver code
if __name__ == "__main__" :
p = 14;
print(maxPeople(p));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum
// number of people that can be killed
static int maxPeople(int p)
{
int tmp = 0, count = 0;
// Loop will break when the ith person
// cannot be killed
for (int i = 1; i * i <= p; i++)
{
tmp = tmp + (i * i);
if (tmp <= p)
count++;
else
break;
}
return count;
}
// Driver code
public static void Main()
{
int p = 14;
Console.WriteLine(maxPeople(p));
}
}
// This code is contributed by anuj_67..
Javascript
C++
// C++ implementation of the approach
#include
#include
using namespace std;
#define ll long long
static constexpr int kN = 1000000;
// Function to return the maximum
// number of people that can be killed
int maxPeople(int p)
{
// Storing the sum beforehand so that
// it can be used in each query
ll sums[kN];
sums[0] = 0;
for (int i = 1; i < kN; i++)
sums[i] = (ll)(i * i) + sums[i - 1];
// lower_bound returns an iterator pointing to the
// first element greater than or equal to your val
auto it = std::lower_bound(sums, sums + kN, p);
if (*it > p) {
// Previous value
--it;
}
// Returns the index in array upto which
// killing is possible with strength P
return (it - sums);
}
// Driver code
int main()
{
int p = 14;
cout << maxPeople(p);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int kN = 1000000;
// Function to return the maximum
// number of people that can be killed
static int maxPeople(int p)
{
// Storing the sum beforehand so that
// it can be used in each query
long []sums = new long[kN];
sums[0] = 0;
for (int i = 1; i < kN; i++)
sums[i] = (long)(i * i) + sums[i - 1];
// lower_bound returns an iterator pointing to the
// first element greater than or equal to your val
int it = lower_bound(sums, 0, kN, p);
if (it > p)
{
// Previous value
--it;
}
// Returns the index in array upto which
// killing is possible with strength P
return it;
}
private static int lower_bound(long[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low)/2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Driver code
public static void main(String[] args)
{
int p = 14;
System.out.println(maxPeople(p));
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
kN = 1000000;
# Function to return the maximum
# number of people that can be killed
def maxPeople(p):
# Storing the sum beforehand so that
# it can be used in each query
sums = [0] * kN;
sums[0] = 0;
for i in range(1, kN):
sums[i] = (i * i) + sums[i - 1];
# lower_bound returns an iterator
# pointing to the first element
# greater than or equal to your val
it = lower_bound(sums, 0, kN, p);
if (it > p):
# Previous value
it -= 1;
# Returns the index in array upto which
# killing is possible with strength P
return it;
def lower_bound(a, low, high, element):
while(low < high):
middle = int(low + (high - low) / 2);
if(element > a[middle]):
low = middle + 1;
else:
high = middle;
return low;
# Driver code
if __name__ == '__main__':
p = 14;
print(maxPeople(p));
# This code contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
public class GFG
{
static int kN = 1000000;
// Function to return the maximum
// number of people that can be killed
static int maxPeople(int p)
{
// Storing the sum beforehand so that
// it can be used in each query
long []sums = new long[kN];
sums[0] = 0;
for (int i = 1; i < kN; i++)
sums[i] = (long)(i * i) + sums[i - 1];
// lower_bound returns an iterator pointing to the
// first element greater than or equal to your val
int it = lower_bound(sums, 0, kN, p);
if (it > p)
{
// Previous value
--it;
}
// Returns the index in array upto which
// killing is possible with strength P
return it;
}
private static int lower_bound(long[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low)/2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Driver code
public static void Main(String[] args)
{
int p = 14;
Console.WriteLine(maxPeople(p));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# helper function which returns the sum
# of series (1^2 + 2^2 +...+ n^2)
def squareSeries(n):
return (n*(n+1)*(2*n+1))//6
# maxPeople function which returns
# appropriate value using Binary Search
# in O(logn)
def maxPeople(n):
# Set the lower and higher values
low = 0
high = 1000000000000000
while low<=high:
# calculate the mid using
# low and high
mid = low + ((high-low)//2)
value = squareSeries(mid)
#compare value with n
if value<=n:
ans = mid
low = mid+1
else:
high = mid-1
# return the ans
return ans
if __name__=='__main__':
p=14
print(maxPeople(p))
# This code is contributed bu chaudhary_19
# (* Mayank Chaudhary)
3
时间复杂度: O(N)
有效的方法:我们可以看到,如果我们杀了我个人那么我们已经杀死(I – 1)人。这意味着它是一个单调函数f,其域是整数集。现在,我们可以在此单调函数上应用二进制搜索,在该函数中,我们正在寻找一些x ,以使f(x)等于目标值,而不是查找数组。时间复杂度降低为O(Log(n))。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
using namespace std;
#define ll long long
static constexpr int kN = 1000000;
// Function to return the maximum
// number of people that can be killed
int maxPeople(int p)
{
// Storing the sum beforehand so that
// it can be used in each query
ll sums[kN];
sums[0] = 0;
for (int i = 1; i < kN; i++)
sums[i] = (ll)(i * i) + sums[i - 1];
// lower_bound returns an iterator pointing to the
// first element greater than or equal to your val
auto it = std::lower_bound(sums, sums + kN, p);
if (*it > p) {
// Previous value
--it;
}
// Returns the index in array upto which
// killing is possible with strength P
return (it - sums);
}
// Driver code
int main()
{
int p = 14;
cout << maxPeople(p);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int kN = 1000000;
// Function to return the maximum
// number of people that can be killed
static int maxPeople(int p)
{
// Storing the sum beforehand so that
// it can be used in each query
long []sums = new long[kN];
sums[0] = 0;
for (int i = 1; i < kN; i++)
sums[i] = (long)(i * i) + sums[i - 1];
// lower_bound returns an iterator pointing to the
// first element greater than or equal to your val
int it = lower_bound(sums, 0, kN, p);
if (it > p)
{
// Previous value
--it;
}
// Returns the index in array upto which
// killing is possible with strength P
return it;
}
private static int lower_bound(long[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low)/2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Driver code
public static void main(String[] args)
{
int p = 14;
System.out.println(maxPeople(p));
}
}
/* This code is contributed by PrinciRaj1992 */
Python3
# Python3 implementation of the approach
kN = 1000000;
# Function to return the maximum
# number of people that can be killed
def maxPeople(p):
# Storing the sum beforehand so that
# it can be used in each query
sums = [0] * kN;
sums[0] = 0;
for i in range(1, kN):
sums[i] = (i * i) + sums[i - 1];
# lower_bound returns an iterator
# pointing to the first element
# greater than or equal to your val
it = lower_bound(sums, 0, kN, p);
if (it > p):
# Previous value
it -= 1;
# Returns the index in array upto which
# killing is possible with strength P
return it;
def lower_bound(a, low, high, element):
while(low < high):
middle = int(low + (high - low) / 2);
if(element > a[middle]):
low = middle + 1;
else:
high = middle;
return low;
# Driver code
if __name__ == '__main__':
p = 14;
print(maxPeople(p));
# This code contributed by Rajput-Ji
C#
// C# implementation of the approach
using System;
public class GFG
{
static int kN = 1000000;
// Function to return the maximum
// number of people that can be killed
static int maxPeople(int p)
{
// Storing the sum beforehand so that
// it can be used in each query
long []sums = new long[kN];
sums[0] = 0;
for (int i = 1; i < kN; i++)
sums[i] = (long)(i * i) + sums[i - 1];
// lower_bound returns an iterator pointing to the
// first element greater than or equal to your val
int it = lower_bound(sums, 0, kN, p);
if (it > p)
{
// Previous value
--it;
}
// Returns the index in array upto which
// killing is possible with strength P
return it;
}
private static int lower_bound(long[] a, int low,
int high, int element)
{
while(low < high)
{
int middle = low + (high - low)/2;
if(element > a[middle])
low = middle + 1;
else
high = middle;
}
return low;
}
// Driver code
public static void Main(String[] args)
{
int p = 14;
Console.WriteLine(maxPeople(p));
}
}
// This code has been contributed by 29AjayKumar
3
时间复杂度: O(Log(n))
更有效的方法:
我们可以在时间复杂度O(logn)和空间复杂度O(1)中做同样的问题。通过将low的值设置为0并将high的值设置为10 ^ 15来开始二进制搜索。我们将计算中值,并根据中值更改低值和高值的位置。
下面是上述方法的实现。
Python3
# Python3 implementation of the approach
# helper function which returns the sum
# of series (1^2 + 2^2 +...+ n^2)
def squareSeries(n):
return (n*(n+1)*(2*n+1))//6
# maxPeople function which returns
# appropriate value using Binary Search
# in O(logn)
def maxPeople(n):
# Set the lower and higher values
low = 0
high = 1000000000000000
while low<=high:
# calculate the mid using
# low and high
mid = low + ((high-low)//2)
value = squareSeries(mid)
#compare value with n
if value<=n:
ans = mid
low = mid+1
else:
high = mid-1
# return the ans
return ans
if __name__=='__main__':
p=14
print(maxPeople(p))
# This code is contributed bu chaudhary_19
# (* Mayank Chaudhary)
3
时间复杂度: O(Log(n))
空间复杂度: O(1)