zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    ubuntu12.04 死机 卡屏 画面冻结解决方案
    Install Firefox 20 in Ubuntu 13.04, Ubuntu 12.10, Ubuntu 12.04, Linux Mint 14 and Linux Mint 13 by PPA
    ListView1.SelectedItems.Clear()
    android studio 下载地址
    jquery.slider.js jquery幻灯片测试
    jquery.hovermenu.js
    jquery.tab.js选项卡效果
    适配 placeholder,jquery版
    jquery.autoscroll.js jquery自动滚动效果
    将 Google Earth 地图集成到自己的窗体上的 简单控件
  • 原文地址:https://www.cnblogs.com/tinyork/p/3476774.html
Copyright © 2011-2022 走看看