📜  查找每个辐射站的最终辐射

📅  最后修改于: 2021-04-22 00:08:01             🧑  作者: Mango

直线上有N个站,每个站都有一定的非负辐射功率。每个站点可以通过以下方式增加其相邻站点的辐射功率,
台站i与辐射功率R将增加(I – 1)台的辐射由R – 1,(I – 2)R站的辐射– 2,等等…和第(i + 1)台的辐射由R – 1 ,第(i + 2)电台的辐射量为R – 2 ,依此类推…直到有效辐射功率为正时为止。
任务是找到每个站点的最终辐射。
例子:

天真的方法:对于每个站,我都会如上所述增加相邻站的辐射,直到有效辐射变为负值为止。
时间复杂度: O(n * n)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the final radiations
void print(int rStation[], int n)
{
    for (int i = 1; i <= n; i++)
        cout << rStation[i] << " ";
    cout << endl;
}
  
// Function to create the array of the
// resultant radiations
void radiated_Station(int station[], int n)
{
  
    // Resultant radiations
    int rStation[n + 1];
  
    // Initialize resultant array with 0
    memset(rStation, 0, sizeof(rStation));
  
    for (int i = 1; i <= n; i++) {
  
        // Declaring index counter for left
        // and right radiation
        int li = i - 1, ri = i + 1;
  
        // Effective radiation for left
        // and right case
        int lRad = station[i] - 1, rRad = station[i] - 1;
  
        // Radiation for i-th station
        rStation[i] += station[i];
  
        // Radiation increment for left stations
        while (li >= 1 && lRad >= 1) {
            rStation[li--] += lRad--;
        }
  
        // Radiation increment for right stations
        while (ri <= n && rRad >= 1) {
            rStation[ri++] += rRad--;
        }
    }
  
    // Print the resultant radiation
    // for each of the stations
    print(rStation, n);
}
  
