一道算法题,求大侠解答...

deiniyiqiannian 2012-08-10 05:24:50
大概是说: 给你一个无序的非空数组A, 其中每个数都不一样. 在A中挑出一些数,这些数的下标组成新的数组B, 这些数并不要求按在A中的相对顺序, 同时满足:
1. A[B[0]] > A[B[1]] > ... > A[B[K]]
2. 设B中任意两个相邻的数为B[i], B[i+1], 则在A中要满足A[m]<A[B[i+1]], 其中min(B[i], B[i+1]) + 1<m<max(B[i], B[i+1]) - 1. 就是说A中下标在B[i], B[i+1]之间的所有数都要小于A[B[i]]与A[B[i+1]].
求B数组的最长长度.
可能我说得不够清楚,大家可以看原题.如下:

A non-empty zero-indexed array A containing N different integers is given. We are looking for the longest possible sequence built from elements of A, A[B[0]], A[B[1]], ..., A[B[K]], satisfying the following conditions:

1.The sequence must be decreasing; that is, A[B[0]] > A[B[1]] > ... > A[B[K]].
2.For any two consecutive elements of the sequence, A[B[I]] and A[B[I+1]], all the elements of A between them must be smaller than them; that is, for any J = MIN(B[I], B[I+1]) + 1, ..., MAX(B[I], B[I+1]) - 1, we have A[J] < A[B[I+1]].

Write a function:

int sequence(int A[], int N);

that, given a zero-indexed array A containing N different integers, computes the maximum length of a sequence satisfying the above conditions.

For example, for the following array A:

A[0] = 9 A[1] = 10 A[2] = 2
A[3] = -1 A[4] = 3 A[5] = -5
A[6] = 0 A[7] = -3 A[8] = 1
A[9] = 12 A[10] = 5 A[11] = 8
A[12] = -2 A[13] = 6 A[14] = 4

the function should return 6.
A sequence of length 6 satisfying the given conditions can be as follows:

A[9] = 12 A[1] = 10 A[4] = 3
A[8] = 1 A[6] = 0 A[7] = -3

Assume that:

the elements of A are all distinct;
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [-1,000,000,000..1,000,000,000].

Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

没思路,求解答....
...全文
292 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
deiniyiqiannian 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

鄙人数学不过关。。所以只好用最笨的方法了。。。遍历 - -!
一下是代码,还有,我认为这答案给的是错的。应该是5个,你看那答案里,有个A[6]和A[7],我表示这不符合要求,A[6]和A[7]之间没有数字,如果这样也算的话,那最后答案肯定是数组a的长度。所以,这个a数组最后结果应该是5
以下是我的代码。



#include<iostream>
using namespace s……
[/Quote]
这个...A[6]A[7]之前没有数字也是可以的.最后结果也不肯定就是A的长度...你可能理解错了.
光与影的平衡 2012-08-11
  • 打赏
  • 举报
回复
中间有段代码:

if(quzhi==9696&&Blength==6)
{
cout<<quzhi;
}

是我用来测试为什么我输出的是5,在这个给帖子里的出的答案这设断点用的。楼主直接忽略。
光与影的平衡 2012-08-11
  • 打赏
  • 举报
回复
鄙人数学不过关。。所以只好用最笨的方法了。。。遍历 - -!
一下是代码,还有,我认为这答案给的是错的。应该是5个,你看那答案里,有个A[6]和A[7],我表示这不符合要求,A[6]和A[7]之间没有数字,如果这样也算的话,那最后答案肯定是数组a的长度。所以,这个a数组最后结果应该是5
以下是我的代码。



#include<iostream>
using namespace std;
#define ALENGTH 15


int min(int a,int b)
{
if(a<b){
return a;
}
return b;
}


int max(int a,int b)
{
if(a<b){
return b;
}
return a;
}

int main()
{
//获取数组a;
int a[ALENGTH]={9,10,2,-1,3,-5,0,-3, 1,12 , 5 , 8,-2 , 6 , 4};
int quzhi;
// for(int i=0;i<ALENGTH;i++)
// {
// cin>>a[i];
// }
//记录最大长度
int MaxBlength=0;

//数组b;
int Blength;
for(Blength=2;Blength<=ALENGTH;Blength++)
{
//65535即2^15-1;获取在该长度下所有的数组b;
for(quzhi=0;quzhi<65535;quzhi++)
{
int *b=new int [Blength];

int cquzhi=quzhi,len=0;

if(quzhi==9696&&Blength==6)
{
cout<<quzhi;
}

for(int count=0;count<ALENGTH;count++)
{

if(cquzhi%2==1)
{

b[len]=15-count-1;//获取该位的A到数组B
len++;
}
if(len>=Blength)
{
break;
}
cquzhi=cquzhi>>1;
}

if(len>Blength||len<Blength)
{
delete []b;
continue;
}

//对数组b进行排序;
for(int ca=0;ca<Blength-1;ca++)
{
for(int cb=0;cb<Blength-1;cb++)
{
if(a[b[cb]]<a[b[cb+1]])
{
int t=b[cb];
b[cb]=b[cb+1];
b[cb+1]=t;
}
}
}

//对数组b相邻项中间a进行判断;

int flag=1; //用来标记数组b是否符合条件2



for(int indexb=0;indexb<Blength-1;indexb++)
{

if( (b[indexb]-b[indexb+1])==1 //去除掉相邻的,相邻的满足那最后肯定是15了。。所以去掉。
|| (b[indexb]-b[indexb+1])==-1 )
{
//不符合条件2 flag置0;忽略接下来的判断
flag=0;
continue;
}

for(int indexa=min(b[indexb],b[indexb+1]);
indexa<max(b[indexb],b[indexb+1])-1;
indexa++)
{



if(a[indexa+1]>a[b[indexb+1]]) //这里a数组中每个数都不一样,不用写>= ,省去了很多麻烦。
{
//不符合条件2 flag置0;忽略接下来的判断
flag=0;
break;
}
}
if(flag==0)
{
break;
}
}

//不符合条件2,放弃本次数组b,进行下一组b选取;
if(flag==0)
{
delete []b;
continue;
}
//符合条件2;则记录下次长度,并且放弃以下同一长度的判断。跳转至下一长度;
else
{
delete []b;
MaxBlength=Blength;
break;
}

}
}

cout<<MaxBlength<<endl;
return 0;
}
Gonefar 2012-08-11
  • 打赏
  • 举报
回复
首先B[0]、B[1]肯定是符合条件的。进一步分析知道,如果得出B[i],那么B[i+1]和B[i+2]在A中的序号必定是同时大于或同时小于B[i]在A中的序号。然后可以得出很多序列,取最长的。。好像挺复杂的,LZ再仔细想想吧。
deiniyiqiannian 2012-08-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

首先对序列进行降序排列得到B[n],则B[0],B[1]必定符合条件,选出;然后从剩下的数里选,按照A数组顺序,选取B[0]~B[1]序号之外的最大数为B[2];那么然后选B[3]时只需考虑B[1],B[2],如同前面最先取出的B[0],B[1]一样考虑。
[/Quote]
"按A数组的顺序"?
如果8 5 3 6 12,排序后是12 8 6 5 3,在选B[2]时,是选5?

题中要求O(N)啊....
Gonefar 2012-08-10
  • 打赏
  • 举报
回复
首先对序列进行降序排列得到B[n],则B[0],B[1]必定符合条件,选出;然后从剩下的数里选,按照A数组顺序,选取B[0]~B[1]序号之外的最大数为B[2];那么然后选B[3]时只需考虑B[1],B[2],如同前面最先取出的B[0],B[1]一样考虑。

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