2015-06-01 40 views
-6

链接https://code.google.com/codejam/contest/4224486/dashboard这个解决方案的codejam测验有什么错误?问题的

我的解决办法:

#include <iostream> 
#include <vector> 
#include <fstream> 
using namespace std; 

int type_A(vector<int>vec){ // function for method 1 of computation; 
int ret = 0; 
for(auto i = 0;i<vec.size()-1;i++){ 
    int a = vec[i],b=vec[i+1]; 
    if(a>b)ret = ret+(a-b); 
} 
return ret; 
} // end of 1st function 

int type_B(vector<int>vec){ // function for method 2 of computation 
    int ret = 0; 
    for(auto i = 0;i<vec.size()-1;i++){ 
     if(i==vec.size()-2){ 
      if(vec[i]>vec[i+1])ret+=(vec[i]-vec[i+1]); 
     }else{ 
     ret += vec[i]; 
     } 
    } 
    return ret; 
} 
// end of function 
    int main() 
    { 
     ifstream input("input_file.in"); 
     ofstream output("output_file.out"); 
     int t; 
     input>>t; 
     for(auto i =1;i<=t;i++){ 
      int n; 
     input>>n; 
     vector<int>vec(n); 
    for(auto j = 0;j<vec.size();j++){ 
     int x; 
     input >>x; 
     vec[j] =x; 
    } 
     output << "Case #" << i << ": " << type_A(vec) << " " << type_B(vec) << endl; 


     } 

    } 

当我运行的问题给出了一些例子,我得到正确的输出,但是当我上传我的输出文件codejam它说,答案是不正确的 。请帮忙 。

+0

你的方法2没有考虑到固定的最小“吃饭速度” - 你需要首先确定。我相信这是一个失败的测试用例:“10 5 0”(答案应该是10)。 – molbdnilo

回答

0

您的算法对于类型2是错误的。它恰巧发生在所有示例碰巧有最大差异的项目之间的第二个最后项目和最后一个项目,您的代码依赖(测试i==vec.size()-2)。显然,所有的全面测试都不是这种情况。

您需要考虑一种不同的算法。

相关问题