CROC 2016 - Elimination Round B 树状数组处理逆序对个数
来源:互联网 发布:手机优酷网络连接失败 编辑:程序博客网 时间:2024/05/20 02:55
链接:戳这里
B. Mischievous Mess Makers
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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
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 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.
题意:给出n,k (1<=n,k<=1e5) n表示1~n的序列,k表示可以交换序列元素k次,问换了k次之后,逆序对的最大num
思路:额,肯定能换的直接换的 但是当k>=n/2时 就只需要换n/2次,所以特判一下直接输出答案
不能换的话肯定选最左最右的换啊 暴力换了之后 树状数组维护逆序对数量
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;int n,k;int a[1000100];ll sum[1000100];int lowbit(int x){ return x&(-x);}void add(int x,int v){ while(x<=n){ sum[x]+=v; x+=lowbit(x); }}ll getsum(int x){ ll ans=0; while(x>=1){ ans+=sum[x]; x-=lowbit(x); } return ans;}int main(){ scanf("%d%d",&n,&k); ll ans=0; for(int i=1;i<=n;i++) { a[i]=i; if(i!=n) ans+=i; } if(k>=n/2) cout<<ans<<endl; else { ans=0; for(int i=1;i<=k;i++) swap(a[i],a[n-i+1]); for(int i=1;i<=n;i++){ add(a[i],1); ans+=(ll)i-getsum(a[i]); } cout<<ans<<endl; } return 0;}
0 0
- CROC 2016 - Elimination Round B 树状数组处理逆序对个数
- Codeforces CROC 2016 - Elimination Round
- CROC 2016 - Elimination Round C 二分
- CROC 2016 - Elimination Round(A)bfs
- (CROC 2016 - Elimination Round (Rated Unofficial Edition))B. Mischievous Mess Makers(贪心)
- CROC 2016 - Elimination Round (Rated Unofficial Edition)题解
- CROC 2016 - Elimination Round D 二分+拓扑序+bfs
- 树状数组的简单应用(求逆序对个数)
- POJ 3067 Japan(树状数组求逆序对个数)
- 树状数组逆序对
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round (ACM-ICPC)
- CROC-MBTU 2012, Elimination Round / 245F Log Stream Analysis (日期处理&二分)
- 树状数组详解(处理在线数据,求逆序对)
- poj1007 树状数组逆序对
- 树状数组求逆序对
- 求出data为首地址的100D字数组中的最小偶数,并把它存放在AX中,目前只能做出无符号数,有待修改
- JavaScript-数据类型-隐式转换1
- 【心血之作】linux虚拟机下安装配置Hadoop(完全分布式)生态环境(hadoop2.2.0,HBase0.98,Hive0.13(连接oracle),sqoop1.4.4(连接oracle)
- 大数据量高并发的数据库优化
- POJ1275 Cashier Employment
- CROC 2016 - Elimination Round B 树状数组处理逆序对个数
- Python itertools模块详解
- leetcode017 Letter Combinations of a Phone Number
- VBA基础
- Linux 上的常用文件传输方式介绍与比较
- c语言学习路程 冒泡排序算法void bubble(int i )函数
- 实用的 Python 之 feedparser
- JavaScript语言精粹——数组
- nRF52832之硬件I2C