• Larry and Inversions


    Larry just studied the algorithm to count number of inversions. He's very interested in it. He's considering another problem: Given a permutation of integers from 1 to n, how many inversions it has if we reverse one of its subarray?

    Formally speaking, given an integer array a (indices are from 0 to n1) which contains a permutation of integers from 1 to n, two elements [ and [ form an inversion if [ and i<j. Your job is to count, for each pair of 0, the number of inversions if we reverse the subarray from [ to [.

    Input Specification:

    Each input file contains one test case. Each case consists of a positive integer n (≤) in the first line, and a permutation of integers from 1 to n in the second line. The numbers in a line are separated by a single space.

    Output Specification:

    For each test case, output ( integers in a single line. The results are for reversing subarray indicating by all possible pairs of indices 0 in i-major order -- that is, the first n results are for the reverse of subarrary [0..0], [0..1], ...[0..n1]; the next n1 results are for the reverse of subarry [1..1], [1..2],..., [1..n1] and so on.

    All the numbers in a line must be separated by a single space, with no extra space at the beginning or the end of the line.

    Sample Input:

    3
    2 1 3
    
     

    Sample Output:

    1 0 2 1 2 1
    
     

    Hint:

    The original array is { 2, 1, 3 }.

    • Reversing subarray [0..0] makes { 2, 1, 3 } which has 1 inversion.
    • Reversing subarray [0..1] makes { 1, 2, 3 } which has 0 inversion.
    • Reversing subarray [0..2] makes { 3, 1, 2 } which has 2 inversions.
    • Reversing subarray [1..1] makes { 2, 1, 3 } which has 1 inversion.
    • Reversing subarray [1..2] makes { 2, 3, 1 } which has 2 inversions.
    • Reversing subarrays [2..2] makes { 2, 1, 3 } which has 1 inversion.
       1 #include<bits/stdc++.h>
       2 using namespace std;
       3 int n,k;
       4 vector<int> v;
       5 vector<int> vl;
       6 int main()
       7 {
       8 //  freopen("data.txt","r",stdin);
       9       scanf("%d",&n);
      10       vl.resize(n*n,0);
      11       for(int i=0;i<n;i++)
      12       {
      13         scanf("%d",&k);
      14         v.emplace_back(k-1);
      15     }
      16     int brev=0;
      17     for(int i=0;i<n;++i)
      18     {
      19         int rev=0;
      20         for(int j=i;j<n;++j)
      21         {
      22             if(v[j]<v[i])
      23             rev++;
      24             vl[i*n+j]=rev;
      25         }
      26         brev+=rev;
      27     }
      28     bool flag=false;
      29     for(int i=0;i<n;i++)
      30     for(int j=i;j<n;j++)
      31     {
      32         if(i==j)
      33         {
      34             if(flag)
      35             printf(" ");
      36             else
      37             flag=true;
      38             printf("%d",brev);
      39         }
      40         else
      41         {
      42             int drev=0;
      43             for(int t=i;t<j;t++)
      44             drev+=vl[t*n+j];
      45             printf(" %d",brev-2*drev+(j-i)*(j-i+1)/2);
      46         }
      47     }
      48     return 0;
      49 }
    诚者,君子之所守也。
  • 相关阅读:
    JDBC_增删改
    JSP内置对象作用域
    497. Random Point in Non-overlapping Rectangles
    921. Minimum Add to Make Parentheses Valid
    946. Validate Stack Sequences
    824. Goat Latin
    Acwing 165 小猫爬山 (dfs)
    Codeforces #656 Problem D
    Codeforces #656 Problem C
    对象属性拷贝工具类大全
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285752.html
Copyright © 2020-2023  润新知