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. zookeeper配置记录

    1. 准备三台机器,系统CentOS6 2. 将JDK和zookeeper安装包解压到目录 tar -zxvf jdk1.8.0_181-linux-x64.tar.gz -C /javatools ...

  2. ArcGIS 网络分析[3] 发布NAServer到ArcGIS for Server(以Server 10.4为例)

    前阵子对ArcGIS API For JavaScript的网络分析有兴趣,但是不知道其数据是如何获取的. 查阅API知道,AJS的网络分析只有三个功能:最短路径(RouteTask).最近设施点(C ...

  3. 彻底关闭Excle进程的几个方法

    之前研究过的问题,最近有朋友问,这里再总结下做一个笔记. 我们在应用程序里面通过创建Excle应用对象打开Excle的情况下,如果不注意几个问题,可能无法彻底关闭Excle进程,来考察下面的几种情况: ...

  4. jupyter notebook安装、登录

    pip install jupyter 提示pip需要升级(本人装的是anaconda) 输入:python -m pip install --upgrade pip 安装完成. 运行jupyter ...

  5. Java 位运算符和 int 类型的实现

    Java 位运算符和 int 类型的实现 其他运算符 # 算术运算符 +.-.*./.++i.i++.--i.i-- # 关系运算符 ==.!=.>.<.>=.<= # 逻辑运 ...

  6. Java文件下载:如何编码文件名称以及如何设置HttpServletResponse

    在下载文件时,经常遇到文件名乱码等问题. 本文说明如何编码文件名,以及如何设置HttpServletResponse对象. 1,如何编码文件名 String userAgent = request.g ...

  7. sklearn使用——梯度下降及逻辑回归

    一:梯度下降: 梯度下降本质上是对极小值的无限逼近.先求得梯度,再取其反方向,以定步长在此方向上走一步,下次计算则从此点开始,一步步接近极小值.需要注意的是步长的取值,如果过小,则需要多次迭代,耗费大 ...

  8. Extjs 判断对象是非为null或者为空字符串

    Ext.isEmpty(str,[allowEmptyString]) 如果str为 null undefined a zero-length array a zero-length string ( ...

  9. Tomcat与Nginx服务器的配合使用及各自的区别

    Nginx常用做静态内容服务和反向代理服务器,以及页面前端高并发服务器.适合做负载均衡,直面外来请求转发给后面的应用服务(tomcat ,django什么的),Tomcat更多用来做做一个应用容器,让 ...

  10. C++中 #if 和 #ifdef 区别

    以#开头的都是预编译指令,就是在正式编译之前,编译器做一些预处理的工作#if 条件语句程序段1 //如果条件语句成立,那么就编译程序段1#endif程序段2//如果条件不语句成立,那么就编译程序段2# ...