Uvs-111 history grading (LIS)

Source: Internet
Author: User

Question: Ultraviolet A-111 history grading (LIS)


Find the longest Lis. However, the question is really wrong.

For example, 3 1 2 4 9 5 10 6 8 7 indicates that the first historical event is ranked third, and the second historical event is ranked 1st, first, sort the event in chronological order. New Order: 2 3 1 4 6 8 10 9 5 7.


Solution: Lis.


Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 25;const int maxn = 10005;int n;int f[N];int temp[N];char str[maxn];void translate (int * num) {memcpy (temp, num, n * sizeof (int));for (int i = 0; i < n; i++) num[temp[i] - 1] = i;}void handle (int * num) {int cnt = 0;int ans = 0;for (int i = 0; i <= strlen (str); i++) {if (str[i] >= '0' && str[i] <= '9') {ans = ans * 10 + str[i] - '0';} else {num[cnt++] = ans;ans = 0;}}translate (num);}int main () {int num[N];int vis[N];int num1[N];bool flag = 0;scanf ("%d%*c", &n);while (gets(str) != NULL) {if (!flag) {handle(num);flag = 1;for (int i = 0; i < n; i++)vis[num[i]] = i;} else {handle(num1);for (int i = 0; i < n; i++) {num1[i] = vis[num1[i]];f[i] = 1;}for (int i = 0; i < n; i++)for (int j = i - 1; j >= 0; j--) {if (num1[i] > num1[j])if (f[j] + 1 > f[i])f[i] = f[j] + 1;}int ans = 0;for (int i = 0; i < n; i++)if (ans < f[i])ans = f[i];printf ("%d\n", ans);}}return 0;}




Uvs-111 history grading (LIS)

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.