JOI 2007 A Problem, Half of the Test Cases Do Not Go Through Even though the Algorithm Looks Correct

Asked 2 years ago, Updated 2 years ago, 29 views

I used the C++ queue to find the maximum value of the partial sum of the given sequence, but it does not AC.The test case at the original site shows the same output, but half of the atcoder shows WA.

JOI 2007A

#include<bits/stdc++.h>
#define_GLIBCXX_DEBUG
# define rep(i,n) for (inti=0;i<n;i++)
# define reps(i,n) for (inti=1;i<=n;i++)
#define INF1e9
# define ALL(v) v. begin(), v. end()

using namespace std;
using ll = long long;


int main() {
  int n,k;
  cin>>n>>k;
  lls = 0;
  lla;

  queue<int>q;
  rep(i,k){
    cin>>a;
    s+=a;
    q.push(a);
  }

  llans=s;
  if(n>k){
    rep(i,n-k){
      cin>>a;
      s - = q.front();
      q.pop();
      s+=a;
      q.push(a);
      an=max(ans,s);
    }
  }
  cout<<ans;
}

c++

2022-09-29 22:32

1 Answers

As Metropolis pointed out, the problem seems to be that there is no new line (std::endl).
std::endl has the ability to call std::basic_stream::flush() as well as line breaks.
Otherwise, the output data will not be written to the standard output while being buffered.
Therefore, it depends on the buffer whether the program flushes or not, so depending on luck, sometimes it becomes WA.


2022-09-29 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.