【CROC 2016 - Elimination RoundB】【贪心】Mischievous Mess Makers 全排列1到n交换k次最多逆序数

来源:互联网 发布:哈达利雾化器做丝数据 编辑:程序博客网 时间:2024/05/08 19:22
Mischievous Mess Makers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 throughn, 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 1 and 5 during the first minute, then the cows in stalls2 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.


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n, k;int main(){while (~scanf("%d%d", &n,&k)){gmin(k, n / 2);LL ans = 0;for (int i = 1; i <= k; ++i){LL mid = (n - 2 * i);ans += 2 * mid + 1;}printf("%d\n", ans);}return 0;}/*【trick&&吐槽】很多sb都爆了int,>_<~~【题意】给你一个长度为n(1e5)的全排列,数字从1到n。我们最多可以交换k(1e5)次,任意交换位置。问你可以交换得到的最大逆序对数【类型】贪心【分析】每次交换最前最后即可*/


0 0
原创粉丝点击