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

Re:我什么wa了啊

Posted by zw7840 at 2010-02-19 21:32:43 on Problem 3378
In Reply To:我什么wa了啊 Posted by:zw7840 at 2010-02-19 21:32:19
給组数据也好
下面是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

long n,head[50]={0},totm=0;
long long answer[505]={0};

struct case1
{
 long long amount;
 long long sum_amount;
 long num;
 long use;
 long child[2];
}treap[500005]={{0,0,0,0,{0}}};

void set(long *p,long num,long long amount)
{
 treap[++totm].amount=treap[totm].sum_amount=amount;
 treap[totm].num=num;
 treap[totm].use=rand();
 treap[totm].child[0]=treap[totm].child[1]=0;
 
 *p=totm;
}

void turn(long *p,long flag)
{
 long now=*p,t;
 
 t=treap[now].child[flag];
 treap[now].child[flag]=treap[t].child[flag^1];
 treap[t].child[flag^1]=now;
 
 treap[now].sum_amount=treap[now].amount+treap[treap[now].child[0]].sum_amount+treap[treap[now].child[1]].sum_amount;
 treap[t].sum_amount=treap[t].amount+treap[now].sum_amount+treap[treap[t].child[flag]].sum_amount;
 
 *p=t;
}

long long insert(long *p,long num,long long amount)
{
 long now=*p;
 long long s=0;
 
 if(!now)
  {
   set(p,num,amount);
   return 0;
  }
 
 if(num==treap[now].num)
  {
   treap[now].amount+=amount;
   treap[now].sum_amount+=amount;
   return treap[treap[now].child[0]].sum_amount;
  }
 else if(num<treap[now].num)
  {
   s=insert(&treap[now].child[0],num,amount);
   treap[now].sum_amount+=amount;
   
   if(treap[treap[now].child[0]].use<treap[now].use)
     turn(p,0);
  }
 else
  {
   s=insert(&treap[now].child[1],num,amount)+treap[now].amount+treap[treap[now].child[0]].sum_amount;
   treap[now].sum_amount+=amount;
   
   if(treap[treap[now].child[1]].use<treap[now].use)
     turn(p,1);
  }
  
 return s;
}

void add(long long a[55],long long b)
{
 long i;
 
 a[0]+=b;
 
 for(i=0;i<=50;i++)
   {
    a[i+1]+=a[i]/10;
    a[i]%=10;
   }
}

void work()
{
 long number,i,j;
 long long last;
 
 totm=0;
 for(i=1;i<=4;i++)
   head[i]=0;
 for(i=0;i<=50;i++)
   answer[i]=0;
 
 for(i=1;i<=n;i++)
   {
    scanf("%ld",&number);
    for(j=1,last=1;j<=4;j++)
      last=insert(&head[j],number,last);
    add(answer,last);
   }
 
 for(i=50;!answer[i]&&i>0;i--);
 for(;i>=0;i--)
   printf("%I64d",answer[i]);
 printf("\n");
}

int main()
{
 for(srand((unsigned)time(NULL));scanf("%ld",&n)!=EOF;work());
 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