Bzoj2708 [VIOLET 1] puppet

Source: Internet
Author: User

The first thing that comes to mind is greed... Definitely not... T

Then we found that it was actually DP-enabled... However, we need to sort them first.

If f [I] indicates that the first puppet has to lose a few at worst

F [I] = max (F [J] + calc (J + 1, I) where j <I

 

The calc (x, y) function compute can discard a maximum of several matching functions from the X to the Y puppet. The method is as follows:

Hzwer: "enumeration can throw the number k to determine whether the remaining can match each other, cannot return K-1

And whether the thrown out can match each other, can match return K-1"

 

 1 /************************************************************** 2     Problem: 2708 3     User: rausen 4     Language: C++ 5     Result: Accepted 6     Time:24 ms 7     Memory:804 kb 8 ****************************************************************/ 9  10 #include <cstdio>11 #include <cstring>12 #include <algorithm>13  14 using namespace std;15 const int N = 55;16 int n, a[N], f[N];17  18 inline int read(){19     int x = 0, sgn = 1;20     char ch = getchar();21     while (ch < ‘0‘ || ch > ‘9‘){22         if (ch == ‘-‘) sgn = -1;23         ch = getchar();24     }25     while (ch >= ‘0‘ && ch <= ‘9‘){26         x = x * 10 + ch - ‘0‘;27         ch = getchar();28     }29     return sgn * x;30 }31  32 int calc(int x, int y){33     int i, k;34     for (k = 1; k <= y - x + 1; ++k){35         for (i = k; i <= y - x; ++i)36             if (abs(a[i + x] - a[i + x - k]) > 1) return k - 1;37         if (abs(a[x + k - 1] - a[y - k + 1]) <= 1) return k - 1;38     }39     return y - x + 1;40 }41  42 int main(){43     int i, j;44     while (scanf("%d", &n) != EOF){45         for (i = 1; i <= n; ++i)46             scanf("%d", a + i);47         sort(a + 1, a + n + 1);48         memset(f, 0, sizeof(f));49         for (i = 1; i <= n; ++i)50             for (j = 0; j < i; ++j)51                 f[i] = max(f[i], f[j] + calc(j + 1, i));52         printf("%d\n", f[n]);53     }54     return 0;55 }
View code

 

Bzoj2708 [VIOLET 1] puppet

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: info-contact@alibabacloud.com 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.