HDU 6058 枚举 Kanade's sum
2017-08-01 20:08
411 查看
题目
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2496 Accepted Submission(s): 1037
Problem Description
Give you an array A[1..n]
of length n.
Let f(l,r,k) be
the k-th largest element of A[l..r].
Specially , f(l,r,k)=0 if r−l+1<k.
Give you k ,
you need to calculate ∑nl=1∑nr=lf(l,r,k)
There are T test cases.
1≤T≤10
k≤min(n,80)
A[1..n] is a permutation of [1..n]
∑n≤5∗105
Input
There is only one integer T on first line.
For each test case,there are only two integers n,k on
first line,and the second line consists of n integers
which means the array 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
题目大意
给我们一个数组A,给了我们一个区间,变成for循环就是for(l从1到n) for(r从l到n) 求区间内第K大的数,把这些第K大的数累计求和
解题思路
采用枚举的思想,从每一个数组中的元素遍历他的左右边看分别有多少个比他大的数,这样我们就可以知道该元素可以有多少个第K大的组合可能
链表的代码,用链表省去了每次去寻找和比较比他大的数的时间,随着N的增大节省时间越多
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define read(a) scanf("%d",&a)
const int maxn=5*1e5+10;
int pre[maxn],nex[maxn],pos[maxn],f[maxn],b[maxn],a[maxn],n,k;
void dele(int p)//更新前驱和后继
{
if(p==1)
pre[nex[p]]=pre[p];
else if(p==n)
nex[pre[p]]=nex[p];
else
{
pre[nex[p]]=pre[p];
nex[pre[p]]=nex[p];
}
pre[p]=nex[p]=0;
}
void work()
{
// int n,k;
memset(f,0,sizeof(f));
memset(b,0,sizeof(b));
read(n);read(k);
for(int i=1;i<=n;i++)
{
read(a[i]);
pos[a[i]]=i;//记录a[i]的位置
pre[i]=i-1;//记录前驱
nex[i]=i+1;//记录后继
}
LL ans=0;
for(int i=1;i<=n-k+1;i++)//本代码的核心所在,因为是1-n的数所以可以是第k大的只可能是1-(n-k+1)这几个数
//从1开始找,因为1最小所以它的两边都是比他大的数,找完1之后,在dele()函数里面把1从链表里面拿下来
//这样继续找2,因为1不在链表里面了,所以2的两边都是比它大的数,以此类推直到n-k+1
{
int fro=0,beh=0;//front behind
int p=pos[i];
for(int v=p;v&&fro<=k;v=pre[v]) f[fro++]=v;
for(int v=p;v<=n&&beh<=k;v=nex[v]) b[beh++]=v;
f[fro]=0,b[beh]=n+1;
for(int j=0;j<fro&&j<k;j++)//fro有可能是k+1,要有j<k限制他,MMP,找了半个多小时,烦躁
{
if(k-1-j>beh-1)
continue;
{
int lnum=f[j]-f[j+1];
int rnum=b[k-1-j+1]-b[k-1-j];
ans+=1ll*i*lnum*rnum;
}
}
dele(p);
}
printf("%lld\n",ans);
}
int main()
{
//freopen("1003.in", "r", stdin);
//freopen("data.out", "w", stdout);
int T;
read(T);
while(T--)
{
work();
}
return 0;
}
Kanade's sum
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2496 Accepted Submission(s): 1037
Problem Description
Give you an array A[1..n]
of length n.
Let f(l,r,k) be
the k-th largest element of A[l..r].
Specially , f(l,r,k)=0 if r−l+1<k.
Give you k ,
you need to calculate ∑nl=1∑nr=lf(l,r,k)
There are T test cases.
1≤T≤10
k≤min(n,80)
A[1..n] is a permutation of [1..n]
∑n≤5∗105
Input
There is only one integer T on first line.
For each test case,there are only two integers n,k on
first line,and the second line consists of n integers
which means the array 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
题目大意
给我们一个数组A,给了我们一个区间,变成for循环就是for(l从1到n) for(r从l到n) 求区间内第K大的数,把这些第K大的数累计求和
解题思路
采用枚举的思想,从每一个数组中的元素遍历他的左右边看分别有多少个比他大的数,这样我们就可以知道该元素可以有多少个第K大的组合可能
#include<iostream> #include<cstdio> using namespace std; #define read(a) scanf("%d",&a) #define LL long long const int maxn=500000+10; int a[maxn]; int l[maxn],r[maxn]; int main() { //freopen("1003.in", "r", stdin); //freopen("data.out", "w", stdout); int t; read(t); while(t--) { int n,k; read(n); read(k); for(int i=0;i<n;i++) { read(a[i]); } LL ans=0; for(int i=0;i<n;i++) { int lcnt=1,rcnt=1,j;//lcnt代表元素a[i]左边比他大的数有多少个,rcnt同理 for( j=i+1;j<n;j++) { if(rcnt>k) break; if(a[j]>a[i]) { r[rcnt++]=j-i;//r[rcnt]代表右边第rcnt个比a[i]大的数距离a[i]的距离,这个是方便计算的,可以等于j, //但是计算的时候要特殊处理右边只有一个比a[i]大的时候,下方的rnum=1,比较麻烦, //原来是那样做的,不建议 } } if(j>=n) r[rcnt]=n-i; //如果a[i]右边比他大的数没超过k个, //那么我们知道a[i]右边比他大的数只有rcnt-1个, //我们假设距离a[i]最远的比他大的那个数为righht, //(程序中没有right这个变量,这里就是为了方便理解) //这里的r[rcnt]就是为了方便后面统计right右边有多少个比a[i]小的数 for(j=i-1;j>=0;j--) { if(lcnt>k) break; if(a[j]>a[i]) { l[lcnt++]=i-j;//同理上面 } } if(j<=0) l[lcnt]=i+1;//同理上面 for(j=0;j<lcnt;j++) { if(k-j-1>=rcnt) continue; int lnum=l[j+1]-l[j]; int rnum=r[k-j]-r[k-j-1]; ans+=(LL)a[i]*lnum*rnum; } } printf("%lld\n",ans); } return 0; }
链表的代码,用链表省去了每次去寻找和比较比他大的数的时间,随着N的增大节省时间越多
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define read(a) scanf("%d",&a)
const int maxn=5*1e5+10;
int pre[maxn],nex[maxn],pos[maxn],f[maxn],b[maxn],a[maxn],n,k;
void dele(int p)//更新前驱和后继
{
if(p==1)
pre[nex[p]]=pre[p];
else if(p==n)
nex[pre[p]]=nex[p];
else
{
pre[nex[p]]=pre[p];
nex[pre[p]]=nex[p];
}
pre[p]=nex[p]=0;
}
void work()
{
// int n,k;
memset(f,0,sizeof(f));
memset(b,0,sizeof(b));
read(n);read(k);
for(int i=1;i<=n;i++)
{
read(a[i]);
pos[a[i]]=i;//记录a[i]的位置
pre[i]=i-1;//记录前驱
nex[i]=i+1;//记录后继
}
LL ans=0;
for(int i=1;i<=n-k+1;i++)//本代码的核心所在,因为是1-n的数所以可以是第k大的只可能是1-(n-k+1)这几个数
//从1开始找,因为1最小所以它的两边都是比他大的数,找完1之后,在dele()函数里面把1从链表里面拿下来
//这样继续找2,因为1不在链表里面了,所以2的两边都是比它大的数,以此类推直到n-k+1
{
int fro=0,beh=0;//front behind
int p=pos[i];
for(int v=p;v&&fro<=k;v=pre[v]) f[fro++]=v;
for(int v=p;v<=n&&beh<=k;v=nex[v]) b[beh++]=v;
f[fro]=0,b[beh]=n+1;
for(int j=0;j<fro&&j<k;j++)//fro有可能是k+1,要有j<k限制他,MMP,找了半个多小时,烦躁
{
if(k-1-j>beh-1)
continue;
{
int lnum=f[j]-f[j+1];
int rnum=b[k-1-j+1]-b[k-1-j];
ans+=1ll*i*lnum*rnum;
}
}
dele(p);
}
printf("%lld\n",ans);
}
int main()
{
//freopen("1003.in", "r", stdin);
//freopen("data.out", "w", stdout);
int T;
read(T);
while(T--)
{
work();
}
return 0;
}
相关文章推荐
- HDU 2017 多校联合训练赛3 3003 6058 Kanade's sum 枚举 模拟链表
- HDU 6058 Kanade's sum 链表+枚举
- HDU 6058 Kanade's sum 逆序求第k大
- 2017 Multi-University Training Contest - Team 3 1003(hdu 6058) Kanade's sum(链表)(set)
- hdu 6058 Kanade's sum(链表)(2017 Multi-University Training Contest - Team 3 )
- 【多校训练】hdu 6058 Kanade's sum
- HDU 6058 Kanade's sum(链表)
- HDU-6058 Kanade's sum
- hdu 6058 Kanade's sum(链表)(2017 Multi-University Training Contest - Team 3 )
- HDU 6058 Kanade's sum
- hdu 6058 Kanade's sum(链表)(2017 Multi-University Training Contest - Team 3 )
- hdu 6058 Kanade's sum
- Kanade's sum(hdu 6058)
- hdu 6058 Kanade's sum(链表)(2017 Multi-University Training Contest - Team 3 )
- HDU 6058 Kanade's sum (链表)
- HDU 6058 Kanade's sum
- HDU 6058 Kanade's sum(多校3)
- 2017多校第3场 HDU 6058 Kanade's sum 双链表,思维
- hdu 6058 Kanade's sum(链表)(2017 Multi-University Training Contest - Team 3 )
- HDU 6058 Kanade's sum