题目链接:

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. SQL SERVER 事务例子

    存储过程格式: CREATE PROCEDURE YourProcedure AS BEGIN SET NOCOUNT ON; BEGIN TRY---------------------开始捕捉异常 ...

  2. MYSQL5.7 忘记ROOT密码/初始化ROOT密码

    编辑my.cnf允许空密码登录 [root@7Core ~]# vi /etc/my.cnf #在[mysqld]下加入一行 skip-grant-tables=1 重新启动Mysql服务 [root ...

  3. 死磕 java集合之LinkedList源码分析

    问题 (1)LinkedList只是一个List吗? (2)LinkedList还有其它什么特性吗? (3)LinkedList为啥经常拿出来跟ArrayList比较? (4)我为什么把LinkedL ...

  4. dede自定义表单放首页出错的解决办法

    一.当自定义表单放首页提交的时候跳出这个页面怎么解决 二.解决办法 可能有多个from表单提交出错,也就是代码冲突的意思,只要把代码检查好,from提交不要重复冲突就可以了

  5. MAC 添加共享,脚本执行

    Linux需要首先安装 yum install samba-client linxu添加windows 公共盘  mount -t cifs  user=guest,password=guest // ...

  6. Matlab基础之单元数组和结构数组

    Matlab基础之单元数组和结构数组 前言: 单元数组和结构数组是一种新的数据类型,能将不同类型.不同维数的数组组合在一起,从而方便对不同的数据类型方便管理和维护. 如上图所示的2*2矩阵中,分别存储 ...

  7. boostrapvalidator

    一个例子 <%@ page contentType="text/html;charset=UTF-8" language="java" %> < ...

  8. Zend Studio 离线汉化包下载方法

    进入eclipse官网 语言包位置 http://www.eclipse.org/babel/downloads.php

  9. D1. Toy Train (Simplified)

    D1. Toy Train (Simplified) time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  10. C# defult关键字

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