AGC029C Lexicographic constraints

The meaning of problems

Given \ (n-\) string length \ (a_i \) , find the minimum number of characters used to construct a lexicographical ordering comparison \ (S1 <S2 <⋯ <Sn \) .
\ (1 \ leq N \ leq 2 \ times 10 ^ 5,1 \ leq a_i \ leq 10 ^ 9 \)

Thinking

You can see the string as a hexadecimal number, if \ (a_ {i + 1} > a_i \) directly complement \ (0 \) , otherwise for a little more than he find the first length, if not long enough to make up \ (0 \) , then \ (+ 1 \) , will look to the last burst mantissa. \ (1 \) characters can be special sentence out, in fact, it can be found in the rest of the \ (map \) Maintenance

Obviously, there may dichotomy, two points to the answer.

#include <bits/stdc++.h>
using namespace std;
const int N=200005;
int n,a[N],ans,f=0;
map <int,int> mp;
bool check(int x){
    mp.clear();
    for (int i=2;i<=n;i++)
        if (a[i-1]>=a[i]){
            while (!mp.empty()){
                int t=mp.rbegin()->first;
                if (t>a[i]) mp.erase(t); 
                else break;
            }
            int j=a[i];
            while (mp[j]+1==x) mp.erase(j),j--;
            if (j==0) return false;
            mp[j]++;
        }
    return true;
}
int main(){
    scanf("%d",&n);
    for (int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        if (a[i]<=a[i-1]) f=1;
    }
    if (!f) {puts("1");return 0;}
    int l=2,r=n;
    while (l<=r){
        int mid=(l+r)>>1;
        if (check(mid)){
            ans=mid;
            r=mid-1;
        }else l=mid+1;
    }
    printf("%d\n",ans);
}

Guess you like

Origin www.cnblogs.com/flyfeather6/p/11760490.html