1037: I want you!
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 179 Solved: 29
[ 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
【解析】
这道题的话其实就是叫你算最长子序列得到长度是多少,但是0页要算入里面b这个数组里面只有0或者1,其实就是这样的我们就考虑b为1的情况来算就行了,因为他要a[i]*b[i]<a[j]*b[j] (i<j)其实我们这里可以不用说真的给b数组只有1和0我们完全可以用b数组来存放长度,因为只有保证了b为1才能比较,如果a[i]为0则f[i]为0,其实就是找b数组求它的长度是多少的问题。如果a[i]为0那么f[i]也为0,这样的话其实它也是小于后面的。
#include <iostream>
#include <cstdio>
#include<vector>
using namespace std;
int main()
{
int n,i,j,count1=0,sum,num;
while(~scanf("%d",&n))
{
sum=0;
count1=0;
int a[1000]={0};
int b[1000]={0};
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
{
count1++;
b[i]=0;//如果a[i]为0则给b[i]也设置为0
}
for(j=0;j<i;j++)
{
if(a[j]!=0&&a[j]<a[i]&&b[j]>b[i])
b[i]=b[j];//判断下标比i小的元素小于i的如果此刻上升序列把b[j]序列的长度给a[i]之后再继续,
//这里起到一个动态给的过程
}
if(a[i]!=0)
b[i]++;
if(b[i]>sum)
sum=b[i];
}
printf("%d\n",sum+count1);
}
return 0;
}