# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
465007 | Deepesson | Growing Vegetables is Fun 4 (JOI21_ho_t1) | C++17 | 32 ms | 3780 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
int N;
bool valido(long long* seq) {
bool cresce=true;
long long last=0;
for(int i=0;i!=N;++i){
if(seq[i]>last){
if(!cresce)return false;
}else if(seq[i]<last){
if(cresce)cresce=false;
}else {
return false;
}
last=seq[i];
}
return true;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::cin>>N;
long long array[N];
for(auto&x:array)std::cin>>x;
if(N==2){
if(array[0]==array[1]){
printf("1\n");return 0;
}else printf("0\n");return 0;
}
long long cur1 = 1,cur2=N-2,soma=0;
long long total=0;
while(cur1!=cur2){
if(array[cur1-1]<array[cur1]){
++cur1;
array[cur1]+=soma;
continue;
}
if(array[cur2+1]<array[cur2]){
--cur2;
array[cur2]+=soma;
continue;
}
long long to_cur1 = (array[cur1-1]+1LL)-array[cur1];
long long to_cur2 = (array[cur2+1]+1LL)-array[cur2];
long long res = std::min(to_cur1,to_cur2);
array[cur1]+=res;
array[cur2]+=res;
soma+=res;
total+=res;
}
array[cur1]-=soma;
if(valido(array))goto fim;{
long long pico_cresce = (array[cur1-1]+1LL)-array[cur1];
long long pico_desce = (array[cur2+1]+1LL)-array[cur2];
//std::cout<<pico_cresce << " " << pico_desce << "\n";
total+=std::min(pico_cresce,pico_desce);
array[cur1]+=std::min(pico_cresce,pico_desce);
while(!valido(array)){
++array[cur1];
++total;
}}
fim:
std::cout<<total<<"\n";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |