寒假每日练习:常见优化技巧

文章分享了解四道编程题目,涉及双指针算法在快速排序中的应用,动态规划解决的问题,使用大顶堆的数据结构,以及通过数学方法简化计算以提高效率。
摘要由CSDN通过智能技术生成

今天写了4道题,总体来说收获不错,P1638,P1115,P7072,P2671,都可以说说:

首先是P1638,这道题是双指针的题主要是通i++和j++来移动区间,这个在快速排序里讲过:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int peo[2004];
int dataa[1000003];
int n, m;

int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> dataa[i];
	}
	int num = 0;
	int i = 1;
	int l = 1, r = 1;
	while (num != m)
	{
		if (!peo[dataa[i]])
			num++;
		peo[dataa[i]]++;
		r++;
		i++;
	}
	r--;
	while (peo[dataa[l]] > 1)
	{
		peo[dataa[l]]--;
		l++;
	}
	int ansl = l;
	int ansr = r;
	while (r <= n)
	{
		r++;
		peo[dataa[r]]++;
		while(peo[dataa[l]] > 1)
		{
			peo[dataa[l]]--;
			l++;
		}
		if (ansr - ansl > r - l)
		{
			ansr = r;
			ansl = l;
		}
	}
	cout << ansl << " " << ansr;
	return 0;
}

然后是P1115,这个是老问题了,我都写过不知道多少遍了,但是有个大佬的题解给我看蒙了,

通过动态规划来写,简单方便:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int n;
int a, b;
int ans = -10000000;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a;
		if (i == 1)
		{
			b = a;
		}
		else
			b = max(a, b + a);
		ans = max(b, ans);
	}
	cout << ans;
	return 0;
}

然后是P7072,这个我学到了新知识,对顶堆,维护一段序列第k个的数,很好用:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <queue>

using namespace std;

priority_queue <int, vector<int>, less<int> > q1; // 大顶堆
priority_queue <int, vector<int>, greater<int> > q2; // 小顶堆
int n, w;

void op(int dataa, int k)
{
	if (dataa > q2.top())
	{
		q2.push(dataa);
	}
	else {
		q1.push(dataa);
	}
	while (q2.size() > k)
	{
		q1.push(q2.top());
		q2.pop();
	}
	while (q2.size() < k)
	{
		q2.push(q1.top());
		q1.pop();
	}
}


int main()
{
	cin >> n >> w;
	int tm;
	cin >> tm;
	q2.push(tm);
	printf("%d ", tm);
	for (int i = 2; i <= n; i++)
	{
		int tmp;
		cin >> tmp;
		int k = max(1, int(w * i / 100));
		op(tmp, k);
		printf("%d ", q2.top());
	}

	return 0;
}

P2671这个是数学问题,化简公式来缩小时间复杂度,得记住:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>

using namespace std;
int n, m;
int cnt[100004][2], sum[100005][2], color[100005], nu[100005];
int ans = 0;
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		cin >> nu[i];
	}
	for (int i = 1; i <= n; i++)
	{
		cin >> color[i];
		cnt[color[i]][i % 2]++;
		sum[color[i]][i % 2] = (sum[color[i]][i % 2] + nu[i]) % 10007;
	}
	for (int i = 1; i <= n; i++)
	{
		ans += i * ((cnt[color[i]][i % 2] - 2ll) * nu[i] % 10007 + sum[color[i]][i % 2]);
		ans = ans % 10007;
	}
	cout << ans;
	return 0;
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值