Mischievous Mess Makers

CodeForces - 645B

It is a balmy spring afternoon, and Farmer John's n cows are ruminating about link-cut cacti in their stalls. The cows, labeled 1 through n, are arranged so that the i-th cow occupies the i-th stall from the left. However, Elsie, after realizing that she will forever live in the shadows beyond Bessie's limelight, has formed the Mischievous Mess Makers and is plotting to disrupt this beautiful pastoral rhythm. While Farmer John takes his k minute long nap, Elsie and the Mess Makers plan to repeatedly choose two distinct stalls and swap the cows occupying those stalls, making no more than one swap each minute.

Being the meticulous pranksters that they are, the Mischievous Mess Makers would like to know the maximum messiness attainable in the k minutes that they have. We denote as pi the label of the cow in the i-th stall. The messiness of an arrangement of cows is defined as the number of pairs (i, j) such that i < j and pi > pj.

Input

The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100 000) — the number of cows and the length of Farmer John's nap, respectively.

Output

Output a single integer, the maximum messiness that the Mischievous Mess Makers can achieve by performing no more than k swaps.

Examples

Input
5 2
Output
10
Input
1 10
Output
0

Note

In the first sample, the Mischievous Mess Makers can swap the cows in the stalls 1and 5 during the first minute, then the cows in stalls 2 and 4 during the second minute. This reverses the arrangement of cows, giving us a total messiness of 10.

In the second sample, there is only one cow, so the maximum possible messiness is 0.

sol:如果交换次数>(n/2),答案就是n*(n-1)/2,否则暴力交换,求逆序对数量

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
ll n,m,a[N];
struct BIT
{
ll S[N];
#define lowbit(x) ((x)&(-x))
inline void Ins(int x)
{
for(;x<=n;x+=lowbit(x))
{
S[x]++;
}
}
inline ll Que(int x)
{
int Sum=;
for(;x>;x-=lowbit(x))
{
Sum+=S[x];
}
return Sum;
}
}T;
int main()
{
int i;
R(n); R(m);
if(m>=n/) return Wl(n*(n-)>>),;
else
{
for(i=;i<=n;i++) a[i]=i;
for(i=;i<=m;i++) swap(a[i],a[n-i+]);
// for(i=1;i<=n;i++) Wl(a[i]);
// puts("");
ll ans=;
for(i=n;i>=;i--)
{
ans+=T.Que(a[i]-);
T.Ins(a[i]);
// printf("ans=%d\n",ans);
}
Wl(ans);
}
return ;
}
/*
Input
5 2
Output
10 Input
1 10
Output
0 input
100000 40000
output
4799960000 input
100000 50000
output
4999950000
*/

codeforces645B的更多相关文章

  1. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

随机推荐

  1. Linux常见命令(一)

    三.常见linux命令: 命令提示符的解释: (1).组成(默认):[root@localhost~]# (2)[root@localhost~] root:系统当前登录账户名称,超级管理员为root ...

  2. HTML和CSS前端教程05-CSS盒模型

    目录 1. CSS盒模型 1.1 元素的尺寸 1.2. 元素内边距 padding 1.3. 元素外边距 margin 1.4. 处理溢出overflow 1.5. 元素的可见性Visibility ...

  3. 开源ERP-成功案例分析(3)

    Odoo用户概要 关于Odoo全球的用户,我们来看一些数据: Odoo目前全球有300万使用者 Odoo系统上每天新创建的数据库超过1000个 Odoo和Word.Excel.PowerPoint一样 ...

  4. ArcGIS JavaScriptAPI----- 缓冲区操作

    描述 使用ArcGIS Server 几何服务(geometry service)来对绘制在地图上的图形生成缓冲区.几何服务能够在基于浏览器的应用程序中执行缓冲操作(buffering),投影要素(p ...

  5. iOS-----------设置自定义字体

    1.将字体加入到项目中 2.在info.plist文件中加入相应信息,这一步实际上实在项目的Info页里面增加Fonts provided by application项,并设置相应的ttf文件进去, ...

  6. Android为TV端助力 关于4.0之后不能直接获取SD卡外部存储路径的问题

    Environment.getExternalStorageDirectory()是Android 2.x时代的产物,那时Android主流设备只有很小的内置存储器,然后都会外置一张sd卡,那时这个方 ...

  7. ArrayAdapter、SimpleAdapter简单用法

    1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects)   context:上下 ...

  8. Android 开源框架Glide的使用

    Glide是一个快速高效的多媒体管理和图像加载的框架,封装了Android平台的多媒体的解码,内存和硬盘缓存等,Glide支持解码.显示视频.图像和GIFs,Glide是基于定制的HttpUrlCon ...

  9. (四)版本控制管理器之VSS

    在上一篇<(二)版本控制管理器值CVS(下)>的文章中,我为大家介绍了CVS这个版本控制器,接下来我继续跟大家分享介绍下一个版本控制管理器--VSS,为什么要说这个版本控制器呢?早已过时的 ...

  10. ASP.NET Core 入门教程 4、ASP.NET Core MVC控制器入门

    一.前言 1.本教程主要内容 ASP.NET Core MVC控制器简介 ASP.NET Core MVC控制器操作简介 ASP.NET Core MVC控制器操作简介返回类型简介 ASP.NET C ...