C++中的一组对程序
我正在为数组中的每个子集生成一组最大和最小元素对,但这会给我带来这些错误。最后我需要一套的尺寸。 编辑了一些建议 在函数“int main”中: 27:12:错误:“max_元素”未在此范围内声明 27:12:注:建议的备选方案:“最大对齐” 28:12:错误:“min_元素”未在函数“int main”的此作用域中声明: 代码:C++中的一组对程序,c++,C++,我正在为数组中的每个子集生成一组最大和最小元素对,但这会给我带来这些错误。最后我需要一套的尺寸。 编辑了一些建议 在函数“int main”中: 27:12:错误:“max_元素”未在此范围内声明 27:12:注:建议的备选方案:“最大对齐” 28:12:错误:“min_元素”未在函数“int main”的此作用域中声明: 代码: 为了使你的最后一个计划发挥作用,需要移动std::vector v的声明 此外,您的代码在^3上具有复杂性。实际上,通过计算^2的复杂性是可能的 迭代计算最大值和最小
为了使你的最后一个计划发挥作用,需要移动std::vector v的声明 此外,您的代码在^3上具有复杂性。实际上,通过计算^2的复杂性是可能的 迭代计算最大值和最小值 此代码比较您的代码和新代码。结果是一致的。然而,我不能肯定 您的原始代码执行了您打算执行的操作
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
typedef std::pair<int,int> pairs;
//using namespace std;
void print (const std::set<pairs> &s) {
for (auto& p: s) {
std::cout << "(" << p.first << ", " << p.second << ") ";
}
std::cout << "\n";
}
int count_pairs_op (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
std::vector<int> v;
for(int j = i; j < n; j++) {
v.push_back(a[j]);
if(v.size() > 1) {
max = *std::max_element(v.begin(), v.end());
min = *std::min_element(v.begin(), v.end());
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
}
print (s);
return s.size();
}
int count_pairs_new (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
min = a[i];
max = a[i];
for(int j = i+1; j < n; j++) {
max = std::max (max, a[j]);
min = std::min (min, a[j]);
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
print (s);
return s.size();
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_op(a) << std::endl;
std::cout << count_pairs_new(a) << std::endl;
}
如果你正在学习C++,去掉BITS/STDC++,它是一个拐杖,而不是为每个标准库工具使用适当的标题。在这种情况下,包括ios_base的其他内容。请检查以确保您的操作正确。在使用它带来的任何名称之前,需要先执行using指令。其余的误差是级联误差,非const n的[a] n[n]是非标准C++。考虑使用STD::向量和推送回退条目。你已经在其他地方这样做了。另一方面,对单数事物使用复数名称会在将来给你带来很多混乱。谢谢你的帮助,但我之前已经将向量移到1个循环上面并尝试过了。但我的代码部分是正确的。它只执行部分测试用例,并给出少数测试用例的TLE。我应该试试别的办法。这真是个难题。如果你有空,请解决它并向我解释,先生!!代码应该做什么还不清楚。你有更深入的解释吗?代码失败的输入/输出示例?这是错误输出的问题还是时间限制的问题?我读了链接。看来你对这个问题有误解。b不是子阵列的最小值,而是第二个最大值。此外,修改您或我的代码可以解决正确性问题,而不是效率问题。新问题与你帖子中原来的问题相去甚远。我想最好提出一个新问题,对问题进行新的描述,并编写一个代码来解决这个新问题。如果我没有错的话,您的代码在简单的12345测试用例中也会失败。正确答案是4,而不是10。在您刚才提供的测试用例中,我不希望您的代码提供2的答案。您可以使用我在回答中输入的代码进行检查。例如,对于所有低于I的索引,max_up[I]是最大的元素。如果该值大于a[i],则存在一对a[j],a[i],使得ja[i]。然后,至少存在从j到i的子阵列,使得a[i]是第二max元素。此外,最多存在一个这样的有效子阵列,使得[i]是第二max元素。因此,count++;
typedef pair<int,int> pairs;
typedef std::pair<int,int> pairs;
typedef std::pair<int,int> pair_of_ints;
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
typedef std::pair<int,int> pairs;
//using namespace std;
void print (const std::set<pairs> &s) {
for (auto& p: s) {
std::cout << "(" << p.first << ", " << p.second << ") ";
}
std::cout << "\n";
}
int count_pairs_op (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
std::vector<int> v;
for(int j = i; j < n; j++) {
v.push_back(a[j]);
if(v.size() > 1) {
max = *std::max_element(v.begin(), v.end());
min = *std::min_element(v.begin(), v.end());
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
}
print (s);
return s.size();
}
int count_pairs_new (const std::vector<int>& a) {
int max, min;
int n = a.size();
std::set<pairs> s;
for(int i = 0; i < n; i++) {
min = a[i];
max = a[i];
for(int j = i+1; j < n; j++) {
max = std::max (max, a[j]);
min = std::min (min, a[j]);
pairs p1 = std::make_pair(max, min);
s.insert(p1);
}
}
print (s);
return s.size();
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_op(a) << std::endl;
std::cout << count_pairs_new(a) << std::endl;
}
#include <iostream>
#include <set>
#include <vector>
#include <utility>
#include <algorithm>
int count_pairs_2nd_max (const std::vector<int>& a) {
int n = a.size();
int count = 0;
std::vector<int> max_up(n), max_down(n);
max_up[0] = -1;
for (int i = 1; i < n; ++i) {
max_up[i] = std::max(max_up[i-1], a[i-1]);
}
max_down[n-1] = -1;
for (int i = n-2; i >= 0; --i) {
max_down[i] = std::max(max_down[i+1], a[i+1]);
}
for(int i = 0; i < n; ++i) {
if (max_up[i] > a[i]) count++;
if (max_down[i] > a[i]) count++;
}
return count;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int n;
std::cin >> n;
std::vector<int> a(n);
for(int i = 0; i < n; i++) {
std::cin >> a[i];
}
std::cout << count_pairs_2nd_max(a) << std::endl;
}