题解:Mischievous Mess Makers
来源:互联网 发布:淘宝卖家开通直播 编辑:程序博客网 时间:2024/05/16 15:08
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.
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 a single integer, the maximum messiness that the Mischievous Mess Makers can achieve by performing no more than k swaps.
5 2
10
1 10
0
分析:
使用贪心算法。很容易验证最佳算法为每次交换头尾两个数字。k>n/2时,达到极限。
Code:
#include<stdio.h>int main(){long long int n,k;long long int i,j;long long int count = 0 ;int temp;scanf("%lld%lld",&n,&k);if(k>(n/2)){printf("%lld",n*(n-1)/2); return 0;}count=(2*n-k-1)*k/2+(k-1)*k/2+(n-2*k)*k;printf("%lld",count);return 0;}
- 题解:Mischievous Mess Makers
- CodeForces 645B Mischievous Mess Makers
- Code Forces 645B Mischievous Mess Makers
- Codeforces 645B Mischievous Mess Makers 【逆序对】
- Codeforces 645B Mischievous Mess Makers【逆序数】
- CodeForces 645 B.Mischievous Mess Makers(水~)
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))B. Mischievous Mess Makers(贪心)
- 【CROC 2016 - Elimination RoundB】【贪心】Mischievous Mess Makers 全排列1到n交换k次最多逆序数
- UVa 10338 - Mischievous Children
- 主从 (mess)
- [Linux] Mess
- The Logging Mess
- [mess]java基础1234
- The Maze Makers
- The Java Logging Mess
- UVA1586 Molar Mess
- CSU1566-The Maze Makers
- Network Mess UVA
- C#去掉HTML标记
- Java Servlet(二) Filter
- jdbc.properties内容
- 为什么没安装支付宝客户端时没有跳H5网页?
- 《深入理解mybatis原理》 Mybatis初始化机制详解
- 题解:Mischievous Mess Makers
- Cannot run program "aapt": error=13, Permission denied
- startUpdatingHeading开发一个指北针
- 如何把本地数据库导入服务器
- Java NIO使用及原理分析(二)
- oracle数据库性能优化
- codeforces86D 莫队算法
- busybox find
- Android Studio添加第三方库