标签:hide include eve view open some second 题意 int
Strip
Alexandra has a paper strip with n numbers on it. Let‘s call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
Please help Alexandra to find the minimal number of pieces meeting the condition above.
Input
The first line contains three space-separated integers n, s, l (1 ≤ n ≤ 105, 0 ≤ s ≤ 109, 1 ≤ l ≤ 105).
The second line contains n integers ai separated by spaces ( - 109 ≤ ai ≤ 109).
Output
Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1.
Examples
7 2 2
1 3 1 2 4 1 2
3
7 2 2
1 100 1 100 1 100 1
-1
Note
For the first sample, we can split the strip into 3 pieces: [1, 3, 1], [2, 4], [1, 2].
For the second sample, we can‘t let 1 and 100 be on the same piece, so no solution exists.
题意:把一组长度为N数列分为连续的若干个数列,且满足:1,每个子数列的长度>=L,最大值减最小值<=S,求最小的子数列数目,若无法拆分,输出-1。
dp+ST;
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<cmath> #include<vector> using namespace std; int S,N,L; const int CON=1e5+5; int maxn[CON][20],minn[CON][20]; int a[CON]; int dp[CON];//dp[i]表示包括i在内的最小情况. int query(int x,int y)//查询[x,y]间的最值 { int k=log2(y-x+1); return max(maxn[x][k],maxn[y-(1<<k)+1][k])-min(minn[x][k],minn[y-(1<<k)+1][k]); } int main() { while(scanf("%d%d%d",&N,&S,&L)!=EOF) { for(int i=1;i<=N;i++) { scanf("%d",&a[i]); maxn[i][0]=a[i]; minn[i][0]=a[i]; } memset(dp,0x3f3f3f3f,sizeof dp); int shang=log2(N); for(int j=1;j<=shang;j++) { for(int i=1;i+(1<<j)-1<=N;i++) { maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]); minn[i][j]=min(minn[i][j-1],minn[i+(1<<(j-1))][j-1]); } } int pre=0; dp[0]=0; for(int i=1;i<=N;i++) { while(i-pre>=L&&query(pre+1,i)>S||dp[pre]==0x3f3f3f3f)//(i-pre>=L) => [pre+1,i]=L; //长度满足但最值相差大,则pre++;pre前无值,pre++; pre++; if(i-pre>=L) dp[i]=min(dp[pre]+1,dp[i]); } if(dp[N]==0x3f3f3f3f) printf("-1\n"); else printf("%d\n",dp[N]); } return 0; }
标签:hide include eve view open some second 题意 int
原文地址:https://www.cnblogs.com/switch-waht/p/11396798.html