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个步骤.每一步骤都由政府的某个工作人员负责检查你所提交的材料是否符合条件.为了加快进程,每一步政府都 ...
随机推荐
- 前端-CSS-5-继承性&层叠性&权重比较
1.继承性 <style type="text/css"> .father{ font-size: 30px; background-color: green; } . ...
- C# 获取文件名、目录、后缀、无后缀文件名、扩展名、根目录等
[csharp] view plain copy class Program { static void Main(string[] args) { //获取当前运行程序的目录 string file ...
- scala -- 层级
层级 层级的顶端是Any 类,定义了如下方法 final def ==(that:Any):Boolean final def !=(that:Any):Boolean def equals(that ...
- JDBC连接数据库创建连接对象
1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机), 这通过java.lang.Class类的静态方法forName(String classN ...
- sql查询job
use msdb go --if object_id('tempdb..#SqlAgentJob') is not null -- drop table #SqlAgentJob --go decla ...
- Typora快捷键的使用
无序列表:输入-之后输入空格 有序列表:输入数字+“.”之后输入空格 任务列表:-[空格]空格 文字 标题:ctrl+数字 表格:ctrl+t 生成目录:[TOC]按回车 选中一整行:ctrl+l 选 ...
- python复制文件,路径不存在问题(Windows和linux路径分隔符不统一)
问题: python脚本涉及到复制文件,而我们需要兼容Windows.linux和mac环境 (Windows和linux的路径分隔符不同:通过os.path.sep查看分隔符) 如果用[路径名+ ...
- BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource
Tomcat报错如下: BeanDefinitionStoreException: IOException parsing XML document from ServletContext resou ...
- Linux Shell 文本处理工具集锦(转载)
内容目录: find 文件查找 grep 文本搜索 xargs 命令行参数转换 sort 排序 uniq 消除重复行 用tr进行转换 cut 按列切分文本 paste 按列拼接文本 wc 统计行和字符 ...
- vortex
vortex - Bing dictionary US['vɔr.teks]UK['vɔː(r)teks] n.旋涡:涡旋:低涡:感情(或局势)的旋涡 网络漩涡:涡流:旋风 变形Plural Form ...