日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 4911

發(fā)布時(shí)間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 4911 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Description

bobo has a sequence a 1,a 2,…,a n. He is allowed to swap two adjacent numbers for no more than k times.

Find the minimum number of inversions after his swaps.

Note: The number of inversions is the number of pair (i,j) where 1≤i<j≤n and a i>a j.

Input

The input consists of several tests. For each tests:

The first line contains 2 integers n,k (1≤n≤10 5,0≤k≤10 9). The second line contains n integers a 1,a 2,…,a n (0≤a i≤10 9).

Output

For each tests:

A single integer denotes the minimum number of inversions.

Sample Input

3 1 2 2 1 3 0 2 2 1

Sample Output

1 2 題意:相鄰的最多調(diào)換k次,使得逆對數(shù)最小, 思路,先求出整個(gè)序列的逆對數(shù)-k次就可,如果出現(xiàn)負(fù)數(shù)就輸出為0.用歸并排序求逆對數(shù)。值得注意的是千萬不要使用暴力會(huì)超時(shí),還有就要使用long long型不能也不行。輸出也是一個(gè)值得注意地方。 程序代碼: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define M 100007 #define ll _int64 ll a[M],c[M],ans,k; using namespace std; void merg(ll a[],int first,int mid,int last,ll c[]) {int i=first,j=mid+1;int m=mid,n=last;k=0;while(i<=m||j<=n){if(j>n||(i<=m&&a[i]<=a[j])) c[k++]=a[i++];else {c[k++]=a[j++]; ans+=(m-i+1);}}for(i=0;i<k;i++)a[first+i]=c[i]; } void merge_sort(ll a[], int first,int last,ll c[]) {if(first<last){int mid=(first+last)>>1;merge_sort(a,first,mid,c);merge_sort(a,mid+1,last,c);merg(a,first,mid,last,c);} } int main() { int n;ll k;while(scanf("%d%I64d",&n,&k)!=EOF){memset(a,0,sizeof(a));memset(c,0,sizeof(c));ans=0;for(int i=0;i<n;i++)scanf("%I64d",&a[i]);merge_sort(a,0,n-1,c);if(ans-k<=0)printf("0\n");else printf("%I64d\n",ans-k);}return 0; }

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/yilihua/p/4713239.html

總結(jié)

以上是生活随笔為你收集整理的HDU 4911的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。