排序 pat1080 Graduate Admission (30 分) (未AC,一个节点超时)

按照总分数和ge排序

按照分数顺序,再按照志愿顺序记录下每个志愿是否被录取,录取则break跳出循环

第一次写的答案有一个节点超时

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

typedef struct node
{
	int id;
	int ge;
	int g1;
	int g;
	int choice[10];
	bool flag;
}app;

typedef struct node1
{
	int stunum;
	app stuid[10];
	int quo;
}school;

bool cmp1(app a,app b)
{
	return a.id < b.id;
}

bool cmp(app a,app b)
{
	if(a.g != b.g)
		return a.g > b.g;
	if(a.ge != b.ge)
		return a.ge > b.ge; 
}

int main() 
{
	int n , m, k;
	int br = 0;
	app a[40010];
	school sch[110];
	scanf("%d %d %d",&n,&m,&k);
	for(int i =0;i<m;i++)
	{
		sch[i].stunum = 0;
		scanf("%d",&sch[i].quo);
	}
	for(int i =0;i<n;i++)
	{
		scanf("%d %d",&a[i].ge,&a[i].g1);
		a[i].flag = false;
		a[i].id = i;
		a[i].g = a[i].ge + a[i].g1;
		for(int j =0;j<k;j++)
		{
			scanf("%d",&a[i].choice[j]);
		}
	}
	
	sort(a,a+n,cmp);
	
	for(int j = 0;j<n;j++)
	{
		for(int z = 0;z<k;z++)
		{
			for(int i = 0;i<m;i++)
			{
				if(sch[i].quo==0 && a[j].flag == false && a[j].choice[z] == i && a[j].g == sch[i].stuid[sch[i].stunum - 1].g && a[j].ge == sch[i].stuid[sch[i].stunum - 1].ge)
				{
					a[j].flag = true;
					sch[i].stuid[sch[i].stunum]=a[j];
					sch[i].stunum ++;
					break;
				}
				else if(sch[i].quo>0 && a[j].flag == false && a[j].choice[z] == i)
				{
					sch[i].quo--;
					a[j].flag = true;
					sch[i].stuid[sch[i].stunum]=a[j];
					sch[i].stunum ++;
					break;
				}
			}
        }
	}
	 
	for(int i = 0;i<m;i++)
	{
		sort(sch[i].stuid,sch[i].stuid+sch[i].stunum,cmp1);
		for(int j=0;j<sch[i].stunum;j++)
		{
			if(j==0)
				printf("%d",sch[i].stuid[j].id);
			else
				printf(" %d",sch[i].stuid[j].id);
		}
		printf("\n");
	}
	
	
	
	return 0;
}
在这里插入代码片

问题应该是处理的时候中间有一个n3的循环,过于复杂

事实上最后一个循环没有意义,可以删除。可是还是超时

问题还有可能是学校的节点包含了学生志愿节点,其实只需要知道学生的id就够了

改进只记录学校上一个录取学生的id和记录录取学生id的数组。

还是超时。。。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

typedef struct node
{
	int id;
	int ge;
	int g1;
	int g;
	int choice[10];
}app;

typedef struct node1
{
	int stunum;
	int lastid;
	int stuid[10];
	int quo;
}school;

bool cmp1(int a,int b)
{
	return a < b;
}

bool cmp(app a,app b)
{
	if(a.g != b.g)
		return a.g > b.g;
	if(a.ge != b.ge)
		return a.ge > b.ge; 
}

int main() 
{
	int n , m, k;
	app a[40000];
	school sch[110];
	scanf("%d %d %d",&n,&m,&k);
	for(int i =0;i<m;i++)
	{
		sch[i].stunum = 0;
		scanf("%d",&sch[i].quo);
	}
	for(int i =0;i<n;i++)
	{
		scanf("%d %d",&a[i].ge,&a[i].g1);
		a[i].id = i;
		a[i].g = a[i].ge + a[i].g1;
		for(int j =0;j<k;j++)
		{
			scanf("%d",&a[i].choice[j]);
		}
	}
	
	sort(a,a+n,cmp);
	
	for(int j = 0;j<n;j++)
	{
		for(int z = 0;z<k;z++)
		{
			int i = a[j].choice[z];
				if(sch[i].quo==0 && a[j].g == a[sch[i].lastid].g && a[j].ge == a[sch[i].lastid].ge)
				{
					sch[i].lastid = j;
					sch[i].stuid[sch[i].stunum] = a[j].id;
					sch[i].stunum ++;
					break;
				}
				else if(sch[i].quo>0)
				{
					sch[i].quo--;
					sch[i].lastid = j;
					sch[i].stuid[sch[i].stunum] = a[j].id;
					sch[i].stunum ++;
					break;
				}
        }
	}
	 
	for(int i = 0;i<m;i++)
	{
		sort(sch[i].stuid,sch[i].stuid+sch[i].stunum,cmp1);
		for(int j=0;j<sch[i].stunum;j++)
		{
			if(j==0)
				printf("%d",sch[i].stuid[j]);
			else
				printf(" %d",sch[i].stuid[j]);
		}
		printf("\n");
	}
	
	
	
	return 0;
}
在这里插入代码片

最终改良版本。。。但是还是超时了

超时问题找到了,在于cmp排序的时候 多了一个判断条件
不用比较ge是否相同
但是这样答案错误了。。。

猜你喜欢

转载自blog.csdn.net/qq_15556537/article/details/89241418
今日推荐