9.15 滴滴笔试

赛中T1AC了,T2过了45%,文中贴出的题解代码是正解。

T1(二分)

#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n, k;
int a[N];

bool check(int mid) {
	int rec = 1e9, cnt = 1;
	for(int i = 0; i < n; i ++) {
		int j = i;
		while(j < n && a[j] - a[i] < mid) j ++ ;

		if(j < n) rec = min(rec, a[j] - a[i]), cnt ++;
		i = j - 1;
	}
	return cnt >= k && rec >= mid;
}

void solve() {
	cin >> n >> k;
	for(int i = 0; i < n; i ++) cin >> a[i];

	int l = 1, r = 1e6 + 10;
	while(l < r) {
		int mid = l + r + 1 >> 1;
		if(check(mid)) l = mid;
		else r = mid - 1;
	}

	cout << r << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T2(01BFS)

T2原题:https://leetcode.cn/problems/minimum-obstacle-removal-to-reach-corner/

#include <bits/stdc++.h>

#define x first
#define y second

#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 1e5 + 10;

int n, m;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

void solve() {
	cin >> n >> m;
	vector<vector<int>> g(n, vector<int> (m));
	vector<vector<int>> dist(n, vector<int> (m, 1e9));

	for(int i = 0; i < n; i ++) {
		for(int j = 0; j < m; j ++) {
			cin >> g[i][j];
		}
	}

	deque<PII> q;
	q.push_back({0, 0});
	dist[0][0] = g[0][0];

	while(q.size()) {
		auto t = q.front(); q.pop_front();
		int x = t.x, y = t.y;
		for(int i = 0; i < 4; i ++) {
			int a = x + dx[i], b = y + dy[i];
			if(a >= 0 && a < n && b >= 0 && b < m) {
				if(dist[a][b] > dist[x][y] + g[a][b]) {
					dist[a][b] = dist[x][y] + g[a][b];
					if(g[a][b] == 1) {
						q.push_back({a, b});
					} else {
						q.push_front({a, b});
					}
				}
			}
		}
	}

	cout << dist[n - 1][m - 1] << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值