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 ...
随机推荐
- Dynamics CRM教程:制作普通图表并放入仪表盘中
关注本人微信和易信公众号: 微软动态CRM专家罗勇,回复143或者20150325可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me! 我有个小需求,就是看我家特 ...
- 广州.NET微软技术俱乐部微信群有用信息集锦(10) - 大量json数据压缩方案
这是广州.NET微软技术俱乐部微信群有用信息集锦系列的其中一篇文章. 刚才微信群里有人问:“对于大量json数据返回的时候,有用过什么压缩方案吗?” 大家都给与了回答和帮助.包括: 开启gzi ...
- docker与虚拟机有何不同
docker与虚拟机有何不同 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化. 容器技术有 ...
- Django2.0 正则表示匹配的简单例子
在Django中,使用正则表达式,匹配Url 默认情况下,url固定格式如下: urlpatterns = [ path('admin/', admin.site.urls), ] 如果需要使用正则表 ...
- 从 RAID 到 Hadoop Hdfs 『大数据存储的进化史』
我们都知道现在大数据存储用的基本都是 Hadoop Hdfs ,但在 Hadoop 诞生之前,我们都是如何存储大量数据的呢?这次我们不聊技术架构什么的,而是从技术演化的角度来看看 Hadoop Hdf ...
- Linux创建和挂载XFS文件系统测试实践
XFS文件系统简介 维基百科关于XFS的简介如下: XFS is a high-performance 64-bit 年,由Silicon Graphics为他们的IRIX操作系统而开发,是IRIX ...
- [20190409]pre_page_sga=true与连接缓慢的问题.txt
[20190409]pre_page_sga=true与连接缓慢的问题.txt --//曾经遇到11g下设置pre_page_sga=true启动缓慢的问题(没有使用hugepages).--//链接 ...
- SQL Server数据库————连接查询和分组查询
SQL Server数据库————连接查询和分组查询 分组查询 select 列from <表名> where …… group by 列 注意:跟order by一样group ...
- win10 系统 wifi自动断开连接 wifi热点不稳定
我的系统的电脑是win10系统,笔记本 下载了一个wifi共享大师,但是wifi总是自动断,于是就找了找问题所在 在网上看了许多方案,大多数都是 在 电源管理 把[允许计算机关闭此设备以节 ...
- WIN提权总结【本地存档-转载】
[ web提权 ] 1.能不能执行cmd就看这个命令:net user,net不行就用net1,再不行就上传一个net到可写可读目录,执行/c c:\windows\temp\cookies\net1 ...