HDU多校1003
来源:互联网 发布:ct数据增益处理 编辑:程序博客网 时间:2024/05/27 00:32
Give you an array A[1..n]A[1..n]of length nn.
Let f(l,r,k)f(l,r,k) be the k-th largest element of A[l..r]A[l..r].
Specially , f(l,r,k)=0f(l,r,k)=0 if r−l+1<kr−l+1<k.
Give you kk , you need to calculate ∑nl=1∑nr=lf(l,r,k)∑l=1n∑r=lnf(l,r,k)
There are T test cases.
1≤T≤101≤T≤10
k≤min(n,80)k≤min(n,80)
A[1..n] is a permutation of [1..n]A[1..n] is a permutation of [1..n]
∑n≤5∗105∑n≤5∗105
Input
There is only one integer T on first line.
For each test case,there are only two integers nn,kk on first line,and the second line consists of nn integers which means the array A[1..n]A[1..n]
Output
For each test case,output an integer, which means the answer.
Sample Input
1
5 2
1 2 3 4 5
Sample Output
Let f(l,r,k)f(l,r,k) be the k-th largest element of A[l..r]A[l..r].
Specially , f(l,r,k)=0f(l,r,k)=0 if r−l+1<kr−l+1<k.
Give you kk , you need to calculate ∑nl=1∑nr=lf(l,r,k)∑l=1n∑r=lnf(l,r,k)
There are T test cases.
1≤T≤101≤T≤10
k≤min(n,80)k≤min(n,80)
A[1..n] is a permutation of [1..n]A[1..n] is a permutation of [1..n]
∑n≤5∗105∑n≤5∗105
Input
There is only one integer T on first line.
For each test case,there are only two integers nn,kk on first line,and the second line consists of nn integers which means the array A[1..n]A[1..n]
Output
For each test case,output an integer, which means the answer.
Sample Input
1
5 2
1 2 3 4 5
Sample Output
30
题意:求所有区间第k大值的和
思路:链表,从依次最小数到第k大数处理,处理一个删一个,删除之后会留下一个空位,处理一个数只需得到大于此数的左边k个数和大于此数的右边k个数,上代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;const ll N=500005;ll pos[N],pre[N],nex[N];ll lef[105],righ[105];void shanchu(ll x){ ll p=pos[x],l=pre[p],r=nex[p]; nex[l]=r; pre[r]=l;}int main(){ ll T; scanf("%lld",&T); while(T--){ ll n,k,a; scanf("%lld%lld",&n,&k); for(ll i=1;i<=n;i++){ scanf("%lld",&a); pos[a]=i; } for(ll i=1;i<=n;i++){ pre[i]=i-1; nex[i]=i+1; } ll ans=0; for(ll x=1;x<=n;x++){ ll p=pos[x]; ll l_num=0,r_num=0; for(ll d=p;d!=0&&l_num<=k;d=pre[d])lef[++l_num]=d; for(ll d=p;d!=n+1&&r_num<=k;d=nex[d])righ[++r_num]=d; lef[++l_num]=0; righ[++r_num]=n+1; for(ll i=1;i<l_num;i++){ if(k+1-i>=1&&k+1-i<=r_num-1){ ans+=(righ[k+1-i+1]-righ[k+1-i])*(lef[i]-lef[i+1])*x; } } shanchu(x); } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- HDU多校1003
- HDU-1231&HDU-1003
- hdu 多校
- hdu 多校
- 多校 hdu
- 多校 hdu
- 多校 hdu
- HDU 多校
- HDU 1003
- hdu 1003
- hdu 1003
- HDU 1003
- HDU 1003
- hdu 1003
- hdu 1003
- hdu 1003
- hdu 1003
- hdu 1003
- python3中的列表特性
- POJ 2367Genealogical tree(拓扑排序)
- CC2640R2F BLE5.0 蓝牙概述
- 安卓基础8:服务
- 你还在傻傻分不清楚防篡改和WAF吗?
- HDU多校1003
- js获取table中的tr数量
- IO多路转接之select
- SpannableStringBuilder设置字符串不同颜色和大小
- 如何获取本地时间信息,并把getDay()函数获取到的日期改为日一二三四五六
- vuecli 苹果浏览器 按浏览器返回键 插件失效 ,安卓及pc端正常
- javascript基础(二) 个人笔记
- 浅谈Vue.js中v-for,javascript中for in 输出键值对的区别
- C语言(13)蛋蛋的问题(二)