uva_10635_Prince and Princess ( LIS )
2013-04-13 12:37
375 查看
分析: 如果使用LCS的DP算法,時間複雜度O(n*m),可惜這裏的n高達250*250,時間不允許的,題目說明了一個序列中的元素唯一,所以可以將序列A離散化,然後序列B中的元素如果A中存在相同的可以mark下來,如果不存在直接刪除,這樣可以轉換成LIS,時間複雜度O(nlogn). Code: #include <set> #include <map> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <deque> #include <vector> #include <cstdio> #include <bitset> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; #define DIR 4 #define DIM 2 #define STATUS 2 #define MAXN 62500 + 10 #define MAXM 100000 + 10 #define oo (~0u)>>1 #define INF 0x3F3F3F3F #define REPI(i, s, e) for(int i = s; i <= e; i ++) #define REPD(i, e, s) for(int i = e; i >= s; i --) static const double EPS = 1e-5; int f[MAXN], hash[MAXN]; int lis(int n) { int idx = 0; REPI(i, 1, n) { if( !idx || f[i] > hash[idx-1] ) { hash[idx ++] = f[i]; continue; } int m = lower_bound(hash, hash+idx, f[i])-hash; hash[m] = f[i]; } return idx; } int main(int argc, char const *argv[]) { #ifndef ONLINE_JUDGE freopen("test.in", "r", stdin); #endif int cas, n, p, q, v, idx, cur, cnt(1); scanf("%d", &cas); REPI(k, 1, cas) { scanf("%d %d %d", &n, &p, &q); memset(hash, 0, sizeof(int)*(n*n+1)); cur = idx = 1; REPI(i, 0, p) { scanf("%d", &v); if( hash[v] ) { continue; } hash[v] = cur ++; } REPI(i, 0, q) { scanf("%d", &v); if( !hash[v] ) { continue; } f[idx ++] = hash[v]; } printf("Case %d: %d\n", cnt ++, lis(idx-1)); } return 0; }
相关文章推荐
- UVA - 10635 Prince and Princess (求LIS)
- UVA 10635 - Prince and Princess LCS转化为LIS
- dp(LCS转化成LIS)uva 10635 - Prince and Princess
- UVa 10635 Prince and Princess 【LIS】
- Uva 10635 - Prince and Princess(LIS)
- UVA 10635 Prince and Princess DP LIS
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
- UVa 10635 - Prince and Princess(LCS转LIS)
- UVA 10635 Prince and Princess DP LIS
- uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
- UVa 10635 Prince and Princess / LCS 转 LIS
- UVA 10635 Prince and Princess (LCS优化转LIS)
- UVA 10635 Prince and Princess——LIS
- UVA 10635 Prince and Princess LCS转化为LIS *
- uva_10635 - Prince and Princess(LIS)
- UVA - 10635 Prince and Princess 转化为 LIS
- uva 10635 Prince and Princess (将LCS 转化为 LIS)
- UVA 10635 Prince and Princess(LCS转LIS)
- uva 10635 Prince and Princess(LCS问题转化成LIS问题O(nlogn))