DP预处理+枚举逆序对

C. Insertion Sort
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array a of size n in the non-decreasing order.

for (int i = 1; i < n; i = i + 1)
{int j = i; while (j > 0 && a[j] < a[j - 1]){swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1]j = j - 1;}
}

Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1. He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.

It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the length of the permutation. The second line contains n different integers from0 to n - 1, inclusive — the actual permutation.

Output

Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that swapping the elements of the input permutation with indexes i and j leads to the minimum number of the executions.

Sample test(s)
input
5
4 0 3 1 2

output
3 2

input
5
1 2 3 4 0

output
3 4

Note

In the first sample the appropriate pairs are (0, 3) and (0, 4).

In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).

/*** Created by ckboss on 14-9-4.*/import java.util.*;public class InsertionSort {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int[] a = new int[n + 10];int[][] dp = new int[n + 10][n + 10];for (int i = 1; i <= n; i++) {a[i] = in.nextInt();}for (int i = 1; i <= n; i++) {for (int j = 0; j < n; j++) {int t = 0;if (a[i] < j) t = 1;dp[i][j] = dp[i - 1][j] + t;}}int ISN = 0;for (int i = 1; i <= n; i++)ISN += i - 1 - dp[i][a[i]];int MinISN = (1 << 30);for (int i = 1; i <= n; i++) {for (int j = i + 1; j <= n; j++) {if (a[i] > a[j]) {int len = j - i - 1;int p = dp[j - 1][a[i]] - dp[i][a[i]];int q = dp[j - 1][a[j]] - dp[i][a[j]];int temp = ISN + 2 * (q - p) - 1;if (temp < MinISN)MinISN = temp;}}}int ans = 0;for (int i = 1; i <= n; i++) {for (int j = i + 1; j <= n; j++) {if (a[i] > a[j]) {int len = j - i - 1;int p = dp[j - 1][a[i]] - dp[i][a[i]];int q = dp[j - 1][a[j]] - dp[i][a[j]];int temp = ISN + 2 * (q - p) - 1;if (temp == MinISN)ans++;}}}System.out.println(MinISN + " " + ans);}
}

Codeforces 362C. Insertion Sort相关推荐

  1. CodeForces - Insertion Sort(打表找规律)

    题目链接:http://codeforces.com/gym/101955/problem/C Time limit:6.0 s Memory limit:1024 MB Problem Descri ...

  2. CodeForces 362C

    分析:首先我们要知道调用swap()函数的次数跟什么有关.可以观察发现在Insertion Sort里,当且仅当a[j](j∈[0,i)) > a[i]时会调用一次swap(),也就是说有多少个 ...

  3. [Leetcode] Insertion Sort List

    Sort a linked list using insertion sort. 虽然算法很简单,但是链表操作起来实正是烦啊,特别要注意各种边界条件. 1 /** 2 * Definition for ...

  4. leetcode day2 -- Sort List Insertion Sort List

    1.Sort List Sort a linked list in O(n log n) time using constant space complexity. 分析:对链表排序不是第一次见,但是 ...

  5. [Leetcode]147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序 思路,递归到链表尾,然后循环插入: 1 /** 2 * Definition for singly-l ...

  6. LeetCode 147. Insertion Sort List 链表插入排序 C++/Java

    LeetCode 147. Insertion Sort List 链表插入排序 C++/Java Sort a linked list using insertion sort. A graphic ...

  7. Java实现插入排序及其优化 insertion sort

    本文带来八大排序算法之插入排序. 插入排序(Insertion Sort)属于内部排序算法,是对于欲排序的元素以插入的方式找寻该元素的适当位置,以达到排序的目的. 插入排序基本思想: 把n个待排序的元 ...

  8. python实现排序算法_python实现·十大排序算法之插入排序(Insertion Sort)

    简介 插入排序(Insertion Sort)是一种简单直观的排序算法.它的工作原理是:通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入. 算法实现步骤 从第一个元素开 ...

  9. c++Insertion Sort插入排序的实现算法(附完整源码)

    C++Insertion Sort插入排序的实现算法 C++Insertion Sort插入排序的实现算法完整源码(定义,实现,main函数测试) C++Insertion Sort插入排序的实现算法 ...

最新文章

  1. java list 截取部分数据_Java List.subList()方法:获取列表中指定范围的子列表
  2. 百度股价接连暴涨的背后,看Apollo的2020
  3. native-maven-plugin与maven-nar-plugin配置
  4. 安全系列------web环境搭建组合
  5. 13-Java和Scala中的Future
  6. Linux “身陷囹圄”?
  7. 【Android】实现应用简单的用户登录界面
  8. faketime实现游戏服务器时间定制
  9. 算法复杂度描述中为什么用“logn”,而不用“log2n”、“lnn”或“lgn”
  10. 图文安装VMware Workstation教程
  11. 力扣刷题 DAY_74 回溯
  12. python27安装get-pip
  13. http协议及httpd配置
  14. (11)LAN体系结构及各层的主要功能
  15. 谷歌研究员走火入魔事件曝光:认为AI已具备人格,被罚带薪休假,聊天记录让网友San值狂掉...
  16. 苹果痛失全球市值第一宝座
  17. Swagger 3.0
  18. opencv4.5.3学习——开发环境搭建
  19. win7计算机电源设置在哪里设置,win7系统高级电源管理怎么打开?win7系统设置高级电源管理的方法...
  20. 谈计算机知识对学生的作用,浅谈中学生计算机教育的意义.doc

热门文章

  1. 超级计算机天河二号容量,“天河二号”蝉联最快超级计算机
  2. PageAdmin Cms网站管理系统如何修改后台目录
  3. EBS开发_费用类采购订单创建
  4. Python通过GeoIP获取IP信息(国家、城市、经纬度等)
  5. 如果你想通过自媒体赚钱,平台规则必须了如指掌,才能月入10W+
  6. 转换文件格式的时候总是没有反应
  7. 3D打印机Ender-3 V2 调传动比
  8. 网络摄像头监控中什么情况下需要使用流媒体转发服务器?
  9. WPF 让普通 CLR 属性支持 XAML 绑定(非依赖属性),这样 MarkupExtension 中定义的属性也能使用绑定了
  10. 怎样渲染EDIUS中的不实时特效