yinda_peng 发表于 2023-11-19 17:22:29

哪里出问题了?一直WA



#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int a;
deque<int> q;
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;i++)scanf("%d",&a);
    for(int i=1;i<=n;i++){
      while(!q.empty() && a>a)   q.pop_back();
      q.push_back(i);
      if(i>=m){
            while(!q.empty() && q.front()<=i-m) q.pop_front();
            printf("%d",a);
      }
    }
    printf("\n");
    while(!q.empty())   q.pop_front();
    for(int i=1;i<=n;i++)
    {
      while(!q.empty() && a<a)   q.pop_back();
      q.push_back(i);
      if(i>=m){
            while(!q.empty() && q.front()<=i-m) q.pop_front();
            printf("%d",a);
      }
    }
    printf("\n");
    return 0;
}


yinda_peng 发表于 2023-11-19 17:26:29

果真是空格的问题………{:10_262:}

isdkz 发表于 2023-11-19 17:22:39

这段代码的问题在于在输出结果时没有添加空格。在每次输出结果之后,应该添加一个空格,否则会导致格式错误。在两个地方都需要添加空格,分别是在 printf("%d",a); 之后和 printf("%d",a); 之后。修改后的代码如下:

#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int a;
deque<int> q;
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i<=n;i++)scanf("%d",&a[ i]);
    for(int i=1;i<=n;i++){
      while(!q.empty() && a>a[ i])q.pop_back();
      q.push_back(i);
      if(i>=m){
            while(!q.empty() && q.front()<=i-m) q.pop_front();
            printf("%d",a);
            if(i<n) printf(" ");// 添加空格
      }
    }
    printf("\n");
    while(!q.empty())q.pop_front();
    for(int i=1;i<=n;i++)
    {
      while(!q.empty() && a<a[ i])q.pop_back();
      q.push_back(i);
      if(i>=m){
            while(!q.empty() && q.front()<=i-m) q.pop_front();
            printf("%d",a);
            if(i<n) printf(" ");// 添加空格
      }
    }
    printf("\n");
    return 0;
}


这样修改后,输出结果之间会有一个空格,符合预期格式。

Mike_python小 发表于 2023-11-19 17:23:38

你的代码在使用 deque 实现滑动窗口最大值和最小值时存在一些问题。

首先,在第一个循环中,你没有在输出每个最大值之后加上空格,导致了格式错误。其次,在第二个循环中,你也没有在输出每个最小值之后加上空格。这两点都会导致格式不符合预期。

另外,你的代码似乎是以数组下标从1开始的方式来处理输入数据和数组操作的,这一点需要根据实际情况进行调整。

最后,在整个程序结束后,你需要清空双端队列中的元素,以便下一次使用。

下面是我修改后的代码:


#include <bits/stdc++.h>
using namespace std;

const int N = 1000005;
int a;
deque<int> q;

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) scanf("%d", &a);
    for (int i = 0; i < n; i++) {
      while (!q.empty() && a > a) q.pop_back();
      q.push_back(i);
      if (i >= m - 1) {
            while (!q.empty() && q.front() <= i - m) q.pop_front();
            printf("%d", a);
            if (i < n - 1) printf(" ");
      }
    }
    printf("\n");
    q.clear();
    for (int i = 0; i < n; i++) {
      while (!q.empty() && a < a) q.pop_back();
      q.push_back(i);
      if (i >= m - 1) {
            while (!q.empty() && q.front() <= i - m) q.pop_front();
            printf("%d", a);
            if (i < n - 1) printf(" ");
      }
    }
    printf("\n");
    return 0;
}


我对你的代码进行了一些修改,修复了输出格式、数组下标以及数据读入的问题。希望对你有所帮助!

yinda_peng 发表于 2023-11-19 17:36:31

现在买了竞赛书自己看着学,没老师教,学不动,唉……
页: [1]
查看完整版本: 哪里出问题了?一直WA