답안 #1077014

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1077014 2024-08-26T20:43:54 Z lucri Tortoise (CEOI21_tortoise) C++17
0 / 100
0 ms 344 KB
#include <bits/stdc++.h>
using namespace std;
long long cost,n,v[10010],l[10010],r[10010],jump[10010],nr;
priority_queue<pair<int,int>>q;
bool candy(int time,int poz)
{
    bool ok=false;
    for(int i=poz;v[i]!=-1;++i)
        if(v[i]||jump[i])
            ok=true;
    if(ok==false)
        return true;
    for(int i=poz;v[i]!=-1;++i)
        if(v[i]&&time+i-poz<=(i-1)*2)
            return true;
    return false;
}
bool ok()
{
    int poz=1,time=0,zid=-10010;
    if(candy(0,1)==false)
        return false;
    time=r[1]-1;
    for(int i=1;i<=n;++i)
    {
        if(v[i]==-1)
            continue;
        if(r[i]-i==i-l[i]||r[i]-i==(i-1)-l[i])
        {
            if(candy(time-(i-1-l[i])+1,l[i]+1)==false)
                return false;
            time+=r[i]-(i-1);
        }
        if(r[i]-i<=i-l[i])
        {
            for(int j=1;j<=jump[i];++j)
            {
                if(time+r[i]-i>2*(i-1))
                    return false;
                time+=2*(r[i]-i);
            }
        }
        else
        {
            ++time;
            for(int j=1;j<=jump[i];++j)
            {
                if(time>2*(i-1))
                    return false;
                time+=2*(i-l[i]);
            }
        }
    }
    if(candy(time-(n-l[n])+1,l[n]+1)==false)
        return false;
    return true;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
        cin>>v[i];
    v[10010]=-1;
    l[0]=-10010;
    r[n+1]=10010;
    for(int i=1;i<=n;++i)
    {
        l[i]=l[i-1];
        if(v[i]==-1)
            l[i]=i;
    }
    for(int i=n;i>=1;--i)
    {
        r[i]=r[i+1];
        if(v[i]==-1)
            r[i]=i;
    }
    for(int i=1;i<=n;++i)
        q.push({-min(r[i]-i,i-l[i]),i});
    while(!q.empty())
    {
        int ind=q.top().second;
        q.pop();
        if(v[ind]==-1)
            continue;
        while(v[ind])
        {
            --v[ind];
            ++jump[ind];
            if(ok()==false)
            {
                ++v[ind];
                --jump[ind];
                break;
            }
        }
    }
    for(int i=1;i<=10010;++i)
        if(v[i]!=-1)
            nr+=v[i];
        else
        {
            if(nr)
                --cost;
            nr=0;
        }
    for(int i=1;i<=n;++i)
        if(v[i]!=-1)
            cost+=v[i];
    cout<<cost;
    return 0;
}

Compilation message

tortoise.cpp: In function 'bool ok()':
tortoise.cpp:20:9: warning: unused variable 'poz' [-Wunused-variable]
   20 |     int poz=1,time=0,zid=-10010;
      |         ^~~
tortoise.cpp:20:22: warning: unused variable 'zid' [-Wunused-variable]
   20 |     int poz=1,time=0,zid=-10010;
      |                      ^~~
tortoise.cpp: In function 'int main()':
tortoise.cpp:99:15: warning: iteration 10009 invokes undefined behavior [-Waggressive-loop-optimizations]
   99 |         if(v[i]!=-1)
      |            ~~~^
tortoise.cpp:98:18: note: within this loop
   98 |     for(int i=1;i<=10010;++i)
      |                 ~^~~~~~~
tortoise.cpp:63:12: warning: array subscript 10010 is above array bounds of 'long long int [10010]' [-Warray-bounds]
   63 |     v[10010]=-1;
      |     ~~~~~~~^
tortoise.cpp:3:18: note: while referencing 'v'
    3 | long long cost,n,v[10010],l[10010],r[10010],jump[10010],nr;
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 344 KB Output isn't correct
2 Halted 0 ms 0 KB -