算法分析与设计作业3

1.问题
写出两种检索算法:在一个排好序的数组T[1…n]中查找x,如果x在T中,输出x在T的下标j;如果x不在T中,输出j=0.按实验模板编写,“分析”部分仅给出复杂度结果即可。

2.解析
1.
通过一个标记数组存储数组数据,标记数组下标为数据值,值为原数据对应下标,实现O(1)查询;
2.
二分查找,找到则返回下标,没找到则为0;

3.设计
1.

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 10;
const int N = 2e2 + 10;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
template<typename T>
inline void rd(T&x) {
	int temp = 1;
	char c = getchar();
	x = 0;
	while (c > '9' || c < '0') {if (c == '-')temp = -1;	c = getchar();}
	while (c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
	x *= temp;
}
ll gcd(ll x, ll y) {return y ? gcd(y, x % y) : x;}
int vis[maxn];
int n;
signed main() {
	cin >> n;
	mem(vis, 0);
	for (int i = 1; i <= n; ++i) {
		int x;
		cin >> x;
		vis[x] = i;
	}
	int q;
	while (cin >> q) {
		if (vis[q])cout << vis[q] << endl;
		else cout << 0 << endl;
	}
	return 0;
}
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 10;
const int N = 2e2 + 10;
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
template<typename T>
inline void rd(T&x) {
	int temp = 1;
	char c = getchar();
	x = 0;
	while (c > '9' || c < '0') {if (c == '-')temp = -1;	c = getchar();}
	while (c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
	x *= temp;
}
ll gcd(ll x, ll y) {return y ? gcd(y, x % y) : x;}
int a[maxn];
int vis[maxn];
int n;
signed main() {
	cin >> n;
	mem(vis, 0);
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		vis[a[i]] = i;
	}
	sort(a + 1, a + 1 + n);
	int q;
	while (cin >> q) {
		int pos = lower_bound(a + 1, a + 1 + n, q) - a;
		if (a[pos] ==q)cout << vis[a[pos]] << endl;
		else cout << 0 << endl;
	}
	return 0;
}

4.分析

  1. O(1)
  2. O(logn)

5.源码

  1. 1
  2. 2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值