codeforces 655B B. Mischievous Mess Makers(贪心)

题目链接:

B. 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.

题意:问最多进行k此互换后,the number of pairs (i, j) such that i < j and pi > pj.

思路:由贪心可知,每次互换最外边的一对可以得到数目最大,每换一次最外边的一对,number增加2*n-3;

AC代码:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n,k,ans=0;
    cin>>n>>k;
        while(k--&&n>1)
        {
            ans+=2*n-3;
            n-=2;
        }
        cout<<ans<<endl;

    return 0;
}
时间: 2024-11-05 01:43:43

codeforces 655B B. Mischievous Mess Makers(贪心)的相关文章

CodeForces 645B Mischievous Mess Makers

简单题. 第一次交换$1$和$n$,第二次交换$2$和$n-1$,第三次交换$3$和$n-2$.....计算一下就可以了. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include&

CROC 2016 - Elimination Round Mischievous Mess Makers

这个题的意思是给你一个自然数序列1-n, 然后让你交换其中的一些数使得新序列的逆序对个数最大,  直接推公式即可, 代码如下: #include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { int n, k; scanf("%d%d", &n, &k); LL pa = min(n/2, k); LL res1 = (n-2*pa)*pa; LL res2 =

【打CF,学算法——三星级】CodeForces 645C Enduring Exodus (二分+贪心)

[CF简介] 提交链接:CF 645C 题面: C. Enduring Exodus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output In an attempt to escape the Mischievous Mess Makers' antics, Farmer John has abandoned his farm a

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces 442C Artem and Array(stack+贪心)

题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数,删除一个数的得分为两边数的最小值,如果左右有一边不存在则算作0分.问最大得分是多少. 解题思路:首先将连续的a,b,c,a > b && c > b的情况将c掉,获得min(a,b)分,这样处理后数组变成一个递増再递减的序列,除了最大和第二大的取不到,其他数字均可以得分. 样例:4 10 2 2 8 #include <cstdio> #include

Codeforces 18D Seller Bob java大数+贪心

题目链接:点击打开链接 java: import java.math.BigInteger; import java.util.Scanner; public class Main { static int N = 5005; static BigInteger[] er = new BigInteger[N]; static BigInteger E = new BigInteger("2"); static int[] a = new int[N]; static int[] ma

Codeforces 437C The Child and Toy(贪心)

题目连接:Codeforces 437C The Child and Toy 题目大意:孩子有一个玩具,有n个部件组成,m条绳子组成,每条绳子连接两个部件.小孩比较顽皮,要将玩具拆成不可分割的部件,每次剪断一条绳子的代价是该绳子连接的两个部件的权值中较小的值.问说最小的总代价是多少. 解题思路:以为每条边都是要被剪断的,所以将节点按照代价值从大到小排序,每次拿掉权值大的点,与该点连接并且还未剪断的边均用另外点的权值. #include <cstdio> #include <cstring

Codeforces 437D The Child and Zoo(贪心+并查集)

题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去参观动物园,动物园分很多个区,每个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么f值即为从一个区走到另一个区中所经过的路中权值最小的值做为权值.问,平均两个区之间移动的权值为多少. 解题思路:并查集+贪心.将所有的边按照权值排序,从最大的开始连接,每次连接时计算的次数为连接两块的节点数的积(乘法原理). #in

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3