// Driver code
int main()
{
  
    // 1-based indexing
    int station[] = { 0, 7, 9, 12, 2, 5 };
    int n = (sizeof(station) / sizeof(station[0])) - 1;
  
    radiated_Station(station, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // Function to print the final radiations
    static void print(int rStation[], int n)
    {
        for (int i = 1; i <= n; i++)
            System.out.print(rStation[i] + " ");
        System.out.println("");
    }
  
    // Function to create the array of the
    // resultant radiations
    static void radiated_Station(int station[], int n)
    {
  
        // Resultant radiations
        int rStation[] = new int[n + 1];
  
        for (int i = 1; i <= n; i++) {
  
            // Declaring index counter for left
            // and right radiation
            int li = i - 1, ri = i + 1;
  
            // Effective radiation for left
            // and right case
            int lRad = station[i] - 1, rRad = station[i] - 1;
  
            // Radiation for i-th station
            rStation[i] += station[i];
  
            // Radiation increment for left stations
            while (li >= 1 && lRad >= 1) {
                rStation[li--] += lRad--;
            }
  
            // Radiation increment for right stations
            while (ri <= n && rRad >= 1) {
                rStation[ri++] += rRad--;
            }
        }
  
        // Print the resultant radiation
        // for each of the stations
        print(rStation, n);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        // 1-based indexing
        int station[] = { 0, 7, 9, 12, 2, 5 };
        int n = station.length - 1;
  
        radiated_Station(station, n);
    }
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
  
# Function to print the final radiations
def printf(rStation, n):
    for i in range(1, n + 1, 1):
        print(rStation[i], end = " ")
    print("\n", end = " ")
  
# Function to create the array of the
# resultant radiations
def radiated_Station(station, n):
      
    # Resultant radiations
    rStation = [0 for i in range(n + 1)]
  
    for i in range(1, n + 1):
        # Declaring index counter for left
        # and right radiation
        li = i - 1
        ri = i + 1
  
        # Effective radiation for left
        # and right case
        lRad = station[i] - 1
        rRad = station[i] - 1
  
        # Radiation for i-th station
        rStation[i] += station[i]
  
        # Radiation increment for left stations
        while (li >= 1 and lRad >= 1):
            rStation[li] += lRad
            lRad -= 1
            li -= 1
  
        # Radiation increment for right stations
        while (ri <= n and rRad >= 1):
            rStation[ri] += rRad
            rRad -= 1
            ri += 1
  
    # Print the resultant radiation
    # for each of the stations
    printf(rStation, n)
  
# Driver code
if __name__ == '__main__':
    # 1-based indexing
    station = [0, 7, 9, 12, 2, 5]
    n = len(station) - 1
  
    radiated_Station(station, n)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG {
  
    // Function to print the final radiations
    static void print(int[] rStation, int n)
    {
        for (int i = 1; i <= n; i++)
            Console.Write(rStation[i] + " ");
        Console.WriteLine("");
    }
  
    // Function to create the array of the
    // resultant radiations
    static void radiated_Station(int[] station, int n)
    {
  
        // Resultant radiations
        int[] rStation = new int[n + 1];
  
        for (int i = 1; i <= n; i++) {
  
            // Declaring index counter for left
            // and right radiation
            int li = i - 1, ri = i + 1;
  
            // Effective radiation for left
            // and right case
            int lRad = station[i] - 1, rRad = station[i] - 1;
  
            // Radiation for i-th station
            rStation[i] += station[i];
  
            // Radiation increment for left stations
            while (li >= 1 && lRad >= 1) {
                rStation[li--] += lRad--;
            }
  
            // Radiation increment for right stations
            while (ri <= n && rRad >= 1) {
                rStation[ri++] += rRad--;
            }
        }
  
        // Print the resultant radiation
        // for each of the stations
        print(rStation, n);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        // 1-based indexing
        int[] station = { 0, 7, 9, 12, 2, 5 };
        int n = station.Length - 1;
  
        radiated_Station(station, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */


PHP
= 1 && $lRad >= 1) {
            $rStation[$li--] += $lRad--;
        }
   
        // Radiation increment for right stations
        while ($ri <= $n && $rRad >= 1) {
            $rStation[$ri++] += $rRad--;
        }
    }
   
    // Print the resultant radiation
    // for each of the stations
    print_radiation($rStation, $n);
}
   
// Driver code
  
// 1-based indexing
$station = array( 0, 7, 9, 12, 2, 5 );
$n = (sizeof($station) / sizeof($station[0])) - 1;
  
radiated_Station($station, $n);
  
//code contributed by Shashank_Sharma
?>


C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the final radiations
void print(int rStation[], int n)
{
    for (int i = 1; i <= n; i++)
        cout << rStation[i] << " ";
    cout << endl;
}
  
// Function to create the array of the
// resultant radiations
void radiated_Station(int station[], int n)
{
  
    // Resultant radiations
    int rStation[n + 1];
  
    int left_Rad[n + 2], right_Rad[n + 2];
  
    // Frequency of stations that affect each station
    int lCount[n + 2], rCount[n + 2];
  
    // Initialization of the arrays with 0
    memset(left_Rad, 0, sizeof(left_Rad));
    memset(right_Rad, 0, sizeof(right_Rad));
    memset(lCount, 0, sizeof(lCount));
    memset(rCount, 0, sizeof(rCount));
  
    for (int i = 1; i < n + 1; i++) {
  
        // Radiation of station i
        int Rad = station[i];
  
        // Left and right most position of radiation
        // for station i, index should be
        // in between the station range
        int li = max(1, i - Rad + 1), ri = min(n, Rad - 1 + i);
  
        // At station 1 radiation effect
        // for station i
        int at1 = max(0, Rad - i + 1);
        left_Rad[1] += at1;
  
        // While iterating from left avoid
        // effective radiation at right
        left_Rad[i + 1] -= Rad;
  
        // At station n radiation effect
        // for station i
        int atn = max(0, Rad - n + i);
        right_Rad[n] += atn;
  
        // While iterating from right avoid
        // effective radiation at left
        right_Rad[i - 1] -= Rad;
  
        // Left and right most position
        // where station i effects
        lCount[li]++;
        rCount[ri]++;
  
        // Avoiding right radiation for
        // left iteration and vice-versa
        lCount[i + 1]--;
        rCount[i - 1]--;
    }
  
    // Left iteration
    for (int i = 1; i <= n; i++) {
        lCount[i] += lCount[i - 1];
  
        // Total radiations at index 1 already counted
        if (i > 1)
            left_Rad[i] += left_Rad[i - 1] + lCount[i];
    }
  
    // Right iteration
    for (int i = n; i >= 1; i--) {
        rCount[i] += rCount[i + 1];
  
        // Total radiations at index n already counted
        if (i < n)
            right_Rad[i] += right_Rad[i + 1] + rCount[i];
    }
  
    // Final iteration that creates
    // the resultant radiation
    for (int i = 1; i <= n; i++) {
  
        // Added extra value in each index
        rStation[i] = left_Rad[i] + right_Rad[i] - station[i];
    }
  
    // Print the resultant radiation
    // for each of the stations
    print(rStation, n);
}
  
// Driver code
int main()
{
  
    // 1-based indexing
    int station[] = { 0, 7, 9, 12, 2, 5 };
    int n = (sizeof(station) / sizeof(station[0])) - 1;
  
    radiated_Station(station, n);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // Function to print the final radiations
    static void print(int rStation[], int n)
    {
        for (int i = 1; i <= n; i++)
            System.out.print(rStation[i] + " ");
        System.out.println("");
    }
  
    // Function to create the array of the
    // resultant radiations
    static void radiated_Station(int station[], int n)
    {
  
        // Resultant radiations
        int[] rStation = new int[n + 1];
  
        int[] left_Rad = new int[n + 2];
        int[] right_Rad = new int[n + 2];
  
        // Frequency of stations that affect each station
        int[] lCount = new int[n + 2];
        int[] rCount = new int[n + 2];
  
        for (int i = 1; i < n + 1; i++) {
  
            // Radiation of station i
            int Rad = station[i];
  
            // Left and right most position of radiation
            // for station i, index should be
            // in between the station range
            int li = Math.max(1, i - Rad + 1), ri = Math.min(n, Rad - 1 + i);
  
            // At station 1 radiation effect
            // for station i
            int at1 = Math.max(0, Rad - i + 1);
            left_Rad[1] += at1;
  
            // While iterating from left avoid
            // effective radiation at right
            left_Rad[i + 1] -= Rad;
  
            // At station n radiation effect
            // for station i
            int atn = Math.max(0, Rad - n + i);
            right_Rad[n] += atn;
  
            // While iterating from right avoid
            // effective radiation at left
            right_Rad[i - 1] -= Rad;
  
            // Left and right most position
            // where station i effects
            lCount[li]++;
            rCount[ri]++;
  
            // Avoiding right radiation for
            // left iteration and vice-versa
            lCount[i + 1]--;
            rCount[i - 1]--;
        }
   
        // Left iteration
        for (int i = 1; i <= n; i++) {
            lCount[i] += lCount[i - 1];
  
            // Total radiations at index 1 already counted
            if (i > 1)
                left_Rad[i] += left_Rad[i - 1] + lCount[i];
        }
  
        // Right iteration
        for (int i = n; i >= 1; i--) {
            rCount[i] += rCount[i + 1];
  
            // Total radiations at index n already counted
            if (i < n)
                right_Rad[i] += right_Rad[i + 1] + rCount[i];
        }
  
        // Final iteration that creates
        // the resultant radiation
        for (int i = 1; i <= n; i++) {
  
            // Added extra value in each index
            rStation[i] = left_Rad[i] + right_Rad[i] - station[i];
        }
  
        // Print the resultant radiation
        // for each of the stations
        print(rStation, n);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        // 1-based indexing
        int station[] = { 0, 7, 9, 12, 2, 5 };
        int n = station.length - 1;
  
        radiated_Station(station, n);
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
  
# Function to print the final radiations
def print_array(rStation, n):
      
    for i in range(1, n + 1):
        print(rStation[i], end = " ")
  
# Function to create the array of the
# resultant radiations
def radiated_Station(station, n):
  
    # Resultant radiations
    rStation = [0] * (n + 1)
  
    left_Rad = [0] * (n + 2)
    right_Rad = [0] * (n + 2)
  
    # Frequency of stations that 
    # affect each station
    lCount = [0] * (n + 2)
    rCount = [0] * (n + 2)
  
    for i in range(1, n + 1):
  
        # Radiation of station i
        Rad = station[i]
  
        # Left and right most position of 
        # radiation for station i, index 
        # should be in between the station range
        li = max(1, i - Rad + 1)
        ri = min(n, Rad - 1 + i);
  
        # At station 1 radiation effect
        # for station i
        at1 = max(0, Rad - i + 1)
        left_Rad[1] += at1;
  
        # While iterating from left avoid
        # effective radiation at right
        left_Rad[i + 1] -= Rad;
  
        # At station n radiation effect
        # for station i
        atn = max(0, Rad - n + i);
        right_Rad[n] += atn
  
        # While iterating from right avoid
        # effective radiation at left
        right_Rad[i - 1] -= Rad
  
        # Left and right most position
        # where station i effects
        lCount[li] += 1
        rCount[ri] += 1
  
        # Avoiding right radiation for
        # left iteration and vice-versa
        lCount[i + 1] -= 1
        rCount[i - 1] -= 1
      
    # Left iteration
    for i in range(1, n + 1):
        lCount[i] += lCount[i - 1]
  
        # Total radiations at index 1
        # already counted
        if (i > 1):
            left_Rad[i] += (left_Rad[i - 1] + 
                              lCount[i])
  
    # Right iteration
    for i in range(n, 0, -1):
        rCount[i] += rCount[i + 1]
  
        # Total radiations at index n already counted
        if (i < n):
            right_Rad[i] += (right_Rad[i + 1] + 
                                rCount[i])
  
    # Final iteration that creates
    # the resultant radiation
    for i in range(1, n + 1):
  
        # Added extra value in each index
        rStation[i] = (left_Rad[i] + 
                      right_Rad[i] - 
                        station[i])
      
    # Print the resultant radiation
    # for each of the stations
    print_array(rStation, n)
  
# Driver code
if __name__ == "__main__":
  
    # 1-based indexing
    station = [ 0, 7, 9, 12, 2, 5 ]
    n = len(station) - 1
  
    radiated_Station(station, n)
  
# This code is contributed by chitranayal


C#
// C# implementation of the approach
using System;
  
class GFG {
  
    // Function to print the final radiations
    static void print(int[] rStation, int n)
    {
        for (int i = 1; i <= n; i++)
            Console.Write(rStation[i] + " ");
        Console.WriteLine("");
    }
  
    // Function to create the array of the
    // resultant radiations
    static void radiated_Station(int[] station, int n)
    {
  
        // Resultant radiations
        int[] rStation = new int[n + 1];
  
        int[] left_Rad = new int[n + 2];
        int[] right_Rad = new int[n + 2];
  
        // Frequency of stations that affect each station
        int[] lCount = new int[n + 2];
        int[] rCount = new int[n + 2];
  
        for (int i = 1; i < n + 1; i++) {
  
            // Radiation of station i
            int Rad = station[i];
  
            // Left and right most position of radiation
            // for station i, index should be
            // in between the station range
            int li = Math.Max(1, i - Rad + 1),
                ri = Math.Min(n, Rad - 1 + i);
  
            // At station 1 radiation effect
            // for station i
            int at1 = Math.Max(0, Rad - i + 1);
            left_Rad[1] += at1;
  
            // While iterating from left avoid
            // effective radiation at right
            left_Rad[i + 1] -= Rad;
  
            // At station n radiation effect
            // for station i
            int atn = Math.Max(0, Rad - n + i);
            right_Rad[n] += atn;
  
            // While iterating from right avoid
            // effective radiation at left
            right_Rad[i - 1] -= Rad;
  
            // Left and right most position
            // where station i effects
            lCount[li]++;
            rCount[ri]++;
  
            // Avoiding right radiation for
            // left iteration and vice-versa
            lCount[i + 1]--;
            rCount[i - 1]--;
        }
  
        // Left iteration
        for (int i = 1; i <= n; i++) {
            lCount[i] += lCount[i - 1];
  
            // Total radiations at index 1 already counted
            if (i > 1)
                left_Rad[i] += left_Rad[i - 1] + lCount[i];
        }
  
        // Right iteration
        for (int i = n; i >= 1; i--) {
            rCount[i] += rCount[i + 1];
  
            // Total radiations at index n already counted
            if (i < n)
                right_Rad[i] += right_Rad[i + 1] + rCount[i];
        }
  
        // Final iteration that creates
        // the resultant radiation
        for (int i = 1; i <= n; i++) {
  
            // Added extra value in each index
            rStation[i] = left_Rad[i] + right_Rad[i] - station[i];
        }
  
        // Print the resultant radiation
        // for each of the stations
        print(rStation, n);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        // 1-based indexing
        int[] station = { 0, 7, 9, 12, 2, 5 };
        int n = station.Length - 1;
  
        radiated_Station(station, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */


输出:
26 28 29 28 25

高效方法:

  • 对于每个测站,我们将分别计算其极端的左侧和右侧辐射效果。
  • 然后从左到右从左到右的两次迭代将构建两个数组。
  • 左迭代将构建阵列left_Rad [],其由每个站的左侧有效辐射和right_Rad[]将由权迭代构成。
  • 对于站我,让我们假设它将受到m个站的左辐射和n个站的右辐射的影响。我们需要另一个数组lCount []用于m个值, rcount []用于n个值。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
  
// Function to print the final radiations
void print(int rStation[], int n)
{
    for (int i = 1; i <= n; i++)
        cout << rStation[i] << " ";
    cout << endl;
}
  
// Function to create the array of the
// resultant radiations
void radiated_Station(int station[], int n)
{
  
    // Resultant radiations
    int rStation[n + 1];
  
    int left_Rad[n + 2], right_Rad[n + 2];
  
    // Frequency of stations that affect each station
    int lCount[n + 2], rCount[n + 2];
  
    // Initialization of the arrays with 0
    memset(left_Rad, 0, sizeof(left_Rad));
    memset(right_Rad, 0, sizeof(right_Rad));
    memset(lCount, 0, sizeof(lCount));
    memset(rCount, 0, sizeof(rCount));
  
    for (int i = 1; i < n + 1; i++) {
  
        // Radiation of station i
        int Rad = station[i];
  
        // Left and right most position of radiation
        // for station i, index should be
        // in between the station range
        int li = max(1, i - Rad + 1), ri = min(n, Rad - 1 + i);
  
        // At station 1 radiation effect
        // for station i
        int at1 = max(0, Rad - i + 1);
        left_Rad[1] += at1;
  
        // While iterating from left avoid
        // effective radiation at right
        left_Rad[i + 1] -= Rad;
  
        // At station n radiation effect
        // for station i
        int atn = max(0, Rad - n + i);
        right_Rad[n] += atn;
  
        // While iterating from right avoid
        // effective radiation at left
        right_Rad[i - 1] -= Rad;
  
        // Left and right most position
        // where station i effects
        lCount[li]++;
        rCount[ri]++;
  
        // Avoiding right radiation for
        // left iteration and vice-versa
        lCount[i + 1]--;
        rCount[i - 1]--;
    }
  
    // Left iteration
    for (int i = 1; i <= n; i++) {
        lCount[i] += lCount[i - 1];
  
        // Total radiations at index 1 already counted
        if (i > 1)
            left_Rad[i] += left_Rad[i - 1] + lCount[i];
    }
  
    // Right iteration
    for (int i = n; i >= 1; i--) {
        rCount[i] += rCount[i + 1];
  
        // Total radiations at index n already counted
        if (i < n)
            right_Rad[i] += right_Rad[i + 1] + rCount[i];
    }
  
    // Final iteration that creates
    // the resultant radiation
    for (int i = 1; i <= n; i++) {
  
        // Added extra value in each index
        rStation[i] = left_Rad[i] + right_Rad[i] - station[i];
    }
  
    // Print the resultant radiation
    // for each of the stations
    print(rStation, n);
}
  
// Driver code
int main()
{
  
    // 1-based indexing
    int station[] = { 0, 7, 9, 12, 2, 5 };
    int n = (sizeof(station) / sizeof(station[0])) - 1;
  
    radiated_Station(station, n);
  
    return 0;
}

Java

// Java implementation of the approach
class GFG {
  
    // Function to print the final radiations
    static void print(int rStation[], int n)
    {
        for (int i = 1; i <= n; i++)
            System.out.print(rStation[i] + " ");
        System.out.println("");
    }
  
    // Function to create the array of the
    // resultant radiations
    static void radiated_Station(int station[], int n)
    {
  
        // Resultant radiations
        int[] rStation = new int[n + 1];
  
        int[] left_Rad = new int[n + 2];
        int[] right_Rad = new int[n + 2];
  
        // Frequency of stations that affect each station
        int[] lCount = new int[n + 2];
        int[] rCount = new int[n + 2];
  
        for (int i = 1; i < n + 1; i++) {
  
            // Radiation of station i
            int Rad = station[i];
  
            // Left and right most position of radiation
            // for station i, index should be
            // in between the station range
            int li = Math.max(1, i - Rad + 1), ri = Math.min(n, Rad - 1 + i);
  
            // At station 1 radiation effect
            // for station i
            int at1 = Math.max(0, Rad - i + 1);
            left_Rad[1] += at1;
  
            // While iterating from left avoid
            // effective radiation at right
            left_Rad[i + 1] -= Rad;
  
            // At station n radiation effect
            // for station i
            int atn = Math.max(0, Rad - n + i);
            right_Rad[n] += atn;
  
            // While iterating from right avoid
            // effective radiation at left
            right_Rad[i - 1] -= Rad;
  
            // Left and right most position
            // where station i effects
            lCount[li]++;
            rCount[ri]++;
  
            // Avoiding right radiation for
            // left iteration and vice-versa
            lCount[i + 1]--;
            rCount[i - 1]--;
        }
   
        // Left iteration
        for (int i = 1; i <= n; i++) {
            lCount[i] += lCount[i - 1];
  
            // Total radiations at index 1 already counted
            if (i > 1)
                left_Rad[i] += left_Rad[i - 1] + lCount[i];
        }
  
        // Right iteration
        for (int i = n; i >= 1; i--) {
            rCount[i] += rCount[i + 1];
  
            // Total radiations at index n already counted
            if (i < n)
                right_Rad[i] += right_Rad[i + 1] + rCount[i];
        }
  
        // Final iteration that creates
        // the resultant radiation
        for (int i = 1; i <= n; i++) {
  
            // Added extra value in each index
            rStation[i] = left_Rad[i] + right_Rad[i] - station[i];
        }
  
        // Print the resultant radiation
        // for each of the stations
        print(rStation, n);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        // 1-based indexing
        int station[] = { 0, 7, 9, 12, 2, 5 };
        int n = station.length - 1;
  
        radiated_Station(station, n);
    }
}
  
// This code contributed by Rajput-Ji

Python3

# Python3 implementation of the approach
  
# Function to print the final radiations
def print_array(rStation, n):
      
    for i in range(1, n + 1):
        print(rStation[i], end = " ")
  
# Function to create the array of the
# resultant radiations
def radiated_Station(station, n):
  
    # Resultant radiations
    rStation = [0] * (n + 1)
  
    left_Rad = [0] * (n + 2)
    right_Rad = [0] * (n + 2)
  
    # Frequency of stations that 
    # affect each station
    lCount = [0] * (n + 2)
    rCount = [0] * (n + 2)
  
    for i in range(1, n + 1):
  
        # Radiation of station i
        Rad = station[i]
  
        # Left and right most position of 
        # radiation for station i, index 
        # should be in between the station range
        li = max(1, i - Rad + 1)
        ri = min(n, Rad - 1 + i);
  
        # At station 1 radiation effect
        # for station i
        at1 = max(0, Rad - i + 1)
        left_Rad[1] += at1;
  
        # While iterating from left avoid
        # effective radiation at right
        left_Rad[i + 1] -= Rad;
  
        # At station n radiation effect
        # for station i
        atn = max(0, Rad - n + i);
        right_Rad[n] += atn
  
        # While iterating from right avoid
        # effective radiation at left
        right_Rad[i - 1] -= Rad
  
        # Left and right most position
        # where station i effects
        lCount[li] += 1
        rCount[ri] += 1
  
        # Avoiding right radiation for
        # left iteration and vice-versa
        lCount[i + 1] -= 1
        rCount[i - 1] -= 1
      
    # Left iteration
    for i in range(1, n + 1):
        lCount[i] += lCount[i - 1]
  
        # Total radiations at index 1
        # already counted
        if (i > 1):
            left_Rad[i] += (left_Rad[i - 1] + 
                              lCount[i])
  
    # Right iteration
    for i in range(n, 0, -1):
        rCount[i] += rCount[i + 1]
  
        # Total radiations at index n already counted
        if (i < n):
            right_Rad[i] += (right_Rad[i + 1] + 
                                rCount[i])
  
    # Final iteration that creates
    # the resultant radiation
    for i in range(1, n + 1):
  
        # Added extra value in each index
        rStation[i] = (left_Rad[i] + 
                      right_Rad[i] - 
                        station[i])
      
    # Print the resultant radiation
    # for each of the stations
    print_array(rStation, n)
  
# Driver code
if __name__ == "__main__":
  
    # 1-based indexing
    station = [ 0, 7, 9, 12, 2, 5 ]
    n = len(station) - 1
  
    radiated_Station(station, n)
  
# This code is contributed by chitranayal

C#

// C# implementation of the approach
using System;
  
class GFG {
  
    // Function to print the final radiations
    static void print(int[] rStation, int n)
    {
        for (int i = 1; i <= n; i++)
            Console.Write(rStation[i] + " ");
        Console.WriteLine("");
    }
  
    // Function to create the array of the
    // resultant radiations
    static void radiated_Station(int[] station, int n)
    {
  
        // Resultant radiations
        int[] rStation = new int[n + 1];
  
        int[] left_Rad = new int[n + 2];
        int[] right_Rad = new int[n + 2];
  
        // Frequency of stations that affect each station
        int[] lCount = new int[n + 2];
        int[] rCount = new int[n + 2];
  
        for (int i = 1; i < n + 1; i++) {
  
            // Radiation of station i
            int Rad = station[i];
  
            // Left and right most position of radiation
            // for station i, index should be
            // in between the station range
            int li = Math.Max(1, i - Rad + 1),
                ri = Math.Min(n, Rad - 1 + i);
  
            // At station 1 radiation effect
            // for station i
            int at1 = Math.Max(0, Rad - i + 1);
            left_Rad[1] += at1;
  
            // While iterating from left avoid
            // effective radiation at right
            left_Rad[i + 1] -= Rad;
  
            // At station n radiation effect
            // for station i
            int atn = Math.Max(0, Rad - n + i);
            right_Rad[n] += atn;
  
            // While iterating from right avoid
            // effective radiation at left
            right_Rad[i - 1] -= Rad;
  
            // Left and right most position
            // where station i effects
            lCount[li]++;
            rCount[ri]++;
  
            // Avoiding right radiation for
            // left iteration and vice-versa
            lCount[i + 1]--;
            rCount[i - 1]--;
        }
  
        // Left iteration
        for (int i = 1; i <= n; i++) {
            lCount[i] += lCount[i - 1];
  
            // Total radiations at index 1 already counted
            if (i > 1)
                left_Rad[i] += left_Rad[i - 1] + lCount[i];
        }
  
        // Right iteration
        for (int i = n; i >= 1; i--) {
            rCount[i] += rCount[i + 1];
  
            // Total radiations at index n already counted
            if (i < n)
                right_Rad[i] += right_Rad[i + 1] + rCount[i];
        }
  
        // Final iteration that creates
        // the resultant radiation
        for (int i = 1; i <= n; i++) {
  
            // Added extra value in each index
            rStation[i] = left_Rad[i] + right_Rad[i] - station[i];
        }
  
        // Print the resultant radiation
        // for each of the stations
        print(rStation, n);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        // 1-based indexing
        int[] station = { 0, 7, 9, 12, 2, 5 };
        int n = station.Length - 1;
  
        radiated_Station(station, n);
    }
}
  
/* This code contributed by PrinciRaj1992 */
输出:
26 28 29 28 25