codeforces645B
Mischievous Mess Makers
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
5 2
10
1 10
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的更多相关文章
- Codeforces645B【树状数组求逆序数】
题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...
随机推荐
- 为什么说Java程序员到了必须掌握Spring Boot的时候?
摘要: SpringBoot的来龙去脉. 原文:为什么说 Java 程序员到了必须掌握 Spring Boot 的时候? 微信公众号:纯洁的微笑 Fundebug经授权转载,版权归原作者所有. Spr ...
- 免费下载获取Odoo中文开发 指南 手册
引言 Odoo是一个强大的商业应用开源平台.在此基础上,构建了一套紧密集成的应用程序,涵盖了从CRM到销售到股票和会计的所有业务领域.Odoo有一个动态和不断增长的社区,不断增加功能.连接器和其他商业 ...
- JQuery显示,隐藏和淡入淡出效果
为了把JQuery搞熟悉,看着菜鸟教程,一个一个例子打,边看边记,算是一晚上的一个小总结吧.加油,我很本但是我很勤奋啊.系统的了解它,就要花时间咯. <!DOCTYPE html> < ...
- idea部署Maven入门(一)——环境变量的配置和下载
介绍: 1 Maven是用来管理jar包的一种工具, 2 Maven主要是构建java项目和java web项目 3 maven项目管理所依赖的jar ...
- EntityFramework Code-First 简易教程(六)-------领域类配置之DataAnnotations
EF Code-First提供了一个可以用在领域类或其属性上的DataAnnotation特性集合,DataAnnotation特性会覆盖默认的EF约定. DataAnnotation存在于两个命名空 ...
- Cs231n课堂内容记录-Lecture 9 深度学习模型
Lecture 9 CNN Architectures 参见:https://blog.csdn.net/qq_29176963/article/details/82882080#GoogleNet_ ...
- Docker资源限制
我们在容器中运行docker镜像的时候,可以指定一些设置容器cpu和内存的相关参数来进行限制,这样子尽量把容器资源做的相对稳定一些.这些参数是在docker run/create命令使用,比如: -- ...
- Extjs 改变grid行的背景颜色
## Ext grid 改变行背景色 Ext.util.CSS.createStyleSheet('.ts {background:#9a9a9bc2;}');//单独创建css样式 { xtype: ...
- IIS出现The specified module could not be found的解决方法
1.打开IIS 信息服务,在左侧找到自己的计算机,点右键,选择属性,在主属性中选编辑,打开“目录安全性”选项卡,单击“匿名访问和验证控制”里的“编辑”按钮,在弹出的对话框中确保只选中了“匿名访问 ...
- spring boot 2.0 WebMvcConfigurerAdapter过时解决方法
第一种: @Configuration public class WebAppConfig implements WebMvcConfigurer{ @Bean public HandlerInter ...