分析:首先我们要知道调用swap()函数的次数跟什么有关。可以观察发现在Insertion Sort里,当且仅当a[j](j∈[0,i)) > a[i]时会调用一次swap(),也就是说有多少个a[j](j∈[0,i)) > a[i]成立就会调用多少次swap()(因为a[i]前面的序列都是有序的),这个数目也叫逆序数。所以要使调用swap()的次数最少,就应该要使整个序列的总的逆序数最小。这时,我们可以枚举所有不同的两个位置对换之后的总逆序数来找最小的总逆序数。但枚举的时候我们需要知道a[i]与a[j]对换之后各自的逆序数以及对换之前的逆序数,记为b[i][j]、b[j][i]、b[i][i]、b[j][j]。因为我们对换a[i]和a[j]的行为只会影响到i~j上的元素的逆序数,我们可以用b[i][j]、b[j][i]、b[i][i]、b[j][j]推导出对换之后的总逆序数的增量。只要求出这个最小的增量我们就可以求得最小的总逆序数。

那现在的关键就是比较快的求出数组b的所有元素的值。这个可以在O(n^2)的时间内完成。在n <= 5000的条件下应该还是可以接受的。

这道题花了不少时间,主要原因在于没把推导的公式写得清清楚楚,虽然知道推导公式的方法,但是没有很快的推出正确的公式。

 #include <iostream>
#include <queue>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
int a[], b[][], n;
int main(){
cin >> n;
for (int i = ; i < n; i++){
cin >> a[i];
}
for (int i = ; i < n; i++){
for (int j = ; j < n; j++){
b[i][j] = -;
}
}
for (int i = ; i < n; i++){
b[i][] = ;
for (int j = ; j < i; j++){
if (a[i] < a[j]){
b[i][j + ] = b[i][j] + ;
}else{
b[i][j + ] = b[i][j];
}
}
for(int j = i + ;j < n;j++){
if(a[i] < a[j]){
b[i][j] = b[i][j - ] + ;
}else{
b[i][j] = b[i][j - ];
}
}
}
int ans = ;
map<int,int> m;
for (int i = ; i < n; i++){
for (int j = i + ; j < n; j++){
int x = * (b[i][j] - b[i][i] + b[j][i] - b[j][j]) - (a[i] < a[j]?:) + (a[i] > a[j]?:);
m[x]++;
ans = min(ans,x);
}
}
int counter = ;
for (int i = ; i < n; i++){
int j = i;
while (j > && a[j] < a[j - ])
{
swap(a[j], a[j - ]); // swap elements a[j] and a[j - 1]
counter++;
j = j - ;
}
}
cout << counter + ans << " " << m[ans] << endl;
return ;
}

CodeForces 362C的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. C# 树次结构的数据

    public static void CreateTree(TreeView tv) { ///获取层次结构的数据 Tree tree = new Tree(); DataSet ds = tree. ...

  2. (四)Mybatis总结之接口映射

    前面Mybatis是直接通过Dao层与数据交互,更好的方法是Mybatis通过接口映射方式与数据交互 1.在项目中添加maven支持(即pom.xml下添加支持) <!-- 在pom.xml下配 ...

  3. 6.12---bug

  4. JS高级——文件操作

    https://www.cnblogs.com/mingmingruyuedlut/archive/2011/10/12/2208589.html https://blog.csdn.net/pl16 ...

  5. 使用脚本快速线程转储及列出高cpu线程

    jstack `ps -ef | grep java | grep bocai.jar | awk '{print $2}'` > cpu_high.logtop -b -n1 -Hp `ps ...

  6. Understanding and Analyzing Application Crash Reports

    Introduction When an application crashes, a crash report is created and stored on the device. Crash ...

  7. CAD使用SetXData写数据(网页版)

    主要用到函数说明: MxDrawEntity::SetXData 设置实体的扩展数据,详细说明如下: 参数 说明 [in] IMxDrawResbuf* pXData 扩展数据链表 js代码实现如下: ...

  8. Java变量及数据类型

    变量及数据类型 变量 变量定义格式:数据类型 变量名 = 初始化值; 基本数据类型 整形数据 package com.ahabest.demo; //输出整形数据的最小值,默认值,最大值,二进制位数 ...

  9. node 实现Token状态登录 及数据库增删改查

    1.项目目录结构 2.启动入口文件代码index.js const express = require('express') const bodyParser = require('body-pars ...

  10. (转)vim编辑器操作命令大全-绝对全

    周六了,熟悉熟悉vim 命令 学习链接: vim命令大全 http://blog.csdn.net/scaleqiao/article/details/45153379 vim命令小技巧 http:/ ...