Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ codeforces显示WA,但终端上的输出正确_C++ - Fatal编程技术网

C++ codeforces显示WA,但终端上的输出正确

C++ codeforces显示WA,但终端上的输出正确,c++,C++,当我提交CodeForces551C的解决方案时,法官在第49个测试案例中给了我一个错误的答案。但是当我在终端上运行相同的代码时(使用g++4.9.2),答案是正确的 #include <bits/stdc++.h> #include <algorithm> #define endl '\n' using namespace std; template <class T> inline void dbg(string s,T &a) {

当我提交CodeForces551C的解决方案时,法官在第49个测试案例中给了我一个错误的答案。但是当我在终端上运行相同的代码时(使用g++4.9.2),答案是正确的

#include <bits/stdc++.h>
#include <algorithm>
#define endl '\n'
using namespace std;

template <class T>
inline void dbg(string s,T &a)
{
      cout << s+" is => " << a << endl;
}

int n,m;
int a[100005];
bool check(long long int time)
{
      long long int consumed,temp=0,diff;
      for(int i=1;n!=0 && i<=m;i++)
      {
            consumed=n;
            while(n>0 && consumed<time)
            {
                  if(!temp)
                        temp=a[n];
                  diff=temp<(time-consumed)?temp:time-consumed;
                  temp-=diff;
                  consumed+=diff;
                  if(!temp)
                        n--;
            }
      }
      if(n==0)
            return true;
      else
            return false;
}

int main()
{
      cin >> n >> m;
      for(int i=1;i<=n;i++)
            cin >> a[i];

      while(a[n]==0) n--;
      long long int last_nonempty=n,l=n+1,r=LONG_MAX,mid;

      while(l<r)
      {
            mid=l+(r-l)/2;
            n=last_nonempty;
            if(check(mid))
                  r=mid;
            else
                  l=mid+1;
      }
      cout << l << endl;

      return 0;
}
#包括
#包括


请提供帮助。

顺便说一句,在运算符周围添加空格将使代码更具可读性,并且不会影响执行时间。如果在线法官给了你错误,请选择其他学习方法。我强烈建议使用调试器(
gdb
)或带有调试器的IDE,并使用测试用例中产生错误答案的输入值。回答错误的案例看起来代码中有溢出。可能您的初始化有问题。在您自己的IDE中,结果是正确的,但在OJ上是错误的。