为啥堆排序不行啊
查看原帖
为啥堆排序不行啊
1113121
w_official楼主2023/10/2 09:48

rt,堆排跟快排都是O(nlogn),按理说10^6不会T啊?是我自己的问题么?

堆排TLE70分的代码及评测结果

#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;

int n, now, ans;

struct sec
{
	int s, f;
	bool operator < (const sec tmp)const
	{
		return tmp.f < f;
	}
} a;
priority_queue<sec>q;

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &a.s, &a.f);
		q.push(a);
	}
	for (int i = 1; i <= n; ++i)
	{
		a = q.top();
		q.pop();
		if (a.s >= now)
		{
			now = a.f;
			++ans;
		}
	}
	printf("%d\n", ans);
	return 0;
}

这是改快排之后的AC代码和评测结果

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int n, now, ans;

struct sec
{
	int s, f;
//	bool operator < (const sec tmp)const
//	{
//		return tmp.f < f;
//	}
} a[2000000];

bool cmp(sec x1, sec x2)
{
	return x1.f < x2.f;
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		scanf("%d%d", &a[i].s, &a[i].f);
	}
	sort(a + 1, a + n + 1, cmp);
	for (int i = 1; i <= n; ++i)
	{
		if (a[i].s >= now)
		{
			now = a[i].f;
			++ans;
		}
	}
	printf("%d\n", ans);
	return 0;
}

咋回事啊?

2023/10/2 09:48
加载中...