P1415 拆分数列 DP

传送门:

题意:

  将一个数字串分成许多不同的小串,使得这些小串代表的数字严格递增,要求最后一个数字尽可能地小。

  然后满足字典序尽可能大。

思路:

  由于最后一个数字要尽可能地小,所以先处理出每个数的$L[i]$, 即第i位最少要和$L[i]$这个位子的值组合才能满足要求。求出$L[i]$后,固定好最后的数的大小。

  因为要求前面的数尽可能地大,所以,开始计算$R[i]$,表示第i位最远能和哪个位子的值组合才能满足要求。

 

注意最后一块的前导0要带上。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 9999973;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
/*-----------------------showtime----------------------*/
            const int maxn = 509;

            int dp1[maxn],dp2[maxn];
            char cas[100],num[1009];
            int r[maxn];
            bool check(int l1,int r1,int l2,int r2){
                while(num[l1] == '0' && l1 < r1) l1++;
                while(num[l2] == '0' && l2 < r2) l2++;

                if(r1 - l1 + 1 < r2 - l2 + 1) return true;
                if(r1 - l1 + 1 > r2 - l2 + 1) return false;

                for(int i=l1, j=l2; i<=r1 && j <= r2; i++, j++){
                    if(num[i] > num[j]) return false;
                    else if(num[i] < num[j]) return true;
                }

                return false;
            }
int main(){
                    scanf("%s", num + 1);
                    int n = strlen(num+1);

                    for(int i=1; i<=n; i++){
                        for(int j=1; j<=i; j++){
                            if(j == 1) {dp1[i] = 1;continue;}
                            if(check(dp1[j-1], j-1, j , i)) dp1[i] = j;
                        }
                    }
                    // dp2[i] ~ r[i]
                    int id = dp1[n];
                    while(id && num[id-1] == '0') id--;
                    dp1[n] = id;
                    
                    for(int i= dp1[n]; i<=n; i++)   dp2[i] = n;

                    for(int i=dp1[n]-1; i>=1; i--){
                            for(int j=i; j<dp1[n]; j++)
                            {
                                if(check(i,j,j+1,dp2[j+1])) dp2[i] = j;
                            }
                    }

                    for(int i=dp2[1]; i<n; ) {
                        r[i] = 1;
                        i = dp2[i+1];
                    }
                    for(int i=1; i<=n; i++){
                        printf("%c", num[i]);
                        if(r[i] && i < n) printf(",");
                    }
                    printf("\n");
            return 0;
}
/*
13433723991416664857426899882522765651609851664619848674546418181426101583783039
134,3372,3991,4166,6485,74268,99882,522765,651609,851664,6198486,74546418,181426101,583783039
*/
View Code

 

posted @ 2019-01-28 16:25  ckxkexing  阅读(184)  评论(0编辑  收藏  举报