码迷,mamicode.com
首页 > 其他好文 > 详细

【线型DP】【LCIS】UVA_10635 Prince and Princess

时间:2020-04-09 00:58:08      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:jpg   因此   忽略   tchar   similar   ast   http   color   ssi   

技术图片嘤嘤嘤,我又来了,刚A完就写,这个沙雕题有丶恶心


 

题目:

In an n×n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3, . . . , n∗n, as shown below:

技术图片 
Prince stands in square 1, make p jumps and finally reach square n ∗ n. He enters a square at most once. So if we use xp to denote the p-th square he enters, then x1, x2, . . . , xp+1 are all different. Note that x1 = 1 and xp+1 = n ∗ n. Princess does the similar thing – stands in square 1, make q jumps and finally reach square n ∗ n. We use y1, y2, . . . , yq+1 to denote the sequence, and all q + 1 numbers are different. Figure 2 belows show a 3×3 square, a possible route for Prince and a different route for Princess.
技术图片

The Prince moves along the sequence: 1 –> 7 –> 5 –> 4 –> 8 –> 3 –> 9 (Black arrows), while the Princess moves along this sequence: 1 –> 4 –> 3 –> 5 > 6 –> 2 –> 8 –> 9 (White arrow). The King – their father, has just come. “Why move separately? You are brother and sister!” said the King, “Ignore some jumps and make sure that you’re always together.” For example, if the Prince ignores his 2nd, 3rd, 6th jump, he’ll follow the route: 1 –> 4 –> 8 –> 9. If the Princess ignores her 3rd, 4th, 5th, 6th jump, she’ll follow the same route: 1 –> 4 –> 8 –> 9, (The common route is shown in figure 3) thus satisfies the King, shown above. The King wants to know the longest route they can move together, could you tell him? 

n×n的棋盘中,王子和公主玩游戏。棋盘上的方块编号

1 2 3 。 。 ,n ∗ n,如下所示:

王子站在方格1中,使p跳跃,最后达到方格n ∗ n。他最多只能进入一个广场

一旦。因此,如果我们使用xp表示他输入的第p个平方,则x1,x2,。 。 。 ,xp + 1都不同。注意

x1 = 1且xp + 1 = n * n公主做类似的事情–站在广场1,使q跳,最终达到平方n ∗ n。我们使用y1,y2,。 。 。 ,yq + 1表示序列,并且所有q +1个数字均为不同。下面的图2显示了一个3×3的正方形,这是Prince的可能路线,而Princess的路线则不同。王子按照以下顺序移动:1 –> 7 –> 5 –> 4 –> 8 –> 3 –> 9(黑色箭头),而公主按照以下顺序移动:1 –> 4 –> 3 –> 5> 6 –> 2 –> 8 –> 9(白色箭头)。国王–他们的父亲,刚来。 “为什么要分开移动?你是兄弟姐妹!”国王说,“应该忽略一些跳跃,并确保您一直在一起。”例如,如果王子忽略了他的第二,第三,第六跳,他将遵循以下路线:1 –> 4 –> 8 –>9.如果公主不理her她的第三,第四,第五,第六跳,她将遵循相同的路线:1 –> 4 –> 8 –>9因此,如图9所示,通用路线如图3所示。国王想你知道他们可以一起走的最长路线,你能告诉他吗?

输入值

输入的第一行包含一个整数t(1≤t≤10),其后是测试用例的数量。对于每种情况,第一行包含三个整数n,p,q(2≤n≤250,1≤p,q <n * n)。第二行包含[1]范围内的p + 1个不同的整数。 。 。 n * n],王子的序列。第三行在[1。范围内包含q + 1个不同的整数。 。 。 n * n],公主的顺序。

输出量

对于每个测试案例,请输出案例编号和最长路径的长度。

样本输入

1

3 6 7

1 7 5 4 8 3 9

1 4 3 5 6 2 8 9

 

样本输出

Case 1: 4


框架:

拿到这题第一反应——花里胡哨,前面王子公主一顿操作猛如虎,最后竟然就让我们求个最大公共上升子序列,抱着这样的想法,我带着老师上课写的优化过的O(n*n)LCIS模板以及仰天大笑的姿态提交了这道题,直接上代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;//数组别小了 
 7 const int maxn=250*250+5,INF=0x3f3f3f3f;
 8 int n,m,t,p,q,f[2][maxn],a[maxn],b[maxn];
 9 inline int read(){
10    int s=0,w=1;
11    char ch=getchar();
12    while(ch<0||ch>9){if(ch==-)w=-1;ch=getchar();}
13    while(ch>=0&&ch<=9) s=s*10+ch-0,ch=getchar();
14    return s*w;
15 }//朴素的快读 
16 int main(){
17     freopen("a.in","r",stdin);
18     t=read();
19     int cnt=0;
20     while(++cnt<=t){
21         n=read();
22         p=read();
23         q=read();
24         int maxmax=0;//听说register能省点时间
25         for(register int i=1;i<=n*n;i++){ 
26             f[0][i]=f[1][i]=a[i]=b[i]=0;
27         }
28         for(register int i=1;i<=p+1;i++)a[i]=read();
29         for(register int i=1;i<=q+1;i++)b[i]=read();
30         for(register int i=1;i<=p+1;i++){ 
31             int ans=0;//lcis模板,下篇博客详细介绍lcs和lcis 
32             for(register int j=1;j<=q+1;j++){
33                 f[i%2][j]=f[(i-1)%2][j];//喜欢这样做滚动数组 
34                 if(a[i]>b[j]&&ans<f[i%2][j])ans=f[i%2][j];
35                 if(a[i]==b[j])f[i%2][j]=ans+1;
36                 maxmax=max(maxmax,f[i%2][j]);
37             }//解释:因为lcis一定是从左或从上转移过来 
38              //所以能用滚动数组节省时间空间 
39         }
40         printf("Case %d: %d\n",cnt,maxmax);
41     }
42 }

然后结局很惨:

技术图片

在这几个小时的时间里,我以为是某些地方时间超了那么一丢丢,于是从memset改到循环初始化,从cin改到快读,最后再压了压数组大小,喜提11连错

 


 

嘤嘤嘤,占个坑,今天给别人讲题来着,明天再写 

 

【线型DP】【LCIS】UVA_10635 Prince and Princess

标签:jpg   因此   忽略   tchar   similar   ast   http   color   ssi   

原文地址:https://www.cnblogs.com/614685877--aakennes/p/12663440.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!