题目链接:

http://codeforces.com/problemset/problem/645/B

题意:

给定步数和排列,每步可以交换两个数,问最后逆序数最多是多少对?

分析:

看例子就能看出来肯定是不断往中间逼近,然后交换头尾两个,给定交换的对数,直接算就好了,复杂度O(1)

代码:

#include<iostream>
using namespace std;
typedef long long ll;
int main (void)
{
ll n, m;
cin>>n>>m; ll res = 0;
ll ans = min(n/2, m);
res = (n - 1+ (n - ans + 1))/ 2 * ans + (n - 2 * ans + n - ans - 1)/2 * ans;
cout<<res<<endl; return 0;
}

智商压制

直接用所有对数减去为改变的对数就好了嘛

#include<iostream>
using namespace std;
int main (void)
{
long long n, m;
cin>>n>>m;
long long res = max(n - 2 * m, 0ll);
long long ans = n * (n - 1)/2 - res * (res - 1) / 2;
cout<<ans<<endl; return 0;
}

然后求逆序数的经典方法就是树状数组了,这里写下,时间复杂度O(nlogn)

#include<iostream>
using namespace std;
typedef long long ll;
const int maxn = 400020;
int bit[maxn], a[maxn];
int n, m;
int sum(int i)
{
int s = 0;
while(i){
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x)
{
while(i <= n){
bit[i] += x;
i += i & -i;
}
}
int main (void)
{
cin>>n>>m;
if(m >= n / 2){
cout<<(ll) n * (n - 1)/2<<endl;
return 0;
}
for(int i = 1; i <= n; i++) a[i] = i;
for(int i = 1; i <= m; i++) swap(a[i], a[n - i + 1]);
ll res = 0;
for(int i = 1; i <= n; i++){
add(a[i], 1);
res +=i - sum(a[i]); }
cout<<res<<endl;
}

Codeforces 645B Mischievous Mess Makers【逆序数】的更多相关文章

  1. CodeForces 645B Mischievous Mess Makers

    简单题. 第一次交换$1$和$n$,第二次交换$2$和$n-1$,第三次交换$3$和$n-2$.....计算一下就可以了. #pragma comment(linker, "/STACK:1 ...

  2. Code Forces 645B Mischievous Mess Makers

    It is a balmy spring afternoon, and Farmer John's n cows are ruminating about link-cut cacti in thei ...

  3. codeforces 655B B. Mischievous Mess Makers(贪心)

    题目链接: B. Mischievous Mess Makers time limit per test 1 second memory limit per test 256 megabytes in ...

  4. CROC 2016 - Elimination Round (Rated Unofficial Edition) B. Mischievous Mess Makers 贪心

    B. Mischievous Mess Makers 题目连接: http://www.codeforces.com/contest/655/problem/B Description It is a ...

  5. Codeforces Gym 100463A Crossings 逆序数

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  6. Codeforces Round #261 (Div. 2)459D. Pashmak and Parmida&#39;s problem(求逆序数对)

    题目链接:http://codeforces.com/contest/459/problem/D D. Pashmak and Parmida's problem time limit per tes ...

  7. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

  8. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

  9. CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序

    http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...

随机推荐

  1. 2017广东工业大学程序设计竞赛决赛 G 等凹数字

    题意: Description 定义一种数字称为等凹数字,即从高位到地位,每一位的数字先非递增再非递减,不能全部数字一样,且该数是一个回文数,即从左读到右与从右读到左是一样的,仅形成一个等凹峰,如54 ...

  2. 块级元素的text-align对行内元素和果冻元素(inline-block)的作用

    块级元素社设置了text-align:center以后,对其直接行内元素/果冻元素.继承行内元素/果冻元素都会产生“居中效应”. <style> .test4{ text-align: c ...

  3. 微信小程序button授权页面,用户拒绝后仍可再次授权

    微信小程序授权页面,进入小程序如果没授权跳转到授权页面,授权后跳转到首页,如果用户点拒绝下次进入小程序还是能跳转到授权页面,授权页面如下 app.js  中的 onLaunch或onShow中加如下代 ...

  4. 21全志r58m平台的framework在使用过程中会莫名的崩溃掉

    21全志r58m平台的framework在使用过程中会莫名的崩溃掉 2018/10/25 16:20 版本:V1.0 开发板:SC5806 1.系统编译: rootroot@cm88:/home/ww ...

  5. iTOP-6818开发板-Android4.4系统下RFID射频模块测试例程

    平台:迅为iTOP-6818开发板 系统:Android4.4版本 例程:RFID射频模块测试例程 rc522 驱动在 Android 系统的内核是默认集成的,用户可以在开发板上使用命令“ls /de ...

  6. scrapy 的分页爬取 CrawlSpider

    1.创建scrapy工程:scrapy startproject projectName 2.创建爬虫文件:scrapy genspider -t crawl spiderName www.xxx.c ...

  7. C# defult关键字

    一.问题 今天写一个函数提示用defult,因为第一次用记录一下 public static T GetConfig<T>(string strConfig) { try { return ...

  8. [Python3网络爬虫开发实战] 3.2.1-基本用法

    1. 准备工作 在开始之前,请确保已经正确安装好了requests库.如果没有安装,可以参考1.2.1节安装. 2. 实例引入 urllib库中的urlopen()方法实际上是以GET方式请求网页,而 ...

  9. Linux htop工具使用详解【转】

    原文地址: http://www.cnphp6.com/archives/65078 一.Htop的使用简介 大家可能对top监控软件比较熟悉,今天我为大家介绍另外一个监控软件Htop,姑且称之为to ...

  10. 数组排序函数-php数组函数(一)

    数组排序,共13个 函数中有u的,能自定义比较函数:有k的,按照键来排序:有r(reverse)的,倒序:有a(association)的,一定是键值关联,除了rsort() usort() sort ...