2018 360校招笔试编程题

123

样例:

2

0 0

2 2

输出:

4

样例:

2

0 0

0 3

输出:

9

一道简单题,遍历所有点,x轴记录一个最小值最大值,y轴记录一个最小值最大值,因为是正方形所有选两个最大值-最小值中较大的一个,平方之后输出就可以了

#include<algorithm>
#include<iostream>
using namespace std;
int main() {
	long long x1, x2, y1, y2, x, y, n, l;
	while (cin >> n) {
		x1 = y1 = 1e9+7,x2 = y2 = -(1e9+7);
		for (int a = 0; a < n; a++)
			cin >> x >> y, x1 = min(x1, x), x2 = max(x2, x), y1 = min(y1, y), y2 = max(y2, y);
		l = max((x2 - x1), (y2 - y1));
		cout << l * l << endl;
	}
	return 0;
}

123

样例:

5 3
1 2 3 2 2
3
1 4
2 4
1 5

输出:

3

2

3

看到这道题,一开始想到线段树,想了想,好像直接前缀和就行了,但是看复杂度的话写出来是 1e8 如果跑满了可能就会超时,一时没想到优化,就直接交了,然后就AC了。。。

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int num[2005][105], q, n, m, l, r, x;
int main() {
	while (~scanf("%d%d", &n, &m)) {
		memset(num, 0, sizeof(num));
		for (int a = 1; a <= n; a++) {
			scanf("%d", &x);
			for (int b = 0; b <= m; b++)
				num[a][b] = num[a - 1][b];
			num[a][x]++;
		}
		scanf("%d", &q);
		while (q--) {
			scanf("%d%d", &l, &r);
			int ans = 0;
			for (int a = 0; a <= m; a++)
				if (num[r][a] - num[l - 1][a])
					ans++;
			printf("%d\n", ans);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/icliuli/article/details/82119314
今日推荐