枚举与优化套路

本文详细介绍了ACM竞赛中常见的枚举与优化策略,包括二分查找、哈希表的使用、双指针技巧、前缀和优化等。通过实例解析,如四平方和、礼物分配问题等,阐述如何利用这些方法提高算法效率,降低复杂度,解决实际问题。
摘要由CSDN通过智能技术生成

ACM题集:https://blog.csdn.net/weixin_39778570/article/details/83187443

出自蓝桥学苑 GtDzx老师
#枚举与优化套路(1)
这里有一些非常常用的思路。1是二分,二分查找、二分搜索非常有效,一般是复杂度从O(N)降到O(logN)。使用范围也很广,我们会在后面专门拿出一节时间来讲。2是用Hash,空间换时间。此外还有一些常用的套路:比如双指针,Leetcode上对应的分类是two pointer,直译过来就是双指针,大概的思想就是滑动窗口。还比如前缀后缀和,也是空间换时间的思路


#枚举与优化套路(2)

使用map(红黑树)或者unoreder_map(哈希表),c++11支持,时间复杂度为O(logn)和O(1)


#枚举与优化套路(3)

使用map(红黑树)或者unoreder_map(哈希表),c++11支持,时间复杂度为O(logn)和O(1)

这里写图片描述
支持C++11
这里写图片描述

不支持C++11

#include <iostream>
#include <set>
using namespace std; 
int n, k, x, ans = 0;
set<int> myset;
int main(){
	cin >> n >> k;
	for(int i=0; i<n; i++){
		cin >> x;
		myset.insert(x);
	}
	for(set<int>::iterator i=myset.begin(); i!=myset.end(); i++){
		if(myset.find((*i)+k) != myset.end()){ // (*)解引用
			ans++;
		}
	}
	cout << ans << endl;	
	return 0;
}

题目连接:hihoCoder的1494题

这里写图片描述
支持C++11
这里写图片描述
不支持C++11

#include <iostream>
#include <map>
using namespace std;
int n, c, t, st;
map<int, int> cnt;
int main(){
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> c;
		st = 0;
		for(int j=0; j<c; j++){
			cin >> t;
			st += t;
			if(j != c-1) cnt[st]++;
		}
	}
	int max = 0;
	for(map<int,int>::iterator item=cnt.begin(); item!=cnt.end(); item++){
		if((*item).second > max) max = (*item).second;
	}
	cout << n-max << endl;
	return 0;
}

#枚举与优化套路(4)

例题:蓝桥杯:四平方和、hihoCoder #1505题:小Hi和小Ho的礼物。

这里写图片描述
不支持C++11

#include <iostream>
#include <map>
#include <cmath>
using namespace std;
int n;
map<int, int> f; 
int main(){
	cin >> n;
	for(int c=0; c*c <= n/2; c++){
		for(int d=c; c*c+d*d<=n; d++){
			if(f.find(c*c + d*d) == f.end()){
				f[c*c+d*d] = c; // 保存最小的c 
			}
		}
	}
	for(int a=0; a*a*4<=n; a++){
		for(int b=a; a*a+b*b<=n/2; b++){
			if(f.find(n-a*a-b*b) != f.end()){ // 找到最小的b 
				int c = f[n-a*a-b*b];
				int d = int(sqrt(n - a*a - b*b - c*c) + 1e-3);
				cout << a << ' ' << b << ' ' << c << ' ' << d << endl; 
				return 0;
			}
		}
	}
	return 0;
}

有些同学可能会有疑问,就是f里保存的是c最小的解,会不会这个c比b小,不满足题目要求。比如N=30,我们枚举到a=1,b=2,这时f[25]=0,我们找到的解会是a=1, b=2, c=0, d=5。实际上不用担心这个问题。因为如果a=1, b=2, c=0, d=5是一个解,那么换一下顺序a=0, b=1, c=2, d=5也一定是一个解。并且a=0, b=

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值