• DP细节,上下夹逼 cf-Summer Reading


    Summer Reading
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    At school Vasya got an impressive list of summer reading books. Unlike other modern schoolchildren, Vasya loves reading, so he read some book each day of the summer.

    As Vasya was reading books, he was making notes in the Reader's Diary. Each day he wrote the orderal number of the book he was reading. The books in the list are numbered starting from 1 and Vasya was reading them in the order they go in the list. Vasya never reads a new book until he finishes reading the previous one. Unfortunately, Vasya wasn't accurate and some days he forgot to note the number of the book and the notes for those days remained empty.

    As Vasya knows that the literature teacher will want to check the Reader's Diary, so he needs to restore the lost records. Help him do it and fill all the blanks. Vasya is sure that he spends at least two and at most five days for each book. Vasya finished reading all the books he had started. Assume that the reading list contained many books. So many, in fact, that it is impossible to read all of them in a summer. If there are multiple valid ways to restore the diary records, Vasya prefers the one that shows the maximum number of read books.

    Input

    The first line contains integer n — the number of summer days (2 ≤ n ≤ 2·105). The second line contains n integers a1, a2, ... an — the records in the diary in the order they were written (0 ≤ ai ≤ 105). If Vasya forgot to write the number of the book on the i-th day, then ai equals 0.

    Output

    If it is impossible to correctly fill the blanks in the diary (the diary may contain mistakes initially), print "-1".

    Otherwise, print in the first line the maximum number of books Vasya could have read in the summer if we stick to the diary. In the second line printn integers — the diary with correctly inserted records. If there are multiple optimal solutions, you can print any of them.

    Sample test(s)
    input
    7
    0 1 0 0 0 3 0
    output
    3
    1 1 2 2 3 3 3
    input
    8
    0 0 0 0 0 0 0 0
    output
    4
    1 1 2 2 3 3 4 4
    input
    4
    0 0 1 0
    output
    1
    1 1 1 1
    input
    4
    0 0 0 3
    output
    -1
    #include<iostream>
    using namespace std;
    int a[200010],cnt[100002],n;
    pair<int,int> Max[200010],Min[200010];
    
    int main()
    {
        ios::sync_with_stdio(0); 
        cin>>n;
        for(int i=1;i<=n;++i)
            cin>>a[i];
        if(a[1]>1)  cout<<"-1"<<endl;
        else{
            bool flag=1;
            Max[1]=Min[1]=make_pair(1,1);
            for(int i=2;i<=n&&flag;++i)
            {
                if(Max[i-1].second>=2)  Max[i]=make_pair(Max[i-1].first+1,1);
                else Max[i]=make_pair(Max[i-1].first,Max[i-1].second+1);
                                                            // 上一本书数目大于2。能过,不断地加 112233  小于二,则添加
                if(Min[i-1].second==5)  Min[i]=make_pair(Min[i-1].first+1,1);
                else Min[i]=make_pair(Min[i-1].first,Min[i-1].second+1); 
                                                          //  i[1]=5  满书,放下一个   111112222233333
                if(a[i]){                                          //a[i]==0则不用管了。。刚开始我卡了一阵。。样例不过。。
                    if(a[i]<Min[i].first || a[i]>Max[i].first) flag=0;                //cout<<"1"<<endl;}
                    else{ 
                        Max[i]=min(Max[i],make_pair(a[i],5));
                        Min[i]=max(Min[i],make_pair(a[i],1));
                    }  // 状态转移 
                }
            }
            if(flag)
            {
                pair<int,int> ans = Max[n];
                if(ans.second==1)  ans=make_pair(ans.first-1,5);  // 最后一天要大于等于2 , 这个测试数据中有提示。。我搞了好久~~
                if(ans<Min[n])  cout<<-1<<endl;
                else{
                    cout<<ans.first<<endl;
                    a[n]=ans.first;
                    cnt[a[n]]=1;
                    for(int i=n-1;i;--i)
                    {
                        a[i]=min(Max[i].first,a[i+1]);  //// 两种选择 
                        if(cnt[a[i]]==5)  a[i]--;   /////maxVal一定不会,但是a[]中超过,则要手动操作  
                        cnt[a[i]]++;
                    }
                    for(int i=1;i<=n;++i)
                        cout<<a[i]<<' ';
                    cout<<endl;
                } 
            }
            else cout<<-1<<endl;
        }    
        return 0;
    }
  • 相关阅读:
    Tarjan在图论中的应用(三)——用Tarjan来求解2-SAT
    【CodeForces】CodeForcesRound594 Div1 解题报告
    JS面向对象组件(三)--面向对象中的常用属性和方法
    JS面向对象组件(二)--Javascript原型链
    JS面向对象组件(一) ---包装对象与原型链
    JS面向对象组件(六) -- 拖拽功能以及组件的延展
    面试题目
    webstorm安装破解版
    面试题整理
    Javascript模块化编程(三):require.js的用法 (转)
  • 原文地址:https://www.cnblogs.com/tinyork/p/3476774.html
Copyright © 2020-2023  润新知