pku上3378--crazy thairs老说我Output Limit Exceeded ,帮帮忙

GAWING2007 2008-05-29 01:44:36
#include"stdio.h"
#include"stdlib.h"
#define M 50000
int trace(long a[],int x[],int t,int n,long double &count)
{
int i,j,flag=0,z=0,tf=0,c=0,s=0,tmp[5]={0};
if(t>n)
{
//printf("%d ",x[0]);
if(x[0])
{
tmp[c++]=a[0];//printf("tmp:%d ",tmp[0]);
}
for(i=1;i<=n;i++)
{ //printf("%d ",x[i]);
if(c<=5&&x[i])
{
if(a[i]>tmp[c-1]&&a[i]<=n+1)
{
tmp[c++]=a[i];
// printf("tmp:%d c:%d ",tmp[c-1],c);
}//if
}//if
if(c==5&&x[i+1])
{
//printf("\n");
return 0;
}
}//for
if(c==5)
{
count++;
//printf("---------%d ",count);
}//printf("\n");
}//if
else
for(i=0;i<=1;i++)
{
x[t]=1-i;
if(!x[t]&&t!=0)
{
for(j=t-1;j>=0;j--)
if(x[j]==0) z++;
}
if(n-z<4) return 0;
if(x[t]&&a[t]<=n+1&&t!=0)
{
for(j=t-1;j>=0;j--)
{
if(x[j])
{
flag=1;
if(a[t]>a[j]){//printf("* ");
trace(a,x,t+1,n,count);
break;}
}
}//for
if(!flag) trace(a,x,t+1,n,count);
}
else if(!x[t]||t==0) {
//printf("** ");
trace(a,x,t+1,n,count);}
}
return 0;
}
int main()
{
int c,n;
scanf("%d",&n);
while(n)
{
long double count=0;
if(n<5) {printf("%d\n",count);continue;}
long a[M]={0};
int i,j,x[M+1]={0},temp=0;
//g[M]={0};
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
trace(a,x,0,n-1,count);
printf("%.0f\n",count);
scanf("%d",&n);
//getchar();while (scanf("%d",&n )!= EOF )
// n=getchar();
}
return 0;
}
...全文
326 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
tailzhou 2008-05-31
  • 打赏
  • 举报
回复
当0<n<5 不是死循环?

GAWING2007 2008-05-31
  • 打赏
  • 举报
回复
你看清楚代码没有???
哪里的死循环啊?
请注意!!
每一输出后,会有下一次n的输入,我的原意系当n输入为0时,结束循环~~
tailzhou 2008-05-29
  • 打赏
  • 举报
回复
可以用 while(scanf("%d",&n)==1) 来判断是否输入结束

注意到n个数都不超过109
原题是:n个数都不超过10^9

lz的代码里:
while(n)
{
long double count=0;
if(n <5) {printf("%d\n",count);continue;}

不是死循环?

GAWING2007 2008-05-29
  • 打赏
  • 举报
回复
我想知道那题的输出输入怎么是怎么样的,输入以n=0结束吗????
  • 打赏
  • 举报
回复
上面是网上搜到的,用类似“逆序对”的分治做法来实现的。

实际上,注意到n个数都不超过109。如果再没有负数出现,hash一下用动态规划也是接受的
  • 打赏
  • 举报
回复
Description
These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ≤ N ≤ 50000) numbers, which are no more than 109, Crazy Thair is a group of 5 numbers {i, j, k, l, m} satisfying:

1. 1 ≤ i < j < k < l < m ≤ N
2. Ai < Aj < Ak < Al < Am

For example, in the sequence {2, 1, 3, 4, 5, 7, 6},there are four Crazy Thair groups: {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 3, 4, 5, 7} and {2, 3, 4, 5, 7}.

Could you help Sempr to count how many Crazy Thairs in the sequence?


Input
Input contains several test cases. Each test case begins with a line containing a number N, followed by a line containing N numbers.

Output
Output the amount of Crazy Thairs in each sequence.


Sample Input
5
1 2 3 4 5
7
2 1 3 4 5 7 6
7
1 2 3 4 5 6 7

Sample Output
1
4
21

analyze
It is known that we can implement an extended merge sort to evaluate the number of pairs of integer {i, j} (or the number of 2-number groups) satisfying i < j and Ai < Aj. The general idea of this problem is almost the same, which evaluates the number of 3-number groups, 4-number groups and finally 5-number groups step-by-step. code://baowp
/*POJ Monthly--2007.09.09
我们知道,在一次归并排序中可以得到形如i<j,Ai<Aj的总对数,
或者是i<j,Ai>Aj(逆序数),记录每一个位置长度为2的总数,
再进行一次归并,就可以得到每个位置长度为3的总数(由上一次归并
排序求得),依此类推,进行到第四次归并排序时就可以求出长度为5
的总数。
*/
#include <stdio.h>
#define N 50005
typedef struct
{
int x;
int loc;
}Node;
Node a[N];
Node m[N];
__int64 addf[N];
__int64 addc[N];
int t;
void MergeSort(int l,int r)
{
__int64 sum = 0;

Node *temp = new Node[r-l+6];
if( l == r ) return;

int mid = (l+r)/2;
MergeSort(l,mid);
MergeSort(mid+1,r);

int i,j,k;
i = l , j = mid+1;
k = 0;

while( i <= mid && j <= r )
{
if( m[i].x < m[j].x )
{
temp[k++] = m[i++];
sum += addf[temp[k-1].loc];
}
else
{
temp[k++] = m[j++];
addc[temp[k-1].loc] += sum;
}
}

while( i <= mid )
temp[k++] = m[i++];

while( j <= r )
{
temp[k++] = m[j++];
addc[temp[k-1].loc] += sum;
}

k = 0;
for( i = l ; i <= r ; i++ )
m[i] = temp[k++];
delete temp;
}

int main()
{
int i,j,n;
while( scanf( "%d" , &n ) != EOF )
{
for( i = 1 ; i <= n ; i++ )
{
scanf( "%d" , &a[i].x );
a[i].loc = i;
}
if( n < 5 )
{
printf( "0\n" );
continue;
}


for( i = 1 ; i <= n ; i++ )
addc[i] = 1;


int t;
for( t = 1 ; t < 5 ; t++ )
{
for( j = 1 ; j <= n ; j++ )
{
m[j] = a[j];
addf[j] = addc[j];
addc[j] = 0;
}


MergeSort(1,n);
}
__int64 ans1 = 0,ans2 = 0;

for( i = 1 ; i <= n ; i++ )
{
ans1 += (ans2+addc[i])/10000000000000000LL;
ans2 = (ans2+addc[i])%10000000000000000LL;
}
if(ans1)
{
printf( "%I64d" , ans1 );
printf( "%016I64d\n" , ans2 );
}
else
printf( "%I64d\n", ans2 );

}
return 0;
}

33,009

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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