답안 #1050552

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1050552 2024-08-09T10:53:00 Z sofijavelkovska Stranded Far From Home (BOI22_island) C++17
15 / 100
232 ms 32596 KB
#include <bits/stdc++.h>
using namespace std;

const int MAXN=300000;

int n, m;
int a[MAXN];
vector<int> adj[MAXN];
long long prefix[MAXN];

bool compare(int x, int y)
{
    return a[x]>a[y];
}

long long sum(int l, int r)
{
    if (l==0)
        return prefix[r];

    return prefix[r]-prefix[l-1];
}

vector<bool> brute()
{
    vector<bool> possible(n, 0);
    for (int i=0; i<n; i++)
    {
        priority_queue<pair<int, int> > pq;
        pq.push({-a[i], i});
        long long sum=0;
        bool visited[n]={false};
        while (!pq.empty())
        {
            int x=pq.top().second;
            pq.pop();
            if (x!=i && sum<a[x])
                continue;
            if (visited[x])
                continue;
            visited[x]=true;
            sum=sum+a[x];
            for (auto y : adj[x])
                if (!visited[y])
                    pq.push({-a[y], y});
        }
        if (*min_element(visited, visited+n)==1)
            possible[i]=true;
    }

    return possible;
    /*vector<bool> possible(n, 0);
    for (int i=0; i<n; i++)
    {
        long long sum=a[i];
        int l=i, r=i;
        while (true)
        {
            if (l>0 && sum>=a[l-1])
            {
                sum=sum+a[l-1];
                l=l-1;
            }
            else if (r<n-1 && sum>=a[r+1])
            {
                sum=sum+a[r+1];
                r=r+1;
            }
            else
                break;
        }
        if (l==0 && r==n-1)
            possible[i]=true;
    }

    return possible;*/
}

int main()
{
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);

    srand(time(0));
    int tc=0;
    //cin >> tc;
    //while (true)
    {
        /*tc=tc+1;
        cout << "tc " << tc << '\n';
        n=rand()%100+1, m=0;
        for (int i=0; i<n; i++)
            a[i]=rand()%100000+1;*/
        //for (int i=0; i<n; i++)
          //  cout << a[i] << ' ';
        //cout << '\n';

    cin >> n >> m;
    for (int i=0; i<n; i++)
        cin >> a[i];
    for (int i=0; i<n; i++)
        adj[i].clear();
    /*for (int i=0; i<n-1; i++)
    {
        adj[i].push_back(i+1);
        adj[i+1].push_back(i);
    }*/
    for (int i=0; i<m; i++)
    {
        int x, y;
        cin >> x >> y;
        adj[x-1].push_back(y-1);
        adj[y-1].push_back(x-1);
    }
    int sorted[n];
    for (int i=0; i<n; i++)
        sorted[i]=i;
    sort(sorted, sorted+n, compare);
    int possible[n]={false};
    possible[sorted[0]]=true;
    set<int> larger;
    larger.insert(sorted[0]);
    prefix[0]=a[0];
    for (int i=1; i<=n; i++)
        prefix[i]=prefix[i-1]+a[i];
    for (int i=1; i<n; i++)
    {
        int x=sorted[i];
        int l=-1, r=n;
        auto it=larger.lower_bound(x);
        if (it!=larger.end())
            r=*it;
        if (it!=larger.begin())
        {
            it--;
            l=*it;
        }
        if (l!=-1 && possible[l] && sum(l+1, r-1)>=a[l])
            possible[x]=true;
        if (r!=n && possible[r] && sum(l+1, r-1)>=a[r])
            possible[x]=true;
        larger.insert(x);
    }
    for (int i=0; i<n; i++)
        cout << possible[i];

        /*auto br=brute();
        bool flop=false;
        for (int i=0; i<n; i++)
            if (possible[i]!=br[i])
                flop=true;
        if (flop)
        {
            cout << "FLOP\n";
            cout << n << '\n';
            for (int i=0; i<n; i++)
                cout << a[i] << ' ';
            cout << '\n';
            for (int i=0; i<n; i++)
                cout << possible[i];
            cout << '\n';
            for (int i=0; i<n; i++)
                cout << br[i];
            cout << '\n';
            break;
        }*/
    }

    return 0;
}

Compilation message

island.cpp: In function 'int main()':
island.cpp:85:9: warning: unused variable 'tc' [-Wunused-variable]
   85 |     int tc=0;
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 2 ms 8796 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Incorrect 180 ms 28244 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 212 ms 28160 KB Output is correct
3 Correct 232 ms 28244 KB Output is correct
4 Correct 147 ms 32596 KB Output is correct
5 Correct 121 ms 31060 KB Output is correct
6 Correct 195 ms 32592 KB Output is correct
7 Correct 185 ms 32556 KB Output is correct
8 Correct 192 ms 32592 KB Output is correct
9 Correct 145 ms 31780 KB Output is correct
10 Correct 150 ms 31824 KB Output is correct
11 Correct 134 ms 31056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Incorrect 164 ms 28088 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Incorrect 2 ms 8796 KB Output isn't correct
5 Halted 0 ms 0 KB -