首页 > 解决方案 > 出现错误 - 以退出代码 3221225477 完成

问题描述

#include<iostream>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<string>
typedef long long ll;

using namespace std;

int main() {

    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif 

    multimap< ll, pair<ll,ll>, greater <ll> > mymap; 

    ll n,k,i,j,l=0,sum,temp;
    cin>>n>>k;
    vector<ll> s(n);
    vector<ll> c(n);
    vector<ll> d(n);
    vector<ll> e(n);

    for(i=0;i<n;i++) cin>>s[i]; 

    for(i=0;i<n;i++) cin>>c[i];  // SOME PROBLEM IS HERE!!///

    // for(i=0;i<n;i++) cout<<s[i]<<" "<<c[i];  

    for(i=0;i<n;i++) mymap.insert(make_pair(s[i],make_pair(c[i],i)));
    i=0;
    for (auto it=mymap.begin() ; it!=mymap.end() ; i++,it++){
         d[i]=(it)->second.first;
         s[i]=(it)->second.second;
    }    
    sort(c.rbegin(), c.rend());     // s,c,d,e

    for(i=0;i<n;i++){
        auto it=find(c.begin(), c.end(),d[i]);
        temp=d[i];
        c.erase(it);
        sum=0;
        j=0;
        auto yt=c.begin();

        while(j!=k && yt!=c.end())
        {
            sum=sum+c[j];
            j++;
            yt++;
        }

        e[s[i]]=sum+temp;
    }
    for(i=0;i<n;i++)
    cout<<e[i]<<" ";


  }

这是我要解决的问题

我的这段代码有时编译不输出任何内容或给出错误:[在 4.5 秒内完成,退出代码为 3221225477]

我遵循了类似的标记问题,但其中陈述的所有内容似乎都不是这段代码的问题。我尝试将打印语句放入代码中,但在第一个 for 循环之后没有任何工作(我已经在代码中注释了该区域),我也遵循了文档多图,但仍然没有任何改进。

在这个带有输入的代码中

4 2
4 5 9 7
1 2 11 33

我期待输出为

1 3 46 36 

当我在 Codeforces Online Judge 中提交它时,它会导致运行时错误并带有描述。

Diagnostics
Diagnostics detected issues [cpp.clang++-diagnose]: =================================================================
==3480==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x11e00030 at pc 0x001529a8 bp 0x1087f434 sp 0x1087f430
WRITE of size 8 at 0x11e00030 thread T0
    #0 0x1529a7 in main K:\invoker-prod\work\codeforces6\9b5b62769a35581e24568bbc4cf484df\compile-e4b70584810488eac396a75d667b010b\p71.cpp:35
    #1 0x1fc88f in __scrt_common_main_seh f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:283
    #2 0x756b343c in BaseThreadInitThunk+0x11 (C:\Windows\syswow64\kernel32.dll+0x1343c)
    #3 0x77da9831 in RtlInitializeExceptionChain+0x62 (C:\Windows\SysWOW64\ntdll.dll+0x39831)
    #4 0x77da9804 in RtlInitializeExceptionChain+0x35 (C:\Windows\SysWOW64\ntdll.dll+0x39804)

0x11e00030 is located 0 bytes to the right of 32-byte region [0x11e00010,0x11e00030)
allocated by thread T0 here:
    #0 0x1fae9a in operator new C:\src\llvm_package_1000-final\llvm-project\compiler-rt\lib\asan\asan_new_delete.cpp:99
    #1 0x1699b3 in std::_Allocate C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:93
    #2 0x1a24a5 in std::allocator<long long>::allocate C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\xmemory0:946
    #3 0x1a1081 in std::vector<long long,std::allocator<long long> >::_Buy C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:1978
    #4 0x157ba8 in std::vector<long long,std::allocator<long long> >::vector C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.11.25503\include\vector:696
    #5 0x15183a in main K:\invoker-prod\work\codeforces6\9b5b62769a35581e24568bbc4cf484df\compile-e4b70584810488eac396a75d667b010b\p71.cpp:24
    #6 0x1fc88f in __scrt_common_main_seh f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:283
    #7 0x756b343c in BaseThreadInitThunk+0x11 (C:\Windows\syswow64\kernel32.dll+0x1343c)
    #8 0x77da9831 in RtlInitializeExceptionChain+0x62 (C:\Windows\SysWOW64\ntdll.dll+0x39831)
    #9 0x77da9804 in RtlInitializeExceptionChain+0x35 (C:\Windows\SysWOW64\ntdll.dll+0x39804)

SUMMARY: AddressSanitizer: heap-buffer-overflow K:\invoker-prod\work\codeforces6\9b5b62769a35581e24568bbc4cf484df\compile-e4b70584810488eac396a75d667b010b\p71.cpp:35 in main
Shadow bytes around the buggy address:
  0x323bffb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x323bffc0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x323bffd0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x323bffe0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x323bfff0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x323c0000: fa fa 00 00 00 00[fa]fa 00 00 00 00 fa fa 00 00
  0x323c0010: 00 00 fa fa 00 00 00 fa fa fa 00 00 00 fa fa fa
  0x323c0020: 00 00 00 00 fa fa 00 00 00 fa fa fa 00 00 02 fa
  0x323c0030: fa fa 00 00 07 fa fa fa 00 00 00 06 fa fa 00 00
  0x323c0040: 00 fa fa fa 00 00 00 01 fa fa 00 00 06 fa fa fa
  0x323c0050: 00 00 01 fa fa fa 00 00 00 05 fa fa 00 00 00 03
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==3480==ABORTING

请帮我解决一下这个。谢谢

标签: c++vectorstlruntime-errormultimap

解决方案


你必须添加vector头文件#include <vector>。在那之后你会得到TLE。vector是我通过添加标题提交的代码。


推荐阅读