Submission #129935

# Submission time Handle Problem Language Result Execution time Memory
129935 2019-07-13T14:48:42 Z nikolapesic2802 Sequence (BOI14_sequence) C++14
67 / 100
1000 ms 28096 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 update(string tr)
{
    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,bool trebaNula)
{
    if(sol!=no&&tr.size()>sol.size())
    if(tr.size()>10)
        return;
    if(ostalo==0)
    {
        if(tr[0]=='0')
        {
            if(trebaNula)
                update("1"+tr);
            else{
                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(i==0&&cnt==1)
                    trebaNula=true;
            }
        }
        if(tr[0]=='0')
        {
            if(trebaNula)
                update("1"+tr);
            else{
                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;
        bool nula=false;
        if(i==0&&(cnt.front()==0||trebaNula))
            nula=true;
        build(min(n,first+interval*(9-i)),min(lim,interval*10),trr+tr,ostalo-uzeo,nula);
        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,0);
    cout << sol << endl;
    return 0;
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:228:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%i",&n);
     ~~~~~^~~~~~~~~
sequence.cpp:231: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 51 ms 26052 KB Output is correct
2 Correct 75 ms 26024 KB Output is correct
3 Correct 55 ms 26144 KB Output is correct
4 Correct 55 ms 26052 KB Output is correct
5 Correct 52 ms 26148 KB Output is correct
6 Correct 53 ms 26048 KB Output is correct
7 Correct 79 ms 26116 KB Output is correct
8 Correct 57 ms 26148 KB Output is correct
9 Correct 53 ms 26408 KB Output is correct
10 Correct 109 ms 26048 KB Output is correct
11 Correct 98 ms 26144 KB Output is correct
12 Correct 57 ms 26144 KB Output is correct
13 Correct 60 ms 26148 KB Output is correct
14 Correct 107 ms 26048 KB Output is correct
15 Correct 111 ms 26176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 26184 KB Output is correct
2 Correct 76 ms 26076 KB Output is correct
3 Correct 57 ms 26148 KB Output is correct
4 Correct 57 ms 26024 KB Output is correct
5 Correct 52 ms 26060 KB Output is correct
6 Correct 54 ms 26132 KB Output is correct
7 Correct 59 ms 26148 KB Output is correct
8 Correct 53 ms 26148 KB Output is correct
9 Correct 57 ms 26060 KB Output is correct
10 Correct 52 ms 26048 KB Output is correct
11 Correct 78 ms 26076 KB Output is correct
12 Correct 101 ms 26144 KB Output is correct
13 Correct 97 ms 26048 KB Output is correct
14 Correct 57 ms 26048 KB Output is correct
15 Correct 61 ms 26072 KB Output is correct
16 Correct 108 ms 26180 KB Output is correct
17 Correct 144 ms 26120 KB Output is correct
18 Correct 58 ms 26052 KB Output is correct
19 Correct 69 ms 26016 KB Output is correct
20 Correct 112 ms 26048 KB Output is correct
21 Correct 71 ms 26200 KB Output is correct
22 Correct 115 ms 26120 KB Output is correct
23 Correct 116 ms 26172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 26048 KB Output is correct
2 Correct 126 ms 26052 KB Output is correct
3 Correct 127 ms 26072 KB Output is correct
4 Correct 123 ms 26024 KB Output is correct
5 Correct 125 ms 26112 KB Output is correct
6 Correct 97 ms 26024 KB Output is correct
7 Correct 538 ms 28052 KB Output is correct
8 Correct 431 ms 27804 KB Output is correct
9 Correct 857 ms 28096 KB Output is correct
10 Correct 847 ms 28056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 26052 KB Output is correct
2 Correct 75 ms 26052 KB Output is correct
3 Correct 57 ms 26048 KB Output is correct
4 Correct 56 ms 26020 KB Output is correct
5 Execution timed out 1072 ms 26748 KB Time limit exceeded
6 Halted 0 ms 0 KB -