Tag: acm

  • 如何避免由于编译器差别带来的错误

    转自http://acm.cist.bnu.edu.cn
    1、判题系统使用的是G++编译器,和普通使用的TC,VC都有所不同,建议大家使用Dev C++作为IDE,或者用TC和VC写代码,提交前使用Dev C++编译,预防编译错误。
    提交C语言代码最好使用G++,G++兼容C和C++。C的代码可以用GCC也可用G++提交,而C++的代码不能够用GCC提交,只能用G++。
    (more…)

  • poj_1009(TLE&MLE)

    /*
    //ACM Poj 1009
    //File: 1009.cpp
    //Author: Kangzj
    //Mail: Kangzj@mail.bnu.edu.cn
    //Date: April 29, 2009
    */
    #include
    #include
    #include
    #include
    using namespace std;
    struct node
    {
      int pix;
      int count;
    };
    int main()
    {
      int width,i;
      int pix;
      long len;
      ostringstream buff;
        while(cin>>width&&width!=0)
      {
        //读入图像并解码
        vectorimgin;
        while(cin>>pix>>len&&len!=0)
        {
          vectortemp(len,pix);
          imgin.insert(imgin.end(),temp.begin(),temp.end());
        }
        int height;
        int size=imgin.size();
        height=size/width;
        vectorimgout(size,0);
        for(i=0;i=start)
            {
              max=abs(imgin[pos-1]-imgin[pos]);
            }
            if((pos+1)m?max:m;
            }
            if(pos+widthm?max:m;
              if(pos+width+1m?max:m;
              }
              if(pos+width-1>=start+width)
              {
                int m=abs(imgin[pos+width-1]-imgin[pos]);
                max=max>m?max:m;
              }
            }
            if(pos-width>=0)
            {
              int m=abs(imgin[pos-width]-imgin[pos]);
              max=max>m?max:m;
              if(pos-width+1< =start-1)
              {
                int m=abs(imgin[pos-width]-imgin[pos]);
                max=max>m?max:m;
              }
              if(pos-width-1>=start-width)
              {
                int m=abs(imgin[pos-width-1]-imgin[pos]);
                max=max>m?max:m;
              }
            }
            imgout[pos]=max;
          }
        }
        //将输出图像编码
        buff< rle;
        //第一个先放进去
        node tn;
        tn.pix=imgout[0];
        tn.count=0;
        rle.push_back(tn);
        //游程编码
        for(i=0;i
    

    虽然AC了,但其实是不合格的,时间和空间都超了,500000000的数据量没法处理,好在测试集里没有这样的数据。。。转了一篇别人做的,质量真的不错,想到了好多我根本就没去想的问题,受教了。
    代码完全是按题意做的,一点也没有改进:

  • poj_1008

    /*
    //ACM Poj 1008
    //File: 1008.cpp
    //Author: Kangzj
    //Mail: Kangzj@mail.bnu.edu.cn
    //Date: April 29, 2009
    */
    #include
    #include
    using namespace std;
    struct TYD
    {
    	int Dnum;
    	string Dname;
    };
    int main()
    {
        string TkDays[20]={"imix","ik","akbal","kan","chicchan","cimi","manik","lamat","muluk","ok","chuen","eb","ben","ix","mem","cib","caban","eznab","canac","ahau"};
    	string HbMons[19]={"pop","no","zip","zotz","tzec","xul","yoxkin","mol","chen","yax","zac","ceh","mac","kankin","muan","pax","koyab","cumhu"};
    	ostringstream buff;
    	TYD Tday;
    	int i,j,n;
    	int Hyear,Hday, Tyear;
    	string Hdaystr, Hmonth;
    	int days,Tdays;
    	cin>>n;
    	buff< >Hdaystr>>Hmonth>>Hyear;
    		Hdaystr=Hdaystr.substr(0,Hdaystr.size()-1);
    		if (Hdaystr.size()==2)
    		{
    			Hday=(Hdaystr[0]-'0')*10+(Hdaystr[1]-'0');
    		}else
    		{
    			Hday=(Hdaystr[0]-'0');
    		}
    		if(Hmonth=="uayet")
    		{
    			j=18;
    		}
    		else
    		{
    			for(j=0;j<18;j++)
    			{
    				if(Hmonth==HbMons[j])
    				{
    					break;
    				}
    			}
    		}
    		days=Hyear*365+j*20+Hday;
    		Tyear=days/260;
    		Tdays=days%260;//注意边界!
    		Tday.Dname=TkDays[Tdays%20];
    		Tday.Dnum=Tdays%13+1;
    		buff< 
    

  • poj_1007

     

    /*
    //ACM Poj 1007
    //File: 1007.cpp
    //Author: Kangzj
    //Mail: Kangzj@mail.bnu.edu.cn
    //Date: April 28, 2009
    */
    #include
    #include
    using namespace std;
    struct revNum
    {
      int rvNums;
      int order;
    };
    int getReverseNum(string DNA, int n)
    {
      int *count=new int[n];
      int all=0,i;
      for (i=0;i=0;i--)
      {
        for(int j=0;j>n>>m;
      string *DNA=new string[m];
      int count=0,temp,i;
      revNum *rvNum=new revNum[m];
      for(i=0;i>DNA[i];
        temp=getReverseNum(DNA[i],n);
        //插入排序
        if(count==0)
        {
          rvNum[0].rvNums=temp;
          rvNum[0].order=i;
          count++;
        }
        else
        {
                int j,flag=0;
          for(j=0;jj;k--)
              {
                rvNum[k]=rvNum[k-1];
              }
              rvNum[j].rvNums=temp;
              rvNum[j].order=i;
              count++;
              flag=1;
              break;
            }
          }//for(int j=0;j
    

  • poj_1005

    /*
    //ACM Poj 1005
    //File: 1005.cpp
    //Author: Kangzj
    //Mail: Kangzj@mail.bnu.edu.cn
    //Date: April 28, 2009
    */
    #include
    #include
    #define PI 3.14159265358979
    using namespace std;
    int main()
    {
      double distance;
      int n;
      cin>>n;
      double *x=new double[n];
      double *y=new double[n];
      int *year=new int[n];
      for(int i=0;i>x[i]>>y[i];
        distance=sqrt(x[i]*x[i]+y[i]*y[i]);
        int j=1;
        double r=0;
        while(1)
        {
          r=sqrt(100/PI+r*r);
          if(distance< =r)break;
          j++;
        }
        year[i]=j;
      }
      for(int i=0;i
    

  • poj_2000

    /*
    //ACM Poj 2000
    //File: 2000.cpp
    //Author: Kangzj
    //Mail: Kangzj@mail.bnu.edu.cn
    //Date: April 24, 2009
    */
    #include
    #include
    #include
    using namespace std;
    int main()
    {
      int day;
      int coinDay, coinAll;
      ostringstream buff;
        while(1)
      {
            cin>>day;
            if(day==0)break;
        float temp=sqrt(1+8*day);
        int tmp=(int)temp;
        if(temp!=(float)tmp && tmp%2!=0)
        {
          tmp+=2;
        }else if(temp!=(float)tmp && tmp%2==0)
        {
          tmp++;
        }else
        {
          tmp=tmp;
        }
        coinDay=(-1+tmp)/2;
        coinDay--;
        coinAll=coinDay*(coinDay+1)*(2*coinDay+1)/6;
        coinAll+=(coinDay+1)*(day-(coinDay+1)*coinDay/2);
        buff< 
    

    需要选择G++才能通过,如果想用C++,只要加几个强制转换就可以了。

  • zoj_1002(Fire Net)的一个简单的解法

    做了一天,把此题AC了,看到网上用的算法很复杂,发个自己的出来。
    思路:
    1.如果城市全空,可以不管顺序,随便从哪里开始放“碉堡”,把“碉堡”射程里的城市标记下,后面的“碉堡”不放在这里即可得到结果。
    2.但是如果城市中有“墙”的话,就不可以按照1的方法了,因为放置数量的多少与顺序是有关的,所以,怎样来确定这个顺序是很重要的。观察发现,城市里的空地按照领域中的“墙”的数量可以分为五种,即从0到4。再观察,可以得到这样一个结论:按邻域中墙的数目由多到少,往空地上安放“碉堡”,可以使数目最大!
    于是按照这个结论,编程如下:
    (more…)

  • poj_1006

    Problem: http://poj.grids.cn/problem?id=1006
    Solution:
     

    #include
    #include
    #include
    using namespace std;
    int main()
    {
        int p, e, i, n=0;
        long cp, ce, ci;
        short d;
        int j=1;//计数用
        ostringstream buf;
        while(1)
        {
            cin>>p>>e>>i>>d;
            if(p==-1 && e==-1 && i==-1 && d==-1)
            {
                break;
            }
            n=d;
            while(1)
            {
                n++;
                if((n-p)%23==0 && (n-e)%28==0 && (n-i)%33==0)
                {
                    n-=d;
                    if(n==0)
                    {
                        n=d;
                        continue;
                    }
                    break;
                }
                //cout< <"*";
            }
            buf<<"Case "<
    

  • poj_1004

    #include
    using namespace std;
    int main()
    {
        int i;
        float all=0, temp=0;
        for(i=0;i<12;i++)
        {
            cin>>temp;
            all+=temp;
        }
        all+=0.005;
        cout.precision(2);
        //cout.setf(ios_base::showpoint);
        cout< <"$"<