Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

又是GCC提交wa,C提交ac,希望大牛帮忙看看,代码哪里有问题

Posted by Matrixking at 2013-01-19 15:11:14 on Problem 1018
代码在http://hi.baidu.com/994026084/item/1fb2a7d1d9c9e254d73aae74
我感觉vc比gcc对越界访问容错性强,gcc的全局变量可能初始值不为零。但越界没检查出来,全局变量试过也不行,郁闷~~
#include<stdio.h>
#include<stdlib.h>
#define MAXN 100
#define MAXM 100
#define INF 2147483647
typedef struct
{
 int b;
 int p;
}type;
type pair[MAXN+1][MAXM+1];
int m[MAXN+1],B,P,n;
int b[(MAXN*MAXM)+1];
double ans;
int cmptype(const void * a, const void * b)
{
 return(*(type *)a).p-(*(type *)b).p;
}
int cmpint(const void * a, const void * b)
{
 return(*(int *)a)-(*(int *)b);
}
int Bsearch(int l,int r,type a[],int b)
{
 int mid;
 while(l<=r)
 {
  mid=(r+l)/2;
  if(b<a[mid].b)
   r=mid-1;
  else if(b>a[mid].b)
   l=mid+1;
  else return mid;
 }
 return l;
}
void dfs(int deep)
{
 int i;
 if(deep>=n)
 {
  //printf("%d %lf %d\n",deep,(double)B/P,P);
  if(ans<(double)B/P)
   ans=(double)B/P;
 }
 else
 {
     //for(i=1;i<=m[deep+1]&&pair[deep+1][i].b<B;i++);
  i=Bsearch(1,m[deep+1],pair[deep+1],B);
  if(P==0||ans<(double)B/P)
  {
      //printf("(%d,%d)",i,pair[deep+1][i].p);
   P+=pair[deep+1][i].p;
        dfs(deep+1);
     P-=pair[deep+1][i].p;
  }
 }
}
int main()
{
 int i,j,k,t,c,min,max;
 //freopen("in.txt","r",stdin);
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d",&n);
     for(i=1,min=INF,c=1;i<=n;i++)
  {
      scanf("%d",m+i);
         for(j=1;j<=m[i];j++)
    scanf("%d%d",&(pair[i][j].b),&(pair[i][j].p));
   qsort(pair[i]+1,m[i],sizeof(pair[i][0]),cmptype);
   for(j=2,k=2;j<=m[i];j++)
   {
    if(pair[i][j].b>pair[i][k-1].b)
    {
        pair[i][k]=pair[i][j];
     k++;
    }
   }//取重
   m[i]=k-1;
   for(j=m[i]-1,k=m[i]-1;j>=1;j--)
   {
    if(pair[i][j].p!=pair[i][j+1].p)
    {
     pair[i][k]=pair[i][j];
     k--;
    }
   }
   m[i]-=k;
   for(j=1;j<=m[i];j++)
    pair[i][j]=pair[i][k+j];//取重
   for(j=1,max=0;j<=m[i];j++)
   {
    //printf("(%d %d) ",pair[i][j].b,pair[i][j].p);
    b[c]=pair[i][j].b;
    if(max<pair[i][j].b)
     max=pair[i][j].b;
    c++;
   }
   if(min>max)
    min=max;
   //printf("\n");
  }
  qsort(b+1,c-1,sizeof(b[0]),cmpint);
  //printf("%d b:",c-1);
  //for(i=1;i<c;i++)printf("%d ",b[i]);
  //printf("\nmin:%d\n",min);
  ans=0;
  B=b[1];
  //printf("%d\n",P);
  dfs(0);//对设备最小带宽小于B进行搜索
  for(i=2;b[i]<=min;i++)
  {
   if(b[i]!=b[i-1])
   {
    B=b[i];
    dfs(0);//对设备最小带宽小于B进行搜索
   }
  }
   printf("%.3lf\n",ans);
 }
 return 0;
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator