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. layui topbar图标(即返回顶部)未显示的解决方法

    在自己搭建纯html模板的时候,遇到了topbar无法显示的问题,搜了社区都没啥有用的解决方法,于是引用了util.js的源文件,发现用dom变量未获取到正确的scrollTop值,经过反复测试,把这 ...

  2. Android 性能优化之减少UI过度绘制

    什么是过度绘制(OverDraw) 在多层次重叠的UI结构里面,如果不可见的UI也在做绘制的操作,会导致某些像素区域被绘制了多次.这样就会浪费大量的CPU以及GPU资源.过度绘制最直观的影响就是会导致 ...

  3. QQ邮箱开启SMTP方法如何授权

    步骤一: 在打开的邮箱中心,进入设置 步骤二 从邮箱设置中心,进入帐户 步骤三 在这里可以看到POP3/SMTP服务被关闭了,因此客户端会收不到邮件,我们来开启它,查看该服务为已开启时,就可以用客户端 ...

  4. A Deep Learning-Based System for Vulnerability Detection

    本篇文献作者提出了一种基于深度学习来检测软件漏洞的方案.       摘要:作者开始基于深度学习的漏洞检测研究,是为了减轻专家手工定义特性的繁琐任务,需要制定一些指导性原则来适用于深度学习去进行漏洞探 ...

  5. DISK 100% BUSY,谁造成的?(ok)

    iostat等命令看到的是系统级的统计,比如下例中我们看到/dev/sdb很忙,如果要追查是哪个进程导致的I/O繁忙,应该怎么办? # iostat -xd ... Device: rrqm/s wr ...

  6. (转)Cesium教程系列汇总

    https://www.cnblogs.com/fuckgiser/p/5706842.html Cesium系列目录: 演示实例 ExamplesforCesium 最近老实有一些人问我,下载后在本 ...

  7. shell中的EOF用法

    重定位运算符 >> 是追加内容> 是覆盖原有内容 1.EOF Shell中通常将EOF与 << 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止 ...

  8. Linux:Day12(下) 进程、任务计划

    vmstat命令: vmstat [options] [delay [ count]] procs: r:等待运行的进程的个数: b:处于不可中断睡眠态的进程个数:(被阻塞的队列的长度): memor ...

  9. day22---面向对象基础初识

    面向过程编程: 核心是过程两个字,指的是解决问题的步骤,即先干什么再干什么,基于面向过程设计的程序就好比在设计一条流水线,是一种机械的思维方式. 优点:复杂问题流程化, 缺点:程序的可扩展性差 面向对 ...

  10. Mac中安装git后,终端运行git出错,提示安装Xcode

    mac用户不使用Xcode安装git之后,默认安装路径是: /usr/local/git 但是在终端运行 git 命令时候的路径是: /usr/bin/git 当我们输入 git 命令时出现如下错误, ...