zoukankan      html  css  js  c++  java
  • POJ1692 Crossed Matchings

    Crossed Matchings
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 2954   Accepted: 1918

    Description

    There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segment an r-matching segment. The following figure shows a 3-matching and a 2-matching segment.

    We want to find the maximum number of matching segments possible to draw for the given input, such that:
    1. Each a-matching segment should cross exactly one b-matching segment, where a != b .
    2. No two matching segments can be drawn from a number. For example, the following matchings are not allowed.

    Write a program to compute the maximum number of matching segments for the input data. Note that this number is always even.

    Input

    The first line of the input is the number M, which is the number of test cases (1 <= M <= 10). Each test case has three lines. The first line contains N1 and N2, the number of integers on the first and the second row respectively. The next line contains N1 integers which are the numbers on the first row. The third line contains N2 integers which are the numbers on the second row. All numbers are positive integers less than 100.

    Output

    Output should have one separate line for each test case. The maximum number of matching segments for each test case should be written in one separate line.

    Sample Input

    3
    6 6
    1 3 1 3 1 3
    3 1 3 1 3 1
    4 4
    1 1 3 3 
    1 1 3 3 
    12 11
    1 2 3 3 2 4 1 5 1 3 5 10
    3 1 2 3 2 4 12 1 5 5 3 
    

    Sample Output

    6
    0
    8
    

    Source

     
    【题解】
    dp[i][j]表示num1前i个,num2前j个的最大匹配数(突然有种二分图的感觉)
    dp[i][j] = max(dp[i-1][j],dp[i][j-1], dp[u][v] + 2 && num1[u + 1] == num2[j] && num1[i] == num2[v + 1] && num1[i] != num2[j])
    不难发现选择的线段越靠后越好,于是可以预处理u和v
    On^2
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #define max(a, b) ((a) > (b) ? (a) : (b))
     6 
     7 inline void read(int &x)
     8 {
     9     x = 0;char ch = getchar(), c = ch;
    10     while(ch < '0' || ch > '9')c = ch, ch = getchar();
    11     while(ch <= '9' && ch >= '0')x = x * 10 + ch - '0', ch = getchar();
    12     if(c == '-')x = -x;
    13 }
    14 
    15 const int INF = 0x3f3f3f3f;
    16 const int MAXN = 200 + 10;
    17 
    18 int t,n1,n2,num1[MAXN], num2[MAXN], dp[MAXN][MAXN], iclose[MAXN][MAXN],jclose[MAXN][MAXN];
    19 
    20 /*
    21 iclose[i][j]表示与num2[j]相等的最近的前i-1个num1的下标
    22 jclose[i][j]表示与num1[i]相等的最近的前j-1个num2的下标 
    23 */
    24 
    25 int main()
    26 {
    27     read(t);
    28     for(;t;--t)
    29     {
    30         read(n1), read(n2);
    31         for(register int i = 1;i <= n1;++ i) read(num1[i]);
    32         for(register int i = 1;i <= n2;++ i) read(num2[i]);
    33         for(register int i = 1;i <= n1;++ i)
    34             for (register int j = 1;j <= n2;++ j)
    35                 if(num1[i - 1] == num2[j]) iclose[i][j] = i - 1;
    36                 else iclose[i][j] = iclose[i - 1][j]; 
    37         for(register int i = 1;i <= n1;++ i)
    38             for (register int j = 1;j <= n2;++ j)
    39                 if(num1[i] == num2[j - 1]) jclose[i][j] = j - 1;
    40                 else jclose[i][j] = jclose[i][j - 1]; 
    41         for(register int i = 1;i <= n1;++ i)
    42             for(register int j = 1;j <= n2;++ j)
    43             {
    44                 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    45                 if(num1[i] != num2[j] && iclose[i][j] > 0 && jclose[i][j] > 0)dp[i][j] = max(dp[i][j],dp[iclose[i][j] - 1][jclose[i][j] - 1] + 2);
    46             }
    47         printf("%d
    ", dp[n1][n2]);
    48     }
    49     return 0;
    50 } 
    POJ1692
     
     
  • 相关阅读:
    WebAPI下的如何实现参数绑定
    MYSQL主从不同步延迟原理
    mysql的limit经典用法及优化
    ASP.NET MVC中的模型绑定
    使用EF实现数据库的增删改查
    NoSQL数据库技术特性解析之文档数据库
    MySQL 缓存 Query Cache
    Loadrunner test web service which need username and password
    vb写文件时报'Invalid procedure call or argument'
    Shell 笔记
  • 原文地址:https://www.cnblogs.com/huibixiaoxing/p/7514916.html
Copyright © 2011-2022 走看看