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了,自己排序一直超时,用qsort结构排序通过了

Posted by 677 at 2013-11-29 12:23:26 on Problem 1007
#include "iostream"
#include "string"
#include "stdlib.h"
#include "vector"
using namespace std;

class Astr{
public:
	string s;
	int a;
};

int cmp(const void* x, const void* y){
	return ((Astr*)x)->a > ((Astr*)y)->a?1:-1;
}

int main(){
	int m_cases,n_len;
	vector<Astr> aver;
	string str;
	Astr as;
	cin>>n_len>>m_cases;
	for (int i=0; i<m_cases; i++)
	{
		cin>>str;
		aver.push_back(as);
		aver.at(i).s=str;
	}

	for (int j=0; j<m_cases; j++)
	{
		int inversion=0;
		for (int k=0; k<n_len-1; k++)
		{
			for (int l=k+1; l<n_len; l++)
			{
				if(aver.at(j).s.at(k)>aver.at(j).s.at(l))
					inversion++;
			}
		}
		aver.at(j).a=inversion;
	}

	qsort(&aver[0], m_cases, sizeof(Astr), cmp);

	for (int haha=0; haha<m_cases; haha++)
	{
		cout<<aver.at(haha).s<<endl;
	}
	
	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