「BZOJ2708」[Violet 1] 木偶
Description
Input
Output
Sample Input
1
2
1
54
2
8 9
3
1 2 3
2
56 60
3
59 59 57
3
51 55 59
5
1 2 3 2 4
4
87 70 81 34
4
50 55 58 59
6
1 2 3 4 5 6
6
1 2 3 3 4 5
8
1 2 3 3 4 2 5 4
9
22 23 52 61 39 38 1 40 17
2
1
54
2
8 9
3
1 2 3
2
56 60
3
59 59 57
3
51 55 59
5
1 2 3 2 4
4
87 70 81 34
4
50 55 58 59
6
1 2 3 4 5 6
6
1 2 3 3 4 5
8
1 2 3 3 4 2 5 4
9
22 23 52 61 39 38 1 40 17
Sample Output
0
0
0
1
0
0
0
1
0
0
2
2
2
1
0
0
1
0
0
0
1
0
0
2
2
2
1
HINT
Source
f[i]=f[j]+cal(j+1,i)
cal(x,y)计算x-y互相匹配最多可扔掉几个
枚举可以扔掉的数量k,判断剩下的能否相互匹配,不能返回k-1
以及被扔掉的能否相互匹配,能匹配返回k-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<set> #include<ctime> #include<queue> #include<vector> #include<cmath> #include<algorithm> #include<map> #define pa pair<int,int> #define inf 1000000000 #define ll long long using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n; int a[55],f[55]; int cal(int x,int y) { for(int k=1;k<=y-x+1;k++) { for(int i=k;i<=y-x;i++) if(abs(a[i+x]-a[i+x-k])>1)return k-1; if(abs(a[x+k-1]-a[y-k+1])<=1)return k-1; } return y-x+1; } void dp() { memset(f,0,sizeof(f)); for(int i=1;i<=n;i++) for(int j=0;j<i;j++) f[i]=max(f[i],f[j]+cal(j+1,i)); } int main() { while(scanf("%d",&n)!=EOF) { for(int i=1;i<=n;i++)a[i]=read(); cal(1,n); sort(a+1,a+n+1); dp(); printf("%d\n",f[n]); } return 0; } |
Subscribe