求计算时间复杂度
查看原帖
求计算时间复杂度
794484
huanglihuan楼主2024/10/9 19:29
#include <bits/stdc++.h>
#include <cstdio>
#define int long long
#define ull unsigned long long
#define mod 998244353
#define MOD 1000000007
using namespace std;
int n;
const int N = 2e6 + 5,maxn = 3e3 + 5;
struct record
{
	bool type;
	int price;
	int t;
} a [N];
struct Node
{
	int price;
	int t;
} node;
inline int read ()
{
	int x = 0;
	bool f = 1;
	char c = getchar ();
	while (c < '0' || c > '9') f = (c == '-' ? !f : f),c = getchar ();
	while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48),c = getchar ();
	return f ? x : -x;
}
inline void write (int x)
{
	if (x < 0) putchar ('-'),x = -x;
	if (x > 9) write (x / 10);
	putchar (x % 10 + '0');
	return ;
}
void input ()
{
	n = read ();
	for (int i = 1;i <= n;i ++) a [i].type = read (),a [i].price = read (),a [i].t = read ();
}
signed main ()
{
	input ();
	queue <Node> q;
	int ans = 0;
	for (int i = 1;i <= n;i ++)
	{
		if (a [i].type == 0)
		{
			ans += a [i].price;
			node.price = a [i].price;
			node.t = a [i].t;
			q.push (node);
		}
		else
		{
			if (q.size () == 0) ans += a [i].price;
			else
			{
				queue <Node> qq;
				bool ok = false;
				while (! q.empty ())
				{
					node = q.front ();
					if (node.t < a [i].t - 45)
					{
						q.pop ();
						continue;
					}
					if (node.price >= a [i].price && ok == false)
					{
						q.pop ();
						ok = true;
						continue;
					}
					qq.push (q.front ());
					q.pop ();
				}
				while (! qq.empty ())
				{
					q.push (qq.front ());
					qq.pop ();
				}
				if (! ok) ans += a [i].price;
			}
		}
	}
	write (ans);
	return 0;
}

rt,大循环是 nn,小循环是当前满足条件的优惠票数,

if (node.t < a [i].t - 45)
{
	q.pop ();
	continue;
}

这几行是剪枝,由于 tt 数组递增可以把不满足时间的全部剪掉,但是如果全 1 理论时间复杂的会来到 n2n^2。所以到底是啥?

2024/10/9 19:29
加载中...