CodeChef上的运行时错误:SIGSEGV-CodeChef

时间:2020-04-27 04:45:28

标签: c++ algorithm

我正在解决以下问题:https://www.codechef.com/LTIME83B/problems/FFL

但是我得到RE (SIGSEGV)。我不明白,为什么我明白了?

我认为我没有超出数组的限制。

那我为什么会收到这个错误?

我的代码:

#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#define ll long long
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        s = 100 - s;
        ll i, a[n], k;
        vector<ll> g1;
        vector<ll> g2;
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (i = 0; i < n; i++)
        {
            cin >> k;
            if (k == 0)
            {
                g1.push_back(a[i]);
            }
            if (k == 1)
            {
                g2.push_back(a[i]);
            }
        }
        ll o = *min_element(g1.begin(), g1.end());
        ll p = *min_element(g2.begin(), g2.end());
        if (s >= o + p)
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no"<<endl;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

您没有处理所有可用球员都是防守者或进攻者的情况。

#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#define ll long long
using namespace std;


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll t;
    cin >> t;
    while (t--)
    {
        ll n, s;
        cin >> n >> s;
        s = 100 - s;
        ll i, a[n], k;
        vector<ll> g1;
        vector<ll> g2;
        for (i = 0; i < n; i++)
        {
            cin >> a[i];
        }
        for (i = 0; i < n; i++)
        {
            cin >> k;
            if (k == 0)
            {
                g1.push_back(a[i]);
            }
            if (k == 1)
            {
                g2.push_back(a[i]);
            }
        }
        if(g1.size() == 0 || g2.size() == 0){
            cout<<"no\n";
            continue;
        }
        ll o = *min_element(g1.begin(), g1.end());
        ll p = *min_element(g2.begin(), g2.end());
        if (s >= o + p)
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no"<<endl;
        }
    }
    return 0;
}

尝试提交此代码。

相关问题