题目链接:

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. MyEclipse开启Jquery智能提示

    myeclipse 增加javascript提示和jquery提示等不用安装插件自带功能 (对着需要提示的项目右键,点击properties) 不行的话就得安装插件: http://www.spket ...

  2. .net主站和二级域名下实现session共享

    public class CrossDomainCookie : IHttpModule { private string m_RootDomain = string.Empty; #region I ...

  3. 461在全志r16平台tinav3.0系统下使用地磁计QMC5883L

    461在全志r16平台tinav3.0系统下使用地磁计QMC5883L 2018/9/7 14:08 版本:V1.0 开发板:SC3817R SDK:tina v3.0 (基本确认全志tina v3. ...

  4. 使用 ArrayAdapter 来定制 ListView

    一个 ListView,其宽高都设为 match_parent,可以更省资源. activity_main.xml <ListView android:id="@+id/list_Vi ...

  5. R in action读书笔记(12)第九章 方差分析

    第九章方差分析 9.2 ANOVA 模型拟合 9.2.1 aov()函数 aov(formula, data = NULL, projections =FALSE, qr = TRUE, contra ...

  6. R Programming week 3-Debugging

    Something’s Wrong! Indications that something’s not right message: A generic notification/diagnostic ...

  7. JSP标签 <meta.....>作用总结

    <metahttp-equiv="pragma" content="no-cache"> <metahttp-equiv="cach ...

  8. Asp.Net MVC之 自动装配、动态路径(链接)等

    一.Model层 using System; using System.Collections.Generic; using System.Linq; using System.Web; namesp ...

  9. android 设置跳转

    android.provider.Settings. 1.   ACTION_ACCESSIBILITY_SETTINGS :    // 跳转系统的辅助功能界面            Intent ...

  10. java “==”和“equals”

    菜呀,只能记笔记了 ==:如果是基本数据类型,比较值,如果是引用类型,比较地址 equals:比较值