Submission #582592

# Submission time Handle Problem Language Result Execution time Memory
582592 2022-06-24T07:00:58 Z 조영욱(#8372) Fibonacci representations (CEOI18_fib) C++17
50 / 100
339 ms 424 KB
#include <bits/stdc++.h>
using namespace std;

int n;
vector<int> v;
typedef pair<int,int> P;
set<P> s;
int val[100000];
set<P> del;
vector<long long> vec;
const int mod=1e9+7;
long long dp[105][2];

void insert(P x) {
    val[x.second]=x.first;
    auto it2=s.lower_bound(x);
    auto it1=prev(it2);
    long long d=(*it2).first-(*it1).first;
    del.erase(P(d,(*it1).second));
    d=x.first-(*it1).first;
    del.insert(P(d,(*it1).second));
    d=(*it2).first-x.first;
    del.insert(P(d,x.second));
    s.insert(x);
}

void erase(P x) {
    auto it=s.lower_bound(x);
    P v1=*prev(it);
    P v2=*next(it);
    long long d=x.first-v1.first;
    del.erase(P(d,v1.second));
    d=v2.first-x.first;
    del.erase(P(d,x.second));
    d=v2.first-v1.first;
    del.insert(P(d,v1.second));
    s.erase(it);
}

long long ans(int ind,int type) {
    if (ind==0) {
        if (type==0) {
            return (vec[0]+1)/2;
        }
        return (vec[0]-1)/2;
    }
    if (dp[ind][type]!=-1) {
        return dp[ind][type];
    }
    if (type==1) {
        return dp[ind][type]=(ans(ind,0)-ans(ind-1,0)+mod)%mod;
    }
    long long d=vec[ind]-vec[ind-1];
    if (d%2==0) {
        return dp[ind][type]=((d/2)*ans(ind-1,0)+ans(ind-1,1))%mod;
    }
    return dp[ind][type]=((d/2+1)*ans(ind-1,0))%mod;
}

int main(void) {
    scanf("%d",&n);
    for(int ind=0;ind<n;ind++) {
        int x;
        scanf("%d",&x);
        v.push_back(x);
        s.clear();
        del.clear();
        for(int i=0;i<v.size();i++) {
            s.insert(P(v[i],i));
            val[i]=v[i];
        }
        s.insert(P(2e9+7,-1));
        s.insert(P(-10,-1));
        for(auto iter=s.begin();iter!=s.end();iter++) {
            if (iter!=s.begin()) {
                P now=(*iter);
                P pr=(*prev(iter));
                del.insert(P(now.first-pr.first,pr.second));
            }
        }
        while (1) {
            P got=(*del.begin());
            if (got.first>1) {
                break;
            }
            if (got.first==0) {
                auto it=s.lower_bound(P(val[got.second],got.second));
                P one=(*it);
                it=next(it);
                P two=(*it);
                erase(one);
                erase(two);
                //printf("%d %d %d %d\n",one.first,one.second,two.first,two.second);
                if (one.first==1) {
                    val[one.second]=2;
                    val[two.second]=0;
                    insert(P(2,one.second));
                }
                else if (one.first==2) {
                    val[one.second]=3;
                    val[two.second]=1;
                    insert(P(3,one.second));
                    insert(P(1,two.second));
                }
                else {
                    val[one.second]=one.first+1;
                    val[two.second]=two.first-2;
                    insert(P(one.first+1,one.second));
                    insert(P(two.first-2,two.second));
                }
            }
            else {
                auto it=s.lower_bound(P(val[got.second],got.second));
                P one=(*it);
                it=next(it);
                P two=(*it);
                erase(one);
                erase(two);
                val[one.second]=one.first+2;
                val[two.second]=0;
                insert(P(one.first+2,one.second));
            }
        }
        s.erase(P(2e9+7,-1));
        s.erase(P(-10,-1));
        vec.clear();
        for(auto now:s) {
            //printf("%lld ",now.first);
            vec.push_back(now.first);
        }
        //printf("\n");
        sort(vec.begin(),vec.end());
        memset(dp,-1,sizeof(dp));
        printf("%lld\n",ans(vec.size()-1,0));
    }
}

Compilation message

fib.cpp: In function 'int main()':
fib.cpp:68:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |         for(int i=0;i<v.size();i++) {
      |                     ~^~~~~~~~~
fib.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
fib.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |         scanf("%d",&x);
      |         ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 17 ms 212 KB Output is correct
8 Correct 45 ms 316 KB Output is correct
9 Correct 62 ms 296 KB Output is correct
10 Correct 35 ms 312 KB Output is correct
11 Correct 66 ms 296 KB Output is correct
12 Correct 303 ms 296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 17 ms 212 KB Output is correct
8 Correct 45 ms 316 KB Output is correct
9 Correct 62 ms 296 KB Output is correct
10 Correct 35 ms 312 KB Output is correct
11 Correct 66 ms 296 KB Output is correct
12 Correct 303 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 212 KB Output is correct
15 Correct 8 ms 212 KB Output is correct
16 Correct 44 ms 212 KB Output is correct
17 Correct 2 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 339 ms 424 KB Output is correct
20 Correct 83 ms 212 KB Output is correct
21 Correct 44 ms 312 KB Output is correct
22 Correct 25 ms 320 KB Output is correct
23 Correct 23 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 17 ms 212 KB Output is correct
8 Correct 45 ms 316 KB Output is correct
9 Correct 62 ms 296 KB Output is correct
10 Correct 35 ms 312 KB Output is correct
11 Correct 66 ms 296 KB Output is correct
12 Correct 303 ms 296 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 2 ms 212 KB Output is correct
15 Correct 8 ms 212 KB Output is correct
16 Correct 44 ms 212 KB Output is correct
17 Correct 2 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 339 ms 424 KB Output is correct
20 Correct 83 ms 212 KB Output is correct
21 Correct 44 ms 312 KB Output is correct
22 Correct 25 ms 320 KB Output is correct
23 Correct 23 ms 320 KB Output is correct
24 Incorrect 2 ms 340 KB Output isn't correct
25 Halted 0 ms 0 KB -