Codeforces 362C. Insertion Sort
来源:互联网 发布:潍坊行知2016升学率 编辑:程序博客网 时间:2024/06/01 13:59
DP预处理+枚举逆序对
/** * 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); }}
2 0
- Codeforces 362C. Insertion Sort
- codeforces 362c Insertion Sort
- Codeforces 362C. Insertion Sort【构造】
- codeforces 362 C. Insertion Sort(树状数组,逆序数)
- cf-362C-Insertion Sort
- Codeforces Round #212 (Div. 2) C. Insertion Sort
- Codeforces Div.2 212 C Insertion Sort (DP)
- (C#)插入排序 Insertion Sort
- C. Insertion Sort----构造题
- Insertion Sort (Codeforces Round #212 (Div. 2))
- cf——C. Insertion Sort
- [C++] 插入排序法 (Insertion sort)
- c编写 Insertion or Heap Sort
- 【LeetCode】147. Insertion Sort List C语言
- Insertion Sort
- Insertion Sort
- insertion sort
- insertion sort
- 面向对象编程的弊端
- 黑马程序员-----Hello Word!!
- 开源夏令营之foldcolumn工具及解决方案(七)测试与总结
- memset函数详细说明
- Linux 路由 学习笔记 之七 策略规则的查找
- Codeforces 362C. Insertion Sort
- C++虚函数表解析
- 多校联赛总结
- C++ 继承中的覆盖,隐藏,虚拟继承
- Python_列表和元祖
- Android - 文件操作 小结
- 使用VMWareWorkstation10搭建学习环境(学习笔记)
- Symmetric Tree
- Android 获取地理位置的经度和纬度