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

表示同样的算法c和c++时间差距好大!一个超时一个188ms

Posted by cowerss at 2013-05-10 15:43:46 on Problem 2092
c++用vector stl排序 c用数组加快排。一个跪了一个188ms....
//c++代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
class player
{
    public:
	player(){idx=cnt=0;}//idx索引数,cnt为出现个数
	unsigned short idx,cnt;
	bool operator >(const player &p) const
	{
	    return cnt>p.cnt;
	}
	bool operator <(const player &p) const
	{
	    return idx<p.idx;
	}
};
int main()
{
    int a,b,i,j;
    vector<player> v2(10001);
    for(i=0;i<10001;i++)v2[i].idx=i;
    vector<player> v1(10001);
    void *p1,*p2;
    while(1)
    {
	p1=&v1[0];
	p2=&v2[0];
	cin>>a>>b;
	if(a!=0&&b!=0)
	{
	    memcpy(p1,p2,sizeof(player)*v1.size());//清0
	    vector<player>::iterator it,yb;
	    int tmp;
	    for(i=0;i<a;i++)
		for(j=0;j<b;j++)
		{
		    cin>>tmp;
		    v1[tmp].cnt++;
		}
	    sort(v1.begin(),v1.end(),greater<player>());//快排
	    it=v1.begin()+1;
	    while(it->cnt==v1[1].cnt)
		it++;
	    sort(v1.begin()+1,it,less<player>());//对第二的按索引快排
	    yb=v1.begin()+1;
	    for(yb;yb!=it;yb++)
	    	cout<<yb->idx<<" ";
	    cout<<endl;
	}else
	    continue;
    }
}
//c代码
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
#define MAX 10001

typedef struct
{
    int num;
    int points;  //出现的次数
}Node;
Node node2[MAX];
Node node1[MAX];
int cmp(const void *a,const void *b)  //降序排列
{
    return ((Node*)b)->points-((Node*)a)->points;
}

int cmp1(const void *a,const void *b)
{
    return ((Node*)a)->num-((Node*)b)->num;
}

int main()
{
    int n,m,i,maxn,nums,index,nCount;
    for(i=0; i<MAX; i++)
    {
            node1[i].num=i;
            node1[i].points=0;
    }
    while( scanf("%d%d",&n,&m)!=EOF && n+m)
    {
        memcpy(&node2[0],&node1[0],sizeof(Node)*MAX);
        for(i=0; i<n*m; i++)
        {
                scanf("%d",&nums);   
                node2[nums].points++;
        }
         //根据points排序
        qsort(node2,MAX,sizeof(Node),cmp); 
         maxn=node2[0].points;  //第一大的points值
           i=1;
          while(node2[i].points==maxn)
          {
              i++;
          }
          //确定第二大的位置
          index=i;  //第二大的第一个位置
          maxn=node2[i].points;  //记录第二大的points值
          nCount=0;
          while(node2[i].points==maxn)
          {
             nCount++;
              i++;
          }
          //把排第二的排序一下
          qsort(node2+index,nCount,sizeof(Node),cmp1);
          while(nCount--)
          {
              printf("%d ",node2[index++].num);
          }
          printf("\n");
     }
        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