UVA 111 History Grading 【lcs】

Brief Description:

一个历史考试,有n个历史事件, 它们之间的年份是不同的,要学生把这些事件按照正确的顺序排列出来。有两种记分方式,采用的是第二种: 假设有历史事件1,2,3,4, 它们正确的时间顺序是1,2,3,4, 然后假设学生的答案是1,3,2,4, 那么按照相对顺序正确的数量,答对了三个(1,2,4或者1,3,4),也就是它与正确答案的最长公共子序列长度是3,便是答对的数量。

Analyse:

最长公共子序列模板题,但是这题的输入是个很大的坑,他的输入是按照顺序,事件1是排在第几位,事件2是排在第几位......, 所以要先把输入转换成正确的顺序。

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=100007;

int st[22],t[22];
int dp[22][22];

int main()
{
    int n;
    int id;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&id);
        st[id]=i;
    }

    while(scanf("%d",&id)==1)
    {
        t[id]=1;
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&id);
            t[id]=i;
        }
        mem(dp,0);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(st[i]==t[j])
                    dp[i][j]=dp[i-1][j-1]+1;
                else
                {
                    dp[i][j]=max(dp[i][j],max(dp[i-1][j],dp[i][j-1]));
                }
            }

        }

        printf("%d\n",dp[n][n]);
    }
    return 0;
}
时间: 2024-08-27 06:41:48

UVA 111 History Grading 【lcs】的相关文章

uva 111 - History Grading (dp, LCS)

题目链接 题意:给N,第二行是答案,n个数c1---cn, 代表第一个的顺序是c1,第二个数顺序是c2; 下面每一行是学生的答案,格式同上. 注意:这个给的顺序需要处理一下,不能直接用. 思路:LCS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 using namespac

uva 111 History Grading(DP初步应用)

uva 111 History Grading Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all

UVA 111 History Grading (最长公共子序列)

History Grading Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put s

UVa 111 - History Grading

题目:历史上有一些事件发生的先后顺序,现在有很多学生写了不同的顺序表, 判断每个学生的最大的前后顺序正确的序列. 分析:dp,LIS,最大上升子序列. 注意本题的数据格式,串里的每个元素对应于:对应下标编号的事件在表中的位置: 状态:F(n)记录以第n个元素为结束元素的序列的最长上升子序列,有转移方程: F(n)= max(F(i)+1)  { 其中 0 < i < n 且 data[i] < data[n] }. 说明:发现好多dp题(⊙_⊙). #include <iostre

uva 101 History Grading

Background Many problems in Computer Science involve maximizing some measure according to constraints. Consider a history exam in which students are asked to put several historical events into chronological order. Students who order all the events co

uva 6437 - Power Plant【最小生成树】

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4448 题目大意:和基本的最小生成树想比,其在最开始的时候有多个起点,从任一起点开始生成都可以. 一直到最后才发现做法,就是把起始点都提前加入生成树里面. #include<iostream> #include<stdio.h> #includ

uva 10020 Minimal coverage 【贪心】+【区间完全覆盖】

Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the number of test ca

UVA 10369 Arctic Network【最小生成树】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1310 题意: 有m个卫星,n个站点,卫星可以不用代价相连,剩下要用无线电连,求无线电连接中最大距离的最小 解法: 最小边开始加入,这样第n - m 条边的权值就是答案 代码:略 版权声明:转载请注明出处.

HDU1503:Advanced Fruits 【LCS】

Advanced Fruits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 2 Special Judge Problem Description The company "21st Century Fruits" has specialized in creating