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. Spring框架基础(中)

    Spring对不同持久化技术进行支持 JDBC 导入spring-jdbc-4.3.5.RELEASE.jar.spring-tx-4.3.5.RELEASE.jar 创建对象,设置数据库信息 创建j ...

  2. 【代码笔记】Web-CSS-CSS Padding(填充)

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  3. pthread_once()函数详解

    转自:pthread_once()函数详解 pthread_once()函数详解   在多线程环境中,有些事仅需要执行一次.通常当初始化应用程序时,可以比较容易地将其放在main函数中.但当你写一个库 ...

  4. 想要在launcher中模拟按home键。

    Intent mHomeIntent = new Intent(Intent.ACTION_MAIN); mHomeIntent.addCategory(Intent.CATEGORY_HOME); ...

  5. 微信小程序控件 横/纵向排列

    控件(按钮)横向排列 wxss .view_class { display: flex; flex-direction: row; justify-content: center; } 控件(按钮)纵 ...

  6. web.xml文件介绍

    每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗? 一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的. ...

  7. 【原】Java学习笔记022 - 字符串

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 字符串 // 定义 ...

  8. Burpsuite 之intruder

    首先工具:Burpsuite1.7,DVWA,火狐浏览器代理插件,火狐浏览器,密码字典(以下用Bp代指burpsuite) 启动Bp,启动DVWA,并打开本地代理功能 ​ 开启bp的拦截功能,并在dv ...

  9. Go语言学习笔记-函数部分(三)

    函数部分 函数基本组成:关键字func.函数名.参数列表.返回值.函数体.返回语句 例子: func Add(int a, int b) (return int, err error){ ....函数 ...

  10. netstat简介

    netstat是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表,实际的网络连接以及每一个网络接口设备的状态信息,netstat用于显示与IP,TCP,UDP和ICMP协议相关的统计数据,一 ...