UVa 10635 - Prince and Princess

传送门

UVa 10635 - Prince and Princess

思路

学习了LCS的$O(nlogn)$解法。

代码

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;

int Hash[70000];

int main()
{
    //freopen("input.txt", "r", stdin);
    int T, i, j, an, bn, n, cases = 0;
    scanf("%d", &T);
    while (T--)
    {
        memset(Hash, 0, sizeof Hash);
        vector<int> ve;
        scanf("%d%d%d", &n, &an, &bn);
        int temp = 1;
        //ve.push_back(temp);
        for (i = 1; i <= an + 1; i++)
        {
            scanf("%d", &temp);
            Hash[temp] = i;
        }
        for (i = 0; i <= bn; i++)
        {
            scanf("%d", &temp);
            if (temp == 0)
                continue;
            int v = Hash[temp];
            if (ve.empty() || v > ve.back())
                ve.push_back(v);
            else
            {
                int t = lower_bound(ve.begin(), ve.end(), v) - ve.begin();
                ve[t] = v;
            }
        }
        printf("Case %d: %d\n", ++cases, ve.size());
    }
    return 0;
}

Powered by Jekyll and Theme by solid