版权声明:本人ZZU在校学生,文章均为个人心得,有不足之处请不吝赐教! https://blog.csdn.net/whl_program/article/details/82121281
思路:
题目要求城市是平行于坐标轴的正方形,我们只需要求出最大的横坐标差和最大的纵坐标轴之差,取较大值作为正方形城市边长即可
代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n;
cin >> n;
int xmax=0, xmin=1e9, ymax=0, ymin=1e9, X_MAX=0, Y_MAX=0;
for(int i=0; i<n; i++){
int x, y;
cin >> x >> y;
if(x > xmax)
xmax = x;
if(y > ymax)
ymax = y;
if(x < xmin)
xmin = x;
if(y < ymin)
ymin = y;
}
//这里要定义为LL 据说有同学直接输出较大值的积 不通过
//可能是因为坐标取值范围[-1e9, 1e9],最终面积会大于int范围
long long res = max(xmax-xmin,ymax-ymin);
cout << res*res << endl;
return 0;
}
思路:
暴力解决,set插入排除重复值
Java直接AC,但是C++一直是57%
1. 讨论帖同学A讲,换成C语言scanf printf就能通过,现在也没法儿试了
2. 讨论帖同学B讲,这道题考察离线线段树,具体我也不懂了
先贴出57%代码
#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
int n=2001, m=101;
int main()
{
cin >> n >> m;
int a[n+1];
for(int i=1; i<=n; ++i)
cin >> a[i];
int q;
cin >> q;
for(int i=0; i<q; ++i){
int l, r;
cin >> l >> r;
set<int> myset;
for(int j=l; j<=r; ++j) {
myset.insert(a[j]);
}
cout << myset.size() << endl;
}
return 0;
}
思路:
我觉得题目描述有问题,不是太清楚这道题
贴上别人AC代码 ,具体移步牛客讨论帖
https://www.nowcoder.com/discuss/99552?type=2&order=0&pos=4&page=1
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <queue>
#include <map>
using namespace std;
#define MAXN 50010
const int INF = 1e9;
int idx[MAXN];
int b[MAXN];
int a;
int dp[MAXN];
int main()
{
int n;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%d", &a);
idx[a-1] = i;
}
for(int i=0; i<n; i++){
scanf("%d", &a);
b[i] = idx[a-1];
dp[i] = INF; // 初始化为无限大
}
//最长递增子序列
int pos = 0; // 记录dp当前最后一位的下标
dp[0] = b[0]; // dp[0]值显然为a[0]
for (int i = 1; i < n; i++) {
if (b[i] > dp[pos]) // 若a[i]大于dp数组最大值,则直接添加
dp[++pos] = b[i];
else // 否则找到dp中第一个大于等于a[i]的位置,用a[i]替换之。
dp[lower_bound(dp, dp + pos + 1, b[i]) - dp] = b[i]; // 二分查找
}
printf("%d\n", pos+1);
return 0;
}