📌  相关文章
📜  计算两人比赛中可能出现的最大平局次数

📅  最后修改于: 2022-05-13 01:56:08.554000             🧑  作者: Mango

计算两人比赛中可能出现的最大平局次数

给定一个大小为N的数组对v[][] ,它显示N个实例的匹配分数,其中(v[i][0], v[i][1])显示的分数等于 ( p, q ) 的 2 名玩家。进球后得分将变为 ( p+1: q ) 或 ( p: q+1 )。任务是找出整场比赛中平局的最大次数。

:最后得分代表比赛的最终结果。随着时间的增加,分数以递增的顺序提供。

例子:

方法:假设分数(p, q)(r, s) 。尽可能放在对(x, x)之间。这个x必须是这样的:
p ≤ x ≤ rq ≤ x ≤ s ,因此max(p, q) ≤ x ≤ min(r, s)。因此,只需计算此类x的数量。请按照以下步骤解决问题:

  • 初始化对p1[]p2[]。
  • 将变量draws初始化为1 ,将diff初始化为0。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • p2设置为v[i]。
    • 如果p1.firstp1.second相同,则将draws的值减少1。
    • diff设置为min(p2.first, p2.second) – max(p1.first, p1.second) + 1。
    • 如果diff大于0 ,则将draws的值增加diff。
    • p1的值设置为p2。
  • 执行上述步骤后,打印draws的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Utility Function
void solve(int& N,
           vector > v)
{
 
    // Initializing pair for input and
    // one for starting position {0, 0}
    pair p1, p2;
 
    int draws = 1, diff = 0;
    p1 = make_pair(0, 0);
 
    for (int i = 0; i < N; i++) {
        p2 = v[i];
 
        // Reduce if found a draw situation
        if (p1.first == p1.second) {
            draws--;
        }
 
        // Calculation for possible
        // values for x would be
        // min(r, s) - max(p, q) + 1
        diff = min(p2.first, p2.second)
               - max(p1.first, p1.second)
               + 1;
 
        // If possible value found,
        // add to our answer
        if (diff > 0) {
            draws += diff;
        }
 
        // In end, update previous pair
        // to new pair
        p1 = p2;
    }
    cout << draws;
}
 
// Driver Code
int main()
{
    int N = 1;
    vector > v = { { 5, 4 } };
 
    solve(N, v);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
// User defined Pair class
class Pair {
  int x;
  int y;
 
  // Constructor
  public Pair(int x, int y)
  {
    this.x = x;
    this.y = y;
  }
}
 
class GFG {
 
  // Utility Function
  static void solve(int N, Pair v[])
  {
 
    // Initializing pair for input and
    // one for starting position {0, 0}
    Pair p1 = new Pair(0,0);
 
    int draws = 1, diff = 0;
    p1.x = 0;
    p1.y = 0;
 
    for (int i = 0; i < N; i++) {
      Pair p2 = new Pair(v[i].x, v[i].y);
 
      // Reduce if found a draw situation
      if (p1.x == p1.y) {
        draws--;
      }
 
      // Calculation for possible
      // values for x would be
      // min(r, s) - max(p, q) + 1
      diff = Math.min(p2.x, p2.y)
        - Math.max(p1.x, p1.y)
        + 1;
 
      // If possible value found,
      // add to our answer
      if (diff > 0) {
        draws += diff;
      }
 
      // In end, update previous pair
      // to new pair
      p1 = p2;
    }
    System.out.print(draws);
  }
 
  // Driver Code
  public static void main (String[] args) {
    int n = 1;
    Pair arr[] = new Pair[n];
    arr[0] = new Pair(5, 4);
 
    solve(n, arr);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Utility Function
def solve(N, v):
 
    # Initializing pair for input and
    # one for starting position {0, 0}
    draws = 1
    diff = 0;
    p1 = { "first": 0, "second": 0 };
 
    for i in range(N):
        p2 = v[i];
 
        # Reduce if found a draw situation
        if (p1["first"] == p1["second"]):
            draws -= 1
 
        # Calculation for possible
        # values for x would be
        # min(r, s) - max(p, q) + 1
        diff = min(p2["first"], p2["second"]) - max(p1["first"], p1["second"]) + 1;
 
        # If possible value found,
        # add to our answer
        if (diff > 0):
            draws += diff;
     
 
        # In end, update previous pair
        # to new pair
        p1 = p2;
     
    print(draws);
     
# Driver Code
N = 1;
v = [{ "first": 5, "second": 4 }];
 
solve(N, v);
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
 
// User defined Pair class
class Pair {
  public int x;
  public int y;
 
  // Constructor
  public Pair(int x, int y)
  {
    this.x = x;
    this.y = y;
  }
}
 
public class GFG {
 
  // Utility Function
  static void solve(int N, Pair []v)
  {
 
    // Initializing pair for input and
    // one for starting position {0, 0}
    Pair p1 = new Pair(0,0);
 
    int draws = 1, diff = 0;
    p1.x = 0;
    p1.y = 0;
 
    for (int i = 0; i < N; i++) {
      Pair p2 = new Pair(v[i].x, v[i].y);
 
      // Reduce if found a draw situation
      if (p1.x == p1.y) {
        draws--;
      }
 
      // Calculation for possible
      // values for x would be
      // min(r, s) - max(p, q) + 1
      diff = Math.Min(p2.x, p2.y)
        - Math.Max(p1.x, p1.y)
        + 1;
 
      // If possible value found,
      // add to our answer
      if (diff > 0) {
        draws += diff;
      }
 
      // In end, update previous pair
      // to new pair
      p1 = p2;
    }
    Console.Write(draws);
  }
 
  // Driver Code
  public static void Main(String[] args) {
    int n = 1;
    Pair []arr = new Pair[n];
    arr[0] = new Pair(5, 4);
 
    solve(n, arr);
  }
}
 
// This code is contributed by 29AjayKumar


Javascript



输出
5

时间复杂度: 在)
辅助空间: O(1)