zoukankan      html  css  js  c++  java
  • 【SGU 390】Tickets (数位DP)

     

    Description



    Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor sells several tickets at a time to each passenger. More precisely, he successively gives tickets to the passenger until the sum of the digits on all the tickets given becomes not less than some integer number k. Then this process repeats for the next passenger. Initially conductor has a tape of tickets numbered successively from l to r, inclusive. This way of tickets distribution is quite good, because passengers are glad to get several tickets when they pay only for one. But there is one disadvantage. Since each passenger gets several tickets, it is possible that conductor won't be able to serve all passengers. Your task is to help conductor in this difficult situation. You should calculate how many passengers is the conductor able to serve.

    Input

    Input file contains three integer numbers lr and k (1 ≤ l ≤ r ≤ 10 18, 1 ≤ k ≤ 1000).

    Output

    Output should contain exactly one number — the answer to the problem.

    Sample Input

    sample input
    sample output
    40 218 57
    
    29

    【题意】

      有一位售票员给乘客售票。对于每位乘客,他会卖出多张连续的票,直到已卖出的票的编号的数位之和不小于给定的正数 k。然后他会按照相同的规则给下一位乘客售票。初始时,售票员持有的票的编号是从 L 到 R 的连续整数。请你求出,售票员可以售票给多少位乘客。

    【分析】

      这题真难想啊,光是理解题解就理解了好久。。

      这题跟前一题是相似的,都是划分区间,但是这题k很小,意味着区间很多,不能一个个区间跑的。这能模拟填数得到答案。模拟填数的话画一个树形图很重要。

      一开始觉得画不画树都没什么关系,但是这题画了树真的好理解很多。因为这一题不能用ans[r]-ans[l],只能DP填l~r之间的数,而且要按从小到大的顺序,所以画树形图就很好看,可以拆成多个子树的并。

      以下图为例,(假设是棵二叉树,好画点,做题的时候实际上是十叉的)。

      

     红色路径是L,绿色路径是R。我们填数实际上是从左边的绿色子树一直到右边的蓝色子树。

     把原问题分解成多棵子树的子问题在并起来就好了。时间就会很快,是2*logn。

    问题就是怎么把子树并起来,一开始会以为不可以,因为后子树的答案和前子树有关。但是我们注意到k很小,不妨记录一下前子树最后一个区间的空间,然后求后子树的答案。

    然后这个子树其实是填数过程的后缀那部分的数,我们知道子树的位置所以也知道填数部分的前缀,这个对求子树答案是有影响的,我们子树上的每个数都要加上一个固定的数,所以也把他记录一下。即f[i][j][k]表示高度为i的子树,前子树最后一组剩余空间为j,每个数都要加上k时的答案(我们当作这棵子树的根权值为1,如果不为1而为i则把i加进k里面)。

    f[[i][j][k]要记录分了多少组和最后一部分剩余多少,分别记为f[i][j][k].a,f[i][j][k].b

    则f[i][j][k]=并(f[i][j][k],f[i-1][f[i][j][k].a][k+l]) (0<=l<=9)

    并的话就很容易啦~~~

    按顺序求答案就好啦~~

    真是太厉害了,其实也不是很难,怎么我想不到,可能是因为之前都是做ans[r]-ans[l]的吧!!

    代码如下:

      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<cmath>
      8 using namespace std;
      9 #define LL long long
     10 
     11 LL l,r;
     12 int k,st;
     13 int al[20],ar[20];
     14 int hl[20],hr[20];
     15 
     16 void init()
     17 {
     18     for(int i=1;i<=18;i++)
     19     {
     20         al[i]=(int)l%10;l/=10;
     21         ar[i]=(int)r%10;r/=10;
     22     }
     23     for(st=18;st>=1;st--) if(al[st]!=ar[st]) break;
     24     st++;
     25     hl[19]=hr[19]=0;
     26     for(int i=18;i>=1;i--) 
     27       hl[i]=hl[i+1]+al[i],hr[i]=hr[i+1]+ar[i];
     28 }
     29 
     30 struct node
     31 {
     32     LL sm;
     33     int rm;
     34     node ()
     35     {
     36         sm=-1;
     37     }
     38 };
     39 node f[20][210][1010];
     40 node ans,rm;
     41 
     42 node get_ans(node x,node y)//combine
     43 {
     44     x.sm+=y.sm;
     45     x.rm=y.rm;
     46     return x;
     47 }
     48 
     49 node get_f(int x,int y,int z)//x->height y->add z->room
     50 {
     51     if(f[x][y][z].sm!=-1) return f[x][y][z];
     52     node now;
     53     if(x==1)
     54     {
     55         now.sm=(y+z>=k)?1:0;
     56         now.rm=(y+z>=k)?0:y+z;
     57     }
     58     else
     59     {
     60         now=get_f(x-1,y,z);
     61         for(int i=1;i<10;i++)
     62         {
     63             now=get_ans(now,get_f(x-1,y+i,now.rm));
     64         }
     65     }
     66     return now;
     67 }
     68 
     69 void ffind()
     70 {
     71     ans.sm=(hl[1]>=k)?1:0;ans.rm=(hl[1]>=k)?0:hl[1];
     72     for(int i=2;i<st;i++)
     73     {
     74         for(int j=al[i-1]+1;j<10;j++)
     75         {
     76             ans=get_ans(ans,get_f(i-1,hl[i]+j,ans.rm));
     77         }
     78     }
     79     for(int i=al[st-1]+1;i<ar[st-1];i++)
     80     {
     81         ans=get_ans(ans,get_f(st-1,hl[st]+i,ans.rm));
     82     }int i;
     83     for(i=st-1;i>=1;i--)
     84     {
     85         for(int j=0;j<ar[i-1];j++)
     86         {
     87             ans=get_ans(ans,get_f(i-1,hr[i]+j,ans.rm));
     88         }
     89     }
     90     if(hr[1]+ans.rm>=k) ans.sm++;
     91     printf("%lld
    ",ans.sm);
     92 }
     93 
     94 int main()
     95 {
     96     freopen("a.in","r",stdin);
     97     freopen("a.out","w",stdout);
     98     scanf("%lld%lld%d",&l,&r,&k);
     99     init();
    100     ffind();
    101     return 0;
    102 }
    [SGU 390]

    【其实我没有交,交不了...但是拍了感觉没什么问题..吧


    还是放大神题解吧:

  • 相关阅读:
    Code Review
    关于calendar修改前的代码和修改后的代码
    程序员必备的代码审查(Code Review)清单
    一个数组中最大子数组的和并且加上测试用例
    阅读build to win的个人感想
    结对编码(柳祎、张许君)
    Review(patener)
    Review [myself]
    Impressions
    Array
  • 原文地址:https://www.cnblogs.com/Konjakmoyu/p/5954545.html
Copyright © 2011-2022 走看看