Lightoj 1422 Halloween Costumes

Source: Internet
Author: User

Topic Links:

Simulating the whole process is a stack of operations it's hard to think of the interval $dp$

But think about it. You might find that for a stack of elements there is obviously a stack of elements behind it.

Then there are a bunch of elements in the stack after the stack $......$

We can then divide the interval according to the stack bottom element and then once again become the top element of the stack.

$ ($ in fact that is the test instructions to put on a piece of clothing and once again make it the outermost piece $) $

So the transfer equation can be written as

$f [I][j] = F[i + 1][j] + 1 ($ $i$ Party costume not used multiple times $) $

$f [I][j] = min (f[i][k-1] + f[k + 1][j]) ($ $i$ Party costumes used in the first $k$ of the party are reused $) $

1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 intc[ the], f[ the][ the];7 intt, N;8 intMain ()9 {Tenscanf"%d", &t); One      for(intCA =1; CA <= T; ++CA) A     { -scanf"%d", &n); -          for(inti =1; I <= N; ++i) thescanf"%d", &c[i]); -          for(inti =1; I <= N; ++i) -F[i][i] =1; -          for(intLen =1; Len <= N; ++len) +              for(inti =1; i + Len-1<= N; ++i) -             { +                 intj = i + len-1; AF[I][J] = f[i +1][J] +1; at                      for(intK = i +1; K <= J; ++k) -                         if(C[i] = =C[k]) -F[i][j] = min (f[i][j], f[i][k-1] + f[k +1][j]); -             } -printf"Case %d:%d\n", CA, f[1][n]); -     } in     return 0; -}

Lightoj 1422 Halloween Costumes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.