hdu5489 ||2015合肥網路賽1006 dp+離散化樹狀陣列優化
http://acm.hdu.edu.cn/showproblem.php?pid=5489
Removed Interval
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 656 Accepted Submission(s): 245
Problem Description
Given a sequence of numbers A=a1,a2,…,aN,
a subsequence b1,b2,…,bk of A is
referred as increasing if b1<b2<…<bk.
LY has just learned how to find the longest increasing subsequence (LIS).
Now that he has to select L consecutive numbers and remove them from A for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?
Now that he has to select L consecutive numbers and remove them from A for some mysterious reasons. He can choose arbitrary starting position of the selected interval so that the length of the LIS of the remaining numbers is maximized. Can you help him with this problem?
Input
The first line of input contains a number T indicating
the number of test cases (T≤100).
For each test case, the first line consists of two numbers N and L as described above (1≤N≤100000,0≤L≤N). The second line consists of N integers indicating the sequence. The absolute value of the numbers is no greater than 109.
The sum of N over all test cases will not exceed 500000.
For each test case, the first line consists of two numbers N and L as described above (1≤N≤100000,0≤L≤N). The second line consists of N integers indicating the sequence. The absolute value of the numbers is no greater than 109.
The sum of N over all test cases will not exceed 500000.
Output
For each test case, output a single line consisting of “Case #X: Y”. X is
the test case number starting from 1. Y is
the maximum length of LIS after removing the interval.
Sample Input
2
5 2
1 2 3 4 5
5 3
5 4 3 2 1
Sample Output
Case #1: 3
Case #2: 1
/**
hdu5489 ||2015合肥網路賽1006 dp+樹狀陣列優化
題目大意:給定一個序列,求去掉一個連續長度為L的數後的最長上升子序列的長度
解題思路:用dp[i][0]表示前i個數不去L時的最長子序列,dp[i][1]表示前i個數去L時的最長子序列。
轉移方程為:dp[i][0]:它可以由0到i-1中滿足權值<=x_i的dp0_j轉移到.
dp[i][1]:它可以由0到i-1中滿足權值<=x_i的dp1_j以及0到i-L-1中滿足權值<=x_i的dp0_j轉移到
由於資料範圍是10^5,需要離散化一下數字,用樹狀陣列維護。處理很巧妙,詳見程式碼
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=100100;
int a[maxn],num[maxn],n,m,L;
int dp[maxn][2],A[maxn],B[maxn];
bool cmp(int x,int y)
{
return a[x]<a[y];
}
void add(int *A,int k1,int k2)
{
for(; k1<=m; k1+=(k1&(-k1)))A[k1]=max(A[k1],k2);
}
int find(int *A,int k1)
{
int ans=-1e9;
for(; k1; k1-=(k1&(-k1)))ans=max(ans,A[k1]);
return ans;
}
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&L);
for(int i=1; i<=n; i++)scanf("%d",&a[i]);
a[0]=-2e9;
for(int i=0; i<=n; i++)num[i]=i;
sort(num,num+n+1,cmp);
m=1;
int pre=a[num[0]];
a[num[0]]=m;
for(int i=1; i<=n; i++)
{
if(a[num[i]]!=pre)m++,pre=a[num[i]];
a[num[i]]=m;
}
// for(int i=0;i<=n;i++) printf("%d ",a[i]);
// printf("\n");
for(int i=1; i<=m; i++)A[i]=0,B[i]=-1e9;
if(L==0)for(int i=1; i<=m; i++)B[i]=0;
for(int i=1; i<=n; i++)
{
dp[i][0]=find(A,a[i]-1)+1;
dp[i][1]=find(B,a[i]-1)+1;
add(A,a[i],dp[i][0]);
add(B,a[i],dp[i][1]);
if(i>=L) add(B,a[i-L],max(dp[i-L][0],dp[i-L][1]));
}
int ans=max(0,dp[n-L][0]);
for(int i=1; i<=n; i++)ans=max(ans,dp[i][1]);
printf("Case #%d: %d\n",++tt,ans);
}
return 0;
}
相關文章
- HDU4991 Ordered Subsequence (dp+樹狀陣列+離散化)陣列
- 【dp+離散化+線段樹優化】Paint優化AI
- hdu4325 樹狀陣列+離散化陣列
- HDU 5862 Counting Intersections(樹狀陣列+掃描線+離散化)陣列
- LeetCode 493. 翻轉對(歸併排序 || 離散化+樹狀陣列)LeetCode排序陣列
- Leetcode 327. 區間和的個數 (字首和 + 離散化 + 樹狀陣列)LeetCode陣列
- HDU 4427 Math Magic【dp+優化+滾動陣列】【好題】優化陣列
- 離散化
- 【離散優化】覆蓋問題優化
- Codeforces 909C Python Indentation:樹狀陣列優化dpPython陣列優化
- hdu 4368 樹狀陣列 離線維護陣列
- 樹狀陣列陣列
- SPOJ DQUERY (離線數狀陣列||線上主席樹)陣列
- 變化的區間樹狀陣列,單點查詢陣列
- 解析樹狀陣列陣列
- 運籌優化(十四)--離散優化的啟發式演算法優化演算法
- ICPC2018徐州網路賽 H.Ryuji doesn't want to study ( 樹狀陣列陣列
- 樹狀陣列詳解陣列
- 樹狀陣列基礎陣列
- poj 2481 樹狀陣列陣列
- hdu 3874 樹狀陣列陣列
- 二維樹狀陣列陣列
- NEERC 2015 Southern Subregional G Hiring 樹狀陣列陣列
- 樹狀陣列模板題 & (樹狀陣列 1:單點修改,區間查詢)陣列
- 樹狀陣列和逆序對陣列
- hdu 5147 樹狀陣列陣列
- 樹狀陣列快速入門陣列
- POJ 2528 Mayor's posters (線段樹 區間更新+離散化)
- POJ 2582 Mayor's posters 線段樹入門題+離散化
- POJ 2528 Mayor's posters (線段樹區間更新 + 離散化)
- 連續特徵離散化和歸一化特徵
- 樹狀陣列模板+習題集陣列
- 樹狀陣列3種基本操作陣列
- 學習筆記----樹狀陣列筆記陣列
- 樹狀陣列upc1976陣列
- CSU 4441 Necklace (樹狀陣列/LIS)陣列
- 樹狀陣列(我是真小白)陣列
- 資料結構——樹狀陣列資料結構陣列