UVa 111 History Grading (简单DP,LIS或LCS)
题意:题意就是坑,看不大懂么,结果就做不对,如果看懂了就so easy了,给定n个事件,注意的是,
它给的是第i个事件发生在第多少位,并不是像我们想的,第i位是哪个事件,举个例子吧,4 2 3 1,
表示的是第一个事件发生在第四,第二个事件发生在第二位,第三个在第三位,第四个在第一位。
然后输入n个答案,求有多少个事件相对位置是和原来一样的。
那么知道输入好办了,我们只需对输入做一下预处理,就变成了LIS。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map> using namespace std;
const int maxn = 20 + 5;
int d[maxn], a[maxn], id[maxn]; int main(){
int n, x; cin >> n;
for(int i = 1; i <= n; ++i){
scanf("%d", &x);
id[i] = x;//第i个事件发生在第x位
} while(~scanf("%d", &x)){
a[0] = -10;
a[x] = id[1];
for(int i = 2; i <= n; ++i){
scanf("%d", &x);
a[x] = id[i];//查找第x个事件编号是几
} memset(d, 0, sizeof(d));
int m = 0;
for(int i = 1; i <= n; ++i){//LIS
for(int j = 0; j < i; ++j)
if(a[i] > a[j] && d[j]+1 > d[i]) d[i] = d[j] + 1;
m = max(m, d[i]);
} printf("%d\n", m);
} return 0;
}
网上大数都是用LCS做,其实都差不多。
用LCS代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <cstring>
#include <map> using namespace std;
const int maxn = 20 + 5;
int d[maxn][maxn], a[maxn], id[maxn]; int main(){
int n, x; cin >> n;
for(int i = 1; i <= n; ++i){
scanf("%d", &x);
id[x] = i;
} while(~scanf("%d", &x)){
a[0] = -10;
a[x] = 1;
// a[x] = id[1];
for(int i = 2; i <= n; ++i){
scanf("%d", &x);
a[x] = i;
// a[x] = id[i];
} memset(d, 0, sizeof(d));
int m = 0;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j)
if(id[i] == a[j]) d[i][j] = d[i-1][j-1] + 1;
else d[i][j] = max(d[i-1][j], d[i][j-1]);
// m = max(m, d[i]);
} printf("%d\n", d[n][n]);
} return 0;
}
UVa 111 History Grading (简单DP,LIS或LCS)的更多相关文章
- uva 111 History Grading(最长公共子序列)
题目连接:111 - History Grading 题目大意:给出一个n 代表序列中元素的个数, 然后是一个答案, 接下来是若干个同学的答案(直到文件结束为止), 求出两个序列的最长公共子序列, 注 ...
- UVa 111 - History Grading (by 最长公共子序列 )
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- uva 111 - History Grading (dp, LCS)
题目链接 题意:给N,第二行是答案,n个数c1---cn, 代表第一个的顺序是c1,第二个数顺序是c2; 下面每一行是学生的答案,格式同上. 注意:这个给的顺序需要处理一下,不能直接用. 思路:LCS ...
- uva 111 History Grading(lcs)
题目描述 在信息科学中有一些是关于在某些条件限制下,找出一些计算的最大值. 以历史考试来说好了,学生被要求对一些历史事件根据其发生的年代顺序来排列.所有事件顺序都正确的学生无疑的可以得满分.但是那些没 ...
- UVA 111 History Grading
读题读了好久,其实就是在输入数据时要对数据的位置进行相应的改变 #include<iostream> #include<cstring> #include<cstdio& ...
- UVA - 11584 划分字符串的回文串子串; 简单dp
/** 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34398 UVA - 11584 划分字符串的回文串子串: 简单 ...
- UVA 111 (复习dp, 14.07.09)
History Grading Background Many problems in Computer Science involve maximizing some measure accor ...
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- 洛谷P1130红牌(简单DP)
题目描述 某地临时居民想获得长期居住权就必须申请拿到红牌.获得红牌的过程是相当复杂 ,一共包括NNN个步骤.每一步骤都由政府的某个工作人员负责检查你所提交的材料是否符合条件.为了加快进程,每一步政府都 ...
随机推荐
- js前台遍历后台返回的Datatable数据
jsondata 后台返回的datatable ) { ; j < jsondata.length; j++) { jsondata[j]; }; }
- windows7安装node
一.在官网下载node 二.按照提示进行安装 三.安装好的目录结构 四.测试是否安装好了node 首先按快捷键win+r,在运行窗口输入cmd,调出命令提示窗口,在命令提示窗口中输入path查看nod ...
- CStatic控件SS_NOTIFY属性
SS_NOTIFY Sends the parent window STN_CLICKED, STN_DBLCLK, STN_DISABLE, and STN_ENABLE notification ...
- Java 静态代理和动态代理例子
代理Proxy: Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委 ...
- pip & Jinja2
[pip&Jinja2] 2.7.9 以及上版本的 python 安装程序会自动安装 pip. 升级:pip install --upgrade pip. 使用pip安装Jinja2:sudo ...
- python使用外部PY文件的变量
在用python和selenium编写登录等脚本时,一直都是给用户名和密码直接赋值.但是考虑到这样不便于管理,而且可能多个地方用到同一个变量,所以想把变量放在一个单独的文件中进行管理. 以登录脚本为例 ...
- hdoj3038(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题意:对于给定的a1..an,通过询问下标x..y,给出a[x]+...+a[y],但给出的值可 ...
- Excel Sheet Column Title (STRING - TYPE CONVERTION)
QUESTION Given a positive integer, return its corresponding column title as appear in an Excel sheet ...
- Tunnel Warfare (区间合并|最大值最小值巧妙方法)
Tunnel Warfare http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others) ...
- [leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers ...