📜  最小化要分发的泰迪总数

📅  最后修改于: 2021-04-23 07:00:06             🧑  作者: Mango

假设有N个学生排成一行,并且每个人都有他们在考试中获得的分数。任务是在满足特定条件的情况下分发泰迪熊

  1. 所有学生必须至少有1个泰迪熊
  2. 如果两个学生并排坐着,那么分数较高的学生必须得到更多的玩具。

因此,任务是最大程度地减少泰迪总数。

例子

Input: n = 3, marks = [ 1, 2, 2 ]
Output: 4
Here 1, 2, 2 is the marks. 
Note that when two students have equal marks 
they are allowed to have a different number of teddies. 
Hence optimal distribution will be 1, 2, 1.

Input: n = 6, marks = [ 1, 4, 5, 2, 2, 1 ]
Output: 10

方法:该方法是使用动态编程来解决给定的问题:

  1. 初始化大小为n的表。
  2. 运行循环n次。
  3. 如果相邻的学生得分较高,则复习并更改之前作为解决方案分配的所有表值,直到根据给定的约束发现作为解决方案分配的表值不正确。
  4. 如果右邻的学生得分较高,则在左邻的解决方案中添加一个,而右邻的则分配给解决方案。

下面是上述方法的实现:

C++
// C++ implementation of the
// above approach
#include
using namespace std;
  
long long fun(int marks[],int n)
{
    // Initializing one tablet 
    // for each student
    long long dp[n], temp;
    fill(dp, dp + n, 1);
      
    for(int i = 0; i < n - 1; i++)
    {
        // if left adjacent is having 
        // higher marks review and change 
        // all the dp values assigned before
        // until assigned dp values are found 
        // wrong according to given constrains
        if (marks[i] > marks[i + 1])
        { 
            temp = i;
            while (true)
            {
                if ((marks[temp] > marks[temp + 1]) && 
                                         temp >= 0)
                {
                    if (dp[temp] > dp[temp + 1])
                    {
                        temp -= 1;
                        continue;
                    }
                    else
                    {
                        dp[temp] = dp[temp + 1] + 1;
                        temp -= 1;
                    }
                }                     
                else
                    break; 
            } 
        }
          
        // if right adjacent is having higher
        // marks add one in dp of left adjacent
        // and assign to right one
        else if( marks[i] < marks[i + 1])
            dp[i + 1] = dp[i] + 1;
    }
  
    int sum = 0;
  
    for(int i = 0; i < n; i++)
    sum += dp[i];
  
    return sum;
}
  
// Driver Code
int main()
{
    // n number of students
    int n = 6;
      
    // marks of students
    int marks[6] = { 1, 4, 5, 2, 2, 1};
      
    // solution of problem
    cout << fun(marks, n);
      
    return 0;
}
  
// This code is contributed by ash264


Java
// Java implementation of the 
// above approach 
  
public class GFG {
  
    static long fun(int marks[],int n) 
    { 
        // Initializing one tablet  
        // for each student 
        long dp[] = new long[n] ;
        int temp; 
          
        for (int i = 0;i < n;i ++)
            dp[i] = 1 ; 
            
        for(int i = 0; i < n - 1; i++) 
        { 
            // if left adjacent is having  
            // higher marks review and change  
            // all the dp values assigned before 
            // until assigned dp values are found  
            // wrong according to given constrains 
            if (marks[i] > marks[i + 1]) 
            {  
                temp = i; 
                while (true) 
                { 
                    if ((marks[temp] > marks[temp + 1]) &&  
                                             temp >= 0) 
                    { 
                        if (dp[temp] > dp[temp + 1]) 
                        { 
                            temp -= 1; 
                            continue; 
                        } 
                        else
                        { 
                            dp[temp] = dp[temp + 1] + 1; 
                            temp -= 1; 
                        } 
                    }                     
                    else
                        break;  
                }  
            } 
                
            // if right adjacent is having higher 
            // marks add one in dp of left adjacent 
            // and assign to right one 
            else if( marks[i] < marks[i + 1]) 
                dp[i + 1] = dp[i] + 1; 
        } 
        
        int sum = 0; 
        
        for(int i = 0; i < n; i++) 
        sum += dp[i]; 
        
        return sum; 
    } 
      
    public static void main(String args[])
    {
        // n number of students 
        int n = 6; 
            
        // marks of students 
        int marks[] = { 1, 4, 5, 2, 2, 1}; 
            
        // solution of problem 
        System.out.println(fun(marks, n)); 
    }
    // This code is contributed by ANKITRAI1
}


Python 3
# Python implementation of the above approach
  
def fun(marks, n):
    # Initializing one tablet for each student
    dp = [ 1 for i in range(0, n) ]
  
    for i in range(0, n-1):
  
        # if left adjacent is having higher marks
        # review and change all the dp values assigned before
        # until assigned dp values are found wrong 
        # according to given constrains
        if marks[i] > marks[i + 1]:
            temp = i
            while True:
                if marks[temp] > marks[temp + 1] and temp >= 0:
                    if dp[temp] > dp[temp + 1]:
                        temp -= 1
                        continue
                    else:
                        dp[temp] = dp[temp + 1] + 1
                        temp -= 1                        
                else:
                    break    
  
        # if right adjacent is having higher marks
        # add one in dp of left adjacent and assign to right one
        elif marks[i] < marks[i + 1]:
            dp[i + 1] = dp[i] + 1
  
    return(sum(dp))
  
  
  
# driver code
  
# n number of students
n = 6
  
# marks of students
marks = [ 1, 4, 5, 2, 2, 1]
  
# solution of problem
print(fun(marks, n))


C#
// C# implementation of the 
// above approach 
using System; 
    
class GFG 
{ 
    public static long fun(int[] marks,int n) 
    { 
        // Initializing one tablet  
        // for each student 
        long[] dp = new long[n];
        long temp; 
          
        for(int i = 0; i < n; i++)
            dp[i] = 1;
            
        for(int i = 0; i < n - 1; i++) 
        { 
            // if left adjacent is having  
            // higher marks review and change  
            // all the dp values assigned before 
            // until assigned dp values are found  
            // wrong according to given constrains 
            if (marks[i] > marks[i + 1]) 
            {  
                temp = i; 
                while (true) 
                { 
                    if ((marks[temp] > marks[temp + 1]) &&  
                                             temp >= 0) 
                    { 
                        if (dp[temp] > dp[temp + 1]) 
                        { 
                            temp -= 1; 
                            continue; 
                        } 
                        else
                        { 
                            dp[temp] = dp[temp + 1] + 1; 
                            temp -= 1; 
                        } 
                    }                      
                    else
                        break;  
                }  
            } 
                
            // if right adjacent is having higher 
            // marks add one in dp of left adjacent 
            // and assign to right one 
            else if( marks[i] < marks[i + 1]) 
                dp[i + 1] = dp[i] + 1; 
        } 
        
        long sum = 0; 
        
        for(int i = 0; i < n; i++) 
            sum += dp[i]; 
        
        return sum; 
    } 
        
    // Driver Code 
    static void Main() 
    { 
        // n number of students 
        int n = 6; 
            
        // marks of students 
        int[] marks = new int[]{ 1, 4, 5, 2, 2, 1}; 
            
        // solution of problem 
        Console.Write( fun(marks, n) );  
    }
    //This code is contributed by DrRoot_
}


输出:
10