链接:戳这里
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)-1
using 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;
}