题解:Mischievous Mess Makers
2016-05-18 09:48
302 查看
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
output
input
output
分析:
使用贪心算法。很容易验证最佳算法为每次交换头尾两个数字。k>n/2时,达到极限。
Code:
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
分析:
使用贪心算法。很容易验证最佳算法为每次交换头尾两个数字。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; }
相关文章推荐
- ASF(Apache软件基金会) 新兴的顶级大数据项目
- 公钥私钥加密解密数字证书数字签名详解
- Java内部类面试
- maven 导出jar包以及导出dubbo服务jar包
- 每天laravel-20160809| Container -12
- c
- lucene(四) lucene分词器
- 5.16pkusc模拟赛2
- 即时通讯第三方SDK
- 哪些方法不能实施Spring AOP事务
- 每天laravel-20160809| Container -12
- ie8 不能加载dll的问题解决
- Java_Java SE6调用动态编译
- d
- b
- 《深入理解mybatis原理》 Mybatis数据源与连接池
- 【转】高仿淘宝首页,仿真度很高,大家学习下
- codeforces86D 莫队算法
- Serlvet 处理http请求并保持长连接
- 采购人确认采购文件要把握五要点