1. 程式人生 > >HDU6058-Kanade's sum

HDU6058-Kanade's sum

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

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
30

Source
2017 Multi-University Training Contest - Team 3

題目大意:有一個1~n的排列,問所有區間中第k大的數的總和是多少?
解題思路:我們只要求出對於一個數x左邊最近的k個比他大的和右邊最近k個比他大的,掃一下就可以知道有幾個區間的k大值是x.
我們考慮從小到大列舉x,每次維護一個雙向連結串列,連結串列裡只有>=x的數,那麼往左往右找只要暴力跳k次,刪除也是O(1)的。
時間複雜度:O(nk)

#include<iostream>
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; typedef long long LL; const int INF=0x3f3f3f3f; const LL MOD=1e9+7; const int MAXN=1e6+7; int a[MAXN],pos[MAXN]; int s[MAXN],t[MAXN]; int pre[MAXN],nxt[MAXN]; int n,k; void erase(int x) { int pp=pre[x],nn=nxt[x]; if(pre[x]) nxt[pre[x]]=nn; if(nxt[x]<=n) pre[nxt[x]]=pp; pre[x]=nxt[x]=0; } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&k); for(int i=1;i<=n;++i) { scanf("%d",&a[i]); pos[a[i]]=i; } for(int i=1;i<=n;++i) { pre[i]=i-1; nxt[i]=i+1; } // for(int i=1;i<=n;++i) // { // cout<<pre[i]<<" "<<nxt[i]<<endl; // } LL ans=0; for(int num=1;num<=n-k+1;++num) { int p=pos[num];//cout<<p<<endl; int s0=0,t0=0; for(int d=p;d&&s0<=k;d=pre[d]) s[++s0]=d; for(int d=p;d!=n+1&&t0<=k;d=nxt[d]) t[++t0]=d; s[++s0]=0;t[++t0]=n+1; // cout<<endl; // cout<<endl; // for(int i=1;i<=s0;i++) // { // cout<<s[i]<<" "; // } // cout<<endl; // cout<<endl; // for(int i=1;i<=t0;i++) // { // cout<<t[i]<<" "; // } // cout<<endl; // cout<<endl; for(int i=1;i<=s0-1;++i) { if(k+1-i<=t0-1&&k+1-i>=1) { ans+=(LL)(t[k+1-i+1]-t[k+1-i])*(s[i]-s[i+1])*num; } } erase(p); } printf("%lld\n",ans); } return 0; }

相關推薦

HDU6058-Kanade's sum

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

HDU6058 Kanade's sum

Kanade’s sum 題目大意就是給你一個1~n的一個排列,求滿足題意的所有區間[l,r](區間長度至少為k且1 <= l <= r <= n)中第k大的數之和。 舉個例子: 比如說給你 n = 4, k = 2,其中數列是a[4

HDU 6058 列舉 Kanade's sum

題目 Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2496    Accept

2017 HDU 6058 多校聯合賽 Kanade's sum

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

hdu 6058 Kanade's sum(鏈表)

php blank pri 暴力 splay href .cn 每次 n) 題目鏈接:hdu 6058 Kanade‘s sum 題意: 給你一個n個數的排列,問你全部區間第k大的總和為多少。 題解: 我們只要求出對於一個數x左邊最近的k個比他大的和右邊最近k個比他大的,掃

6058 Kanade's sum 鏈表維護()

bsp ans span min name -- const div amp 題意:給出元素為[1,n]的排列a,定義f[l,r,k]為區間[l,r]內第k大的元素.給出k,求 累加和(l=1~n,r~l~n)f[l,r,k] . n<=5e5,k<=min(8

hdu-6058 Kanade's sum

con sta cst getchar urn bsp pen define set   題意:略   思路:要我們求每個區間第K大數之和,其實可以轉換為求多少個區間的第K大數是X,然後我們在求和就好了。      那麽我們可以從小到大枚舉所有可能成為第K大的數。為什麽從小

【鏈表】2017多校訓練3 HDU 6058 Kanade's sum

iostream ++ 多校 open pos cnblogs names mat play acm.hdu.edu.cn/showproblem.php?pid=6058 【題意】 給定一個排列,計算 【思路】 計算排列A中每個數的貢獻,即對於每個ai,計算有

HDU 6059 Kanade's trio(字典樹)

Description 給出一個長度為n的序列A[1]~A[n],求滿足i < j < k且(A[i]^A[j])<(A[j]^A[k])的三元組(i,j,k)個數 Input 第一行一整數T表示用例組數,每組用例首先輸入一整數n表示序列

hdu 6059 Kanade's trio(trie+容斥)

滿足 struct ons efi isp 鏈接 cnblogs define con 題目鏈接:hdu 6059 Kanade‘s trio 題意: 給你n個數,讓你找有多少個(i,j,k),使得i<j<k滿足a[i]^a[j]<a[j]^a[k]。 題

HDU 6059 17多校3 Kanade's trio(字典樹)

要求 tro none sat details num cst void stream Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k)

HDU 6057 Kanade's convolution

urn oca 鏈接 官方 序列 我們 vol clas 解決 題目鏈接:HDU-6057 題意: 思路:先按照官方題解推導出下面的式子: 現在唯一的問題就是怎麽解決[bit(x)-bit(y)=bit(k)]的問題。 我們定義\( F(A,k)_{i}=\left[

1007 Maximum Subsequence Sum (25 point(s))

 1007 Maximum Subsequence Sum (25 point(s)) 部分未通過 22分 #include<iostream> #include<vector> #include<algorithm> using namespa

解決Python中sum函數出現的TypeError: unsupported operand type(s) for +: 'int' and 'list'錯誤問題

當在Python中執行sum函式時,會出現“TypeError: unsupported operand type(s) for +: 'int' and 'list'”這樣的問題 程式碼如: print(sum([[1, 2, 3], [4, 5, 5]])) 會出現如下的錯誤:

Codeforces Round #248 (Div. 1) C - Tachibana Kanade's Tofu AC自動機

C - Tachibana Kanade's Tofu 思路:把 n 個串丟進AC自動機中,然後dp就好啦。 我的程式碼居然是在CF上跑最快的。。 #include<bits/stdc++.h> #define LL long long #define fi first #defi

1007 Maximum Subsequence Sum (25)(25 point(s))(cj)

1007 Maximum Subsequence Sum (25)(25 point(s))Questions Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequenc

UVA - 434 Matty&#39;s Blocks

mes [0 () block += tty scan 一個 ems 題意:給你正視和側視圖,求最多多少個,最少多少個 思路:貪心的思想。求最少的時候:由於能夠想象著移動,盡量讓兩個視圖的重疊。所以我們統計每一個視圖不同高度的個數。然後計算。至於的話。就是每次拿正視圖的

C/s模式&&B/S模式

http client ref 最大 aid 都是 信息 管理系 電子商務網 C/s模式:是客戶端/服務器(Client/Server)模式,主要指的是傳統的桌面級的應用程序。比如我們經常用的信息管理系統。 C/S 客戶端/服務器 例如QQ,網絡遊戲,需要下載客戶端才能訪

leetcode 209. Minimum Size Subarray Sum

urn https scrip subarray () ins -s mar color https://leetcode.com/problems/minimum-size-subarray-sum/#/description 題目很簡單。題意就是求字符串中長度最小的一

並行編程(2) - sum.msic.Unsafe 二

des address () ++ lec ash one java security 整理了幾個曾經從網上記錄sum.msic.Unsafe類的演示樣例。供大家參考: package com.fish.unsafe; import java.io.File; i