Prince and Process 解题报告
2015-09-16 09:53
375 查看
\
由于数据量太大,DP LCS算法会超时。
记录princess的路径在prince的路径出现的index位置,问题就转化成一个LIS的问题。
这里如果使用数组遍历查询,因数据量大,依旧会超时,所以开了一个足够大的数组来模拟map<int, int>。
当然使用map是不会超时的,但系统规定不让用STL。
LIS问题有一个nlogn的算法。
还有一个问题,因为开的数组太大了,使用C编译出来的程序会挂掉,但使用了c++编译就OK了。
#include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX (250*250 + 1) int pa[MAX]; int qa[MAX]; int f[MAX]; int binsearch(int x, int len) { int left = 0, right = len, cnt = 0; while (left <= right) { int mid = (left + right) >> 1; if (x>f[mid]) { cnt = mid; left = mid + 1; } else right = mid - 1; } return cnt; } int main() { int T, n, p, q, rq; int i, j, id; int lcs, tl; scanf("%d", &T); for (id = 1; id <= T; id++) { lcs = 0; scanf("%d %d %d", &n, &p, &q); p++; q++; memset(pa, 0, sizeof(pa)); for (i = 1; i <= p; i++) { int pv; scanf("%d", &pv); pa[pv] = i; } // LCS will exceed time limit // record the index of the value in qa that appears in pa rq = 0; for (j = 1; j <= q; j++) { int qv; scanf("%d", &qv); if (pa[qv] != 0) qa[rq++] = pa[qv]; } // now become a LIS problem f[0] = -1; for (i = 0; i < rq; i++) { tl = binsearch(qa[i], lcs) + 1; if (tl > lcs) { f[tl] = qa[i]; lcs = tl; } else if (f[tl] > qa[i]) f[tl] = qa[i]; } printf("Case %d: %d\n", id, lcs); } return 0; }
相关文章推荐
- 图像配准转换
- C 二叉树
- can begin transaction in hibernate called while commiting/rollback
- python中令人蛋疼的地方及一些技巧
- Div+CSS布局之写入整体层结构与CSS
- android LayoutInflater.inflate()的参数及其用法
- Tomcat启动读取applicationContext.xml出错的解决方式
- Delphi中Chrome Chromium、Cef3学习笔记(五)
- [总结]视音频编解码技术零基础学习方法
- 史上最全的CSS hack方式一览
- 初学iOS笔记
- jsp中request.getParameter和request.getAttribute之间的区别
- 新浪微博开发-(6)自定义搜索框
- Java TreeMap 源码解析
- 欢迎使用CSDN-markdown编辑器
- 第三方框架、AFN、ASI、SDWebImage
- 操作系统常见面试题总结
- C# 操作数据库的方法
- MongoDB学习日记 - java代码(三):数据库与集合操作
- [C++][编程风格]C++命名规则