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

AC C++ code

Posted by wwwaaannngggrs at 2010-06-02 22:59:55 on Problem 1010
给不停WA/TLE的C++ er们一点参考
code很丑,见谅……

#include <iostream>
#include <algorithm>
using namespace std;

bool tie;
int n,m,t,i,a[100],b[100],x[100],used[100],tx[100],tmax[10];
int ans,ansnum,ansmax,ansrec[100];
int mmax,num,tnum,last;

bool same()
{
     if (num!=ansnum) return false;
     for (int iii=0;iii<num;iii++)
         if (ansrec[iii]!=x[iii]) return false;
     return true;
}
void dfs(int get,int kind)
{
     if (get==b[i])
     {      
        if ((kind>ans)||((kind==ans) &&(ansnum>num))||((kind==ans)&&(ansnum==num)&&(mmax>ansmax)))
        {
                     tie=false;
                     ans=kind;
                     ansnum=num;
                     ansmax=mmax;
                     for (int iii=0;iii<num;iii++) ansrec[iii]=x[iii];
                     return;
        };
        if ((kind==ans)&&(ansnum==num)&&(ansmax==mmax)&&(!same())) 
        {
        tie=true;
        return;
        }
        return;
     }
     if ((get>b[i])||(num>=4)) return;
     for (int ii=last;ii<n;ii++)
     {
         used[ii]++;
         tmax[num]=mmax;
         num++;
         last=ii;
         mmax=a[ii]>mmax?a[ii]:mmax;
         x[num-1]=ii;
         dfs(get+a[ii],kind+(used[ii]==1));
         mmax=tmax[num];
         num--;
         used[ii]--;
     }
}
            
void solve()
{
     for (i=0;i<m;i++)
     {
         
         for (int j=1;j<=4;j++) x[j]=0;
         for (int j=0;j<m;j++) used[j]=0;
         tie=false;
         mmax=0;
         num=0;
         ans=0;
         ansmax=0;
         ansnum=10000;
         last=0;
         dfs(0,0);
         if (!ans) {cout<<b[i]<<" ---- none"<<endl; continue;}
         cout<<b[i]<<" ("<<ans<<")"<<": ";
         if (tie) cout<<"tie"<<endl;
         else 
         {
              for (int ii=0;ii<ansnum-1;ii++) cout<<a[ansrec[ii]]<<" ";
              cout<<a[ansrec[ansnum-1]]<<endl;
              }
     }
}
int main()
{
    while (cin>>t)
    {
          n=1; a[0]=t; cin>>t;
          while (t) {n++; a[n-1]=t; cin>>t;}
          m=0; cin>>t;
          while (t) {m++; b[m-1]=t; cin>>t;}
          solve();         
    }
    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