0

それが Google コード ジャムのストア クレジットの問題です。 https://code.google.com/codejam/contest/351101/dashboard#s=p0

私のコードは、大規模なテストを実行した後に SIGSEGV を発行します。しかし、答えは正しいです!

#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int ps[1000]={0};
vector<int> indice[1000];
int main() {
  int cases; scanf("%d", &cases);
  for(int j=1;j<=cases;j++) {
    printf("Case #%d: ", j);
    int c, is; scanf("%d%d", &c, &is);
    for(int i=0;i<=c;i++) ps[i]=0;
    for(int i=0;i<=c;i++) indice[i].clear();
    for (int i = 0; i < is; i++) {
      int it; scanf("%d", &it);
      indice[it].push_back(i+1);
      ps[it]=1;
      if (c-it>0&&ps[c-it]) {
        int a, b;
        a = indice[it][0];
        b = indice[c-it][0];
        if(c==2*it&&indice[it].size()>1) {
          b=indice[it][1];
        }

        if (a!=b) {
          printf("%d %d\n", min(a,b),max(a,b));
        }
      }
    }
  }
  return 0;
}

だから私はvalgrindを使って何が起こっているのかを調べます..しかし、それは私の問題ではないようです.

==17599== Invalid free() / delete / delete[] / realloc()
==17599==    at 0x4C2A4BC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==17599==    by 0x401669: __gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long) (new_allocator.h:98)
==17599==    by 0x4013CD: std::_Vector_base<int, std::allocator<int> >::_M_deallocate(int*, unsigned long) (stl_vector.h:156)
==17599==    by 0x400F60: std::_Vector_base<int, std::allocator<int> >::~_Vector_base() (stl_vector.h:142)
==17599==    by 0x400D8D: std::vector<int, std::allocator<int> >::~vector() (stl_vector.h:351)
==17599==    by 0x400C48: __tcf_0 (a.cpp:6)
==17599==    by 0x5383900: __run_exit_handlers (exit.c:78)
==17599==    by 0x5383984: exit (exit.c:100)
==17599==    by 0x5369773: (below main) (libc-start.c:258)
==17599==  Address 0x1 is not stack'd, malloc'd or (recently) free'd
==17599== 
==17599== 
==17599== HEAP SUMMARY:
==17599==     in use at exit: 128 bytes in 1 blocks
==17599==   total heap usage: 4,527 allocs, 4,527 frees, 113,664 bytes allocated
==17599== 
==17599== LEAK SUMMARY:
==17599==    definitely lost: 0 bytes in 0 blocks
==17599==    indirectly lost: 0 bytes in 0 blocks
==17599==      possibly lost: 0 bytes in 0 blocks
==17599==    still reachable: 128 bytes in 1 blocks
==17599==         suppressed: 0 bytes in 0 blocks
==17599== Rerun with --leak-check=full to see details of leaked memory
==17599== 
==17599== For counts of detected and suppressed errors, rerun with: -v
==17599== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)

私はとても混乱しています...何が起こっているのか誰か教えてもらえますか? 私は C++ の初心者です。どうもありがとうございました。

4

1 に答える 1