hdu 6058 链表

传送门

Kanade's sum

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1135    Accepted Submission(s): 470


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  rl+1<k .

Give you  k  , you need to calculate  nl=1nr=lf(l,r,k)

There are T test cases.

1T10

kmin(n,80)

A[1..n] is a permutation of [1..n]

n5105
 

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
  
  
30
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6066  6065  6064  6063  6062 
题意:找区间第K大的是谁,然后+=这个数 。最后问sum。

做法:比赛的时候想到了每个点左右k个位置看贡献有多少。就是没想到用链表维护每个点的位置。太菜了。

//china no.1
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=5e5+10;
const int maxx=1e7+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline int Scan()
{
    int Res=0,ch,Flag=0;
    if((ch=getchar())=='-')Flag=1;
    else if(ch>='0' && ch<='9')Res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
    return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int pre[maxn],nex[maxn],n,k,a[maxn],pos[maxn],num1[maxn],num2[maxn];
LL solve(int x)
{
    int x1=0,x2=0;
    for(int i=x;i&&x1<=k;i=pre[i])
        num1[++x1]=i-pre[i];
    for(int i=x;i<=n&&x2<=k;i=nex[i])
        num2[++x2]=nex[i]-i;
    LL ans=0;
    for(int i=1;i<=x1;i++)
        if(k-i+1<=x2&&k-i>=0)
            ans+=num1[i]*num2[k-i+1];
    return ans;
}

void del(int x)
{
    pre[nex[x]]=pre[x];
    nex[pre[x]]=nex[x];
}
int main()
{
    int t;
    t=Scan();
    while(t--)
    {
        n=Scan();k=Scan();
        for(int i=1;i<=n;i++)
        {
            a[i]=Scan();
            pos[a[i]]=i;
        }
        for(int i=0;i<=n+1;i++)
        {
            pre[i]=i-1;
            nex[i]=i+1;
        }
        pre[0]=0;
        nex[n+1]=n+1;
        LL ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=solve(pos[i])*i;
            del(pos[i]);
        }
        cout<<ans<<endl;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值