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++ AC g++WA 用的STL priorityqueue

Posted by seu_cl at 2009-08-22 19:01:41 on Problem 1521
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char s[1000];
int a[300];
struct node
{
	int v,w;
} b[10000];
struct poi
{
	int p;	
};
bool operator<(const poi &A,const poi&B)
{
	return b[A.p].v>b[B.p].v;
}
int main()
{
	while (scanf("%s",s)==1&&strcmp(s,"END")!=0)
	{
		int l,i,j;
		l=strlen(s);
		memset(a,0,sizeof a);
		for (i=0;i<l;i++) a[s[i]]++;
		priority_queue<poi> Q;
		for (j=i=0;i<300;i++)
		{
			if (a[i]>0)
			{
				b[j].v=a[i];
				b[j].w=0;
				poi t;
				t.p=j++;
				Q.push(t);
			}
		}
		if (j==1)
		{
			printf("%d %d 8.0\n",b[0].v*8,b[0].v);
			continue;
		}
		while (1)
		{
			poi t1=Q.top();
			Q.pop();
			if (Q.empty()) break;
			poi t2=Q.top();
			Q.pop();
			b[j].v=b[t1.p].v+b[t2.p].v;
			b[j].w=b[t1.p].w+b[t2.p].w+b[j].v;
			poi t;
			t.p=j++;
			Q.push(t);
		}
		printf("%d %d %.1lf\n",l*8,b[j-1].w,(double)(l*8/b[j-1].w));
	}
}

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