Submission #129930

# Submission time Handle Problem Language Result Execution time Memory
129930 2019-07-13T14:38:36 Z nikolapesic2802 Sequence (BOI14_sequence) C++14
42 / 100
1000 ms 26244 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>

#define ll long long
#define pb push_back
#define sz(x) (int)(x).size()
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define D(x) cerr << #x << " is " << (x) << "\n";

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>; ///find_by_order(),order_of_key()
template<class T1, class T2> ostream& operator<<(ostream& os, const pair<T1,T2>& a) { os << '{' << a.f << ", " << a.s << '}'; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<T>& a){os << '{';for(int i=0;i<sz(a);i++){if(i>0&&i<sz(a))os << ", ";os << a[i];}os<<'}';return os;}
template<class T> ostream& operator<<(ostream& os, const set<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T> ostream& operator<<(ostream& os, const multiset<T>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}
template<class T1,class T2> ostream& operator<<(ostream& os, const map<T1,T2>& a) {os << '{';int i=0;for(auto p:a){if(i>0&&i<sz(a))os << ", ";os << p;i++;}os << '}';return os;}

//3:20

const int N=1e5+5;
struct node{
    int l,r,sum;
    node(){l=-1;r=-1;sum=0;}
};
struct segTree{
    vector<node> drvo;
    vector<int> roots;
    vector<int> cnt;
    int newNode()
    {
        node a;
        drvo.pb(a);
        return drvo.size()-1;
    }
    int copyNode(int i)
    {
        drvo.pb(drvo[i]);
        return drvo.size()-1;
    }
    void build(int l=0,int r=N-1,int tr=0)
    {
        if(l==r)
            return;
        int m=(l+r)>>1,a=newNode();
        drvo[tr].l=a;
        a=newNode();
        drvo[tr].r=a;
        build(l,m,drvo[tr].l);
        build(m+1,r,drvo[tr].r);
    }
    void init()
    {
        int a=newNode();
        roots.pb(a);
        build();
    }
    void undo()
    {
        for(int i=0;i<cnt.back();i++)
            drvo.pop_back();
        cnt.pop_back();
        roots.pop_back();
    }
    int get(int qs,int qe)
    {
        return gett(qs,qe,roots.back());
    }
    int gett(int qs,int qe,int tr,int l=0,int r=N-1)
    {
        if(qs>r||qe<l||drvo[tr].sum==0)
            return 0;
        if(qs<=l&&qe>=r)
            return drvo[tr].sum;
        int m=(l+r)>>1;
        return gett(qs,qe,drvo[tr].l,l,m)+gett(qs,qe,drvo[tr].r,m+1,r);
    }
    void postavi(int qs,int qe)
    {
        cnt.pb(0);
        int a=sett(qs,qe,roots.back());
        roots.pb(a);
    }
    int sett(int qs,int qe,int tr,int l=0,int r=N-1)
    {
        if(qs>r||qe<l||drvo[tr].sum==0)
            return tr;
        int nov=copyNode(tr);
        cnt.back()++;
        if(qs<=l&&qe>=r)
        {
            drvo[nov].sum=0;
            return nov;
        }
        int m=(l+r)>>1;
        int a=sett(qs,qe,drvo[tr].l,l,m);
        drvo[nov].l=a;
        a=sett(qs,qe,drvo[tr].r,m+1,r);
        drvo[nov].r=a;
        drvo[nov].sum=drvo[drvo[nov].l].sum+drvo[drvo[nov].r].sum;
        return nov;
    }
    void add(int pos,int tr=0,int l=0,int r=N-1)
    {
        if(l>pos||r<pos)
            return;
        if(l==r)
        {
            drvo[tr].sum++;
            return;
        }
        int m=(l+r)>>1;
        add(pos,drvo[tr].l,l,m);
        add(pos,drvo[tr].r,m+1,r);
        drvo[tr].sum=drvo[drvo[tr].l].sum+drvo[drvo[tr].r].sum;
    }
} num[10];
string no=".",sol=".";
int n,lim=1e7;
vector<int> k;
void dodaj(string &num,int pos)
{
    if(num[pos]=='9')
    {
        num[pos]='0';
        dodaj(num,pos-1);
    }
    else
        num[pos]++;

}
bool moze(string num)
{
    num="000000"+num;
    for(int i=0;i<n;i++)
    {
        bool krenuo=false,nasao=false;
        for(auto p:num)
        {
            if(p!='0')
                krenuo=true;
            if(krenuo&&p-'0'==k[i])
            {
                nasao=true;
                break;
            }
        }
        if(!nasao)
            return false;
        dodaj(num,num.size()-1);
    }
    return true;
}
void update(string tr)
{
    if(!moze(tr))
        return;
    if(sol==".")
        sol=tr;
    if(tr.size()<sol.size())
        sol=tr;
    if(tr.size()==sol.size())
        sol=min(sol,tr);
}
void build(int first,int interval,string tr,int ostalo)
{
    if(sol!=no&&tr.size()>sol.size())
    if(tr.size()>10)
        return;
    if(ostalo==0)
    {
        if(tr[0]=='0')
        {
            update("1"+tr);
            while(tr[0]=='0'&&tr.size()>1)
                tr=tr.substr(1,tr.size()-1);
            update(tr);
        }
        else
            update(tr);
        return;
    }
    if(first==n)
    {
        int cnt=0;
        for(int i=9;i>=0;i--)
        {
            int br=num[i].get(0,n-1);
            if(br)
            {
                cnt++;
                char trr='0'+i;
                tr=trr+tr;
                if(i==0&&cnt>1)
                    swap(tr[0],tr[1]);
            }
        }
        if(tr[0]=='0')
        {
            update("1"+tr);
            while(tr[0]=='0'&&tr.size()>1)
                tr=tr.substr(1,tr.size()-1);
            update(tr);
        }
        else
            update(tr);
        return;
    }
    for(int i=0;i<=9;i++)
    {
        vector<int> cnt;
        int uzeo=0;
        if(first!=0)
        {
            int tr=num[i].get(0,first-1);
            uzeo+=tr;
            if(tr)
                num[i].postavi(0,first-1),cnt.pb(i);
        }
        int l=first,r=first+interval-1,j=(i+1)%10;
        while(l<n)
        {
            int tr=num[j].get(l,min(r,n-1));
            uzeo+=tr;
            if(tr)
                num[j].postavi(l,min(r,n-1)),cnt.pb(j);
            l+=interval;
            r+=interval;
            j=(j+1)%10;
        }
        if(cnt.empty())
            continue;
        char trr='0'+i;
        build(min(n,first+interval*(9-i)),min(lim,interval*10),trr+tr,ostalo-uzeo);
        for(auto p:cnt)
            num[p].undo();
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    for(int i=0;i<10;i++)
        num[i].init();
    scanf("%i",&n);
    k.resize(n);
    for(int i=0;i<n;i++)
        scanf("%i",&k[i]),num[k[i]].add(i);
    build(1,1,"",n);
    cout << sol << endl;
    return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:252:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
sequence.cpp:255:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%i",&k[i]),num[k[i]].add(i);
         ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 53 ms 26048 KB Output is correct
2 Correct 136 ms 26052 KB Output is correct
3 Correct 60 ms 26048 KB Output is correct
4 Correct 61 ms 26144 KB Output is correct
5 Correct 51 ms 26016 KB Output is correct
6 Correct 52 ms 26052 KB Output is correct
7 Correct 53 ms 26052 KB Output is correct
8 Correct 81 ms 26048 KB Output is correct
9 Correct 51 ms 26016 KB Output is correct
10 Correct 411 ms 26244 KB Output is correct
11 Correct 381 ms 26048 KB Output is correct
12 Correct 60 ms 26020 KB Output is correct
13 Correct 72 ms 26136 KB Output is correct
14 Correct 558 ms 26044 KB Output is correct
15 Correct 557 ms 26016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 26052 KB Output is correct
2 Correct 136 ms 26148 KB Output is correct
3 Correct 59 ms 26052 KB Output is correct
4 Correct 59 ms 26048 KB Output is correct
5 Correct 52 ms 26020 KB Output is correct
6 Correct 52 ms 26020 KB Output is correct
7 Correct 81 ms 26016 KB Output is correct
8 Correct 53 ms 26160 KB Output is correct
9 Correct 80 ms 26136 KB Output is correct
10 Correct 53 ms 26148 KB Output is correct
11 Correct 315 ms 26048 KB Output is correct
12 Correct 412 ms 26052 KB Output is correct
13 Correct 383 ms 26016 KB Output is correct
14 Correct 60 ms 26148 KB Output is correct
15 Correct 70 ms 26072 KB Output is correct
16 Correct 551 ms 26016 KB Output is correct
17 Correct 550 ms 26144 KB Output is correct
18 Correct 65 ms 26160 KB Output is correct
19 Correct 111 ms 26056 KB Output is correct
20 Correct 524 ms 26096 KB Output is correct
21 Correct 101 ms 26144 KB Output is correct
22 Correct 595 ms 26180 KB Output is correct
23 Correct 590 ms 26052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 26020 KB Output is correct
2 Execution timed out 1079 ms 26056 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 52 ms 26020 KB Output is correct
2 Correct 135 ms 26052 KB Output is correct
3 Correct 60 ms 26204 KB Output is correct
4 Correct 60 ms 26116 KB Output is correct
5 Execution timed out 1089 ms 26028 KB Time limit exceeded
6 Halted 0 ms 0 KB -