1037: I want you!
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Web Board]
Description
Given an array Ai(0<=i<n), its length is n; We want to find an another array Bi , which is 0 or 1,and its length is n too;
Besides, Ai and Bi meets the following conditions:
If neither A[i]*B[i] nor A[j]*B[j] equals to 0, then A[i]*B[i] < A[j]*B[j];(0<=i<j<n)
Now , we want to know the maximum of ∑Bi(0<=i<n) you can get.
Input
The input consists of multiple test cases。For each test case ,the first line contains one integer n (1<n<=300).Next line contains n integers.
Output
For each case, output the maximum of ∑Bi.
Sample Input
6
1 2 3 4 5 6
4
3 2 3 6
Sample Output
6
3
【分析】
题意很简单,就是找另一个b数组使得当a[i]*b[i]!=0时a[i]*b[i]<a[j]*b[j](i<j)
b数组内只有0和1,可以发现,当b[i]为1时,a[i]*b[i]=a[i],b[i]为0时,a[i]*b[i]=0
也就是说,当b[i]=1时,保留a[i],否则的话去掉a[i],并且满足a[i]*b[i]<a[j]*b[j],不难发现,其实就是去求a数组中的一个最长严格上升序列。
但是这个时候会发现题目留了一个小坑,如果a[i]本身就是0呢?那么这一位b[i]=1的时候a[i]*b[i]还是0,所以对a[i]=0时需要特殊处理,a[i]=0时b[i]显然为1
所以结果就是上升序列长度+0的个数(这里的上升序列要把0除外)
【代码】
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
int f[10000];
int a[10000];
int main()
{
int n;
while (~scanf("%d",&n))
{
int ans=0;
int sum=0;
memset(f,0,sizeof(f));
for (int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if (a[i]==0) sum++;
f[i]=0;
for (int j=0;j<i;j++)
if (a[j]!=0 && a[i]>a[j] && f[j]>f[i])
f[i]=f[j];
if (a[i]) f[i]++;
if (f[i]>ans) ans=f[i];
}
printf("%d\n",ans+sum);
}
}