J - FatMouse's Speed dp
题目链接: https://vjudge.net/contest/68966#problem/J
找最长子串并且记录路径。
TLE代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<stdio.h>
using namespace std;
# define inf 0x3f3f3f3f
# define maxn 200000+10
int dp[maxn];
int road[maxn];
int a[maxn];
struct node
{
int we;
int sp;
int num;
} q[maxn];
bool cmp(node t1,node t2)
{
if(t1.sp!=t2.sp)return t1.sp>t2.sp;
if(t1.sp==t2.sp&&t1.we!=t2.we)return t1.we>t2.we;
return false;
}
int main()
{
int t=1;
while(~scanf("%d%d",&q[t].we,&q[t].sp))
{
q[t].num=t;
t++;
}
t--;
sort(q+1,q+t+1,cmp);
memset(dp,0,sizeof(dp));
memset(road,0,sizeof(road));
dp[1]=1;
road[1]=q[1].num;
for(int i=2; i<=t; i++)
{
int maxx=0;
int po=0;
for(int j=i-1; j>=1; j--)
{
if(q[i].we>q[j].we&&q[i].sp<q[j].sp&&maxx<dp[j])
{
maxx=dp[j];
po=q[j].num;
}
}
road[i]=po;
dp[i]=maxx+1;
}
int po=0;
int maxx=0;
for(int i=1; i<=t; i++)
{
if(dp[i]>maxx)
{
maxx=dp[i];
po=i;
}
}
int ans=0;
printf("%d\n",maxx);
int j=po;
while(1)//在这个地方超时,其实可以通过递归来实现
{
for(int i=1; i<=t; i++)
{
if(q[i].num==j)
{
a[++ans]=j;
j=road[i];
}
}
if(ans==maxx)break;
}
for(int i=ans; i>=1; i--)
printf("%d\n",a[i]);
return 0;
}
AC代码;
#include<iostream>
#include<string>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<stdio.h>
using namespace std;
# define inf 0x3f3f3f3f
# define ll long long
# define maxn 1000+10
struct node
{
int num;
int we;
int sp;
} q[maxn];
int dp[maxn];
int road[maxn];
bool cmp(node t1,node t2)
{
if(t1.sp!=t2.sp)return t1.sp>t2.sp;
if(t1.sp==t2.sp&&t1.we!=t2.we)return t1.we<t2.we;
return false;
}
void print(int k)
{
if(k==0)return ;
print(road[k]);
printf("%d\n",q[k].num);
}
int main()
{
int t=1;
while(~scanf("%d%d",&q[t].we,&q[t].sp))
{
q[t].num=t;
t++;
}
t--;
sort(q+1,q+1+t,cmp);
memset(dp,0,sizeof(dp));
memset(road,0,sizeof(road));
road[1]=0;
dp[1]=1;
for(int i=2; i<=t; i++)
{
int maxx=0;
int po=0;
for(int j=i-1; j>=1; j--)
{
if(q[i].we>q[j].we&&q[i].sp<q[j].sp&&maxx<dp[j])
{
maxx=dp[j];
po=j;//与超时的代码相比,原来存储的是第j个的编号,而如果直接存储排完序后的编号的话,到时候倒着输出就可以了。
}
}
dp[i]=maxx+1;
road[i]=po;
}
int maxx=0;
int po=0;
for(int i=1; i<=t; i++)
{
if(dp[i]>maxx)
{
maxx=dp[i];
po=i;
}
}
printf("%d\n",maxx);
print(po);
return 0;
}
J - FatMouse's Speed dp的更多相关文章
- J - FatMouse's Speed
p的思路不一定要到最后去找到ans:也可以设置成在中间找到ans:比如J - FatMouse's Speed 这个题,如果要是让dp[n]成为最终答案的话,即到了i,最差的情况也是dp[i-1],就 ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- HDU1160 FatMouse's Speed —— DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS ...
- FatMouse's Speed
J - FatMouse's Speed DP的题写得多了慢慢也有了思路,虽然也还只是很简单的DP. 因为需要输出所有选择的老鼠,所以刚开始的时候想利用状态压缩来储存所选择的老鼠,后面才发现n太大1& ...
- FatMouse's Speed——J
J. FatMouse's Speed FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- FatMouse's Speed 基础DP
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- zoj 1108 FatMouse's Speed 基础dp
FatMouse's Speed Time Limit: 2 Seconds Memory Limit:65536 KB Special Judge FatMouse believe ...
- HDU 1160 FatMouse's Speed(DP)
题意 输入n个老鼠的体重和速度 从里面找出最长的序列 是的重量递增时速度递减 简单的DP 令d[i]表示以第i个老鼠为所求序列最后一个时序列的长度 对与每一个老鼠i 遍历全部老鼠j 当 ...
随机推荐
- 第一次Sprint冲刺结果评价
组名 软件项目名称 评价 hzsy 图文转换 这款软件最初的目标是扫描书本上文字转换成电子版,而且也可以将语音转换成文字,但在展示时,没有实现完整的功能,只有简单的界面,看不到更深的实质 ...
- HTML5遇到的问题
一.Uncaught SyntaxError: Unexpected identifier 解决办法: Uncaught SyntaxError: Unexpected identifier这个问题, ...
- 第三次作业--导入excel表格(完整版)
031302322 031302316 将教师排课表导入系统 使用powerdesigner设计数据库表格 设计概念模型 打开new -> Conceptual Data Model创建概念模型 ...
- Python的三种格式化输出
今天刚学了python的三种格式化输出,以前没接触过这么有趣的输出方式,现在来分享一下. #!/user/bin/env python#coding:utf-8#三种格式化输出 #第一种格式化输出na ...
- Solr查询语法
基于solr版本:6.0.0 当配置好本地的环境之后,就访问http://localhost:8080/solr/index.html.或者是访问已经放在服务器上的solr环境,例如http://10 ...
- 12th 本周工作量及进度统计
本周PSP: C(类别) C(内容) S(开始时间) ST(结束时间) I(中断时间) T(实际时间) 活动 1日—3日 用户调查 12月1日21:00 12月3日12:00 25小时 14小时 活动 ...
- [转帖]DRAM芯片战争,跨越40年的生死搏杀
DRAM芯片战争,跨越40年的生死搏杀 超级工程一览 ·2017-08-20 12:50·半导体行业观察 阅读:1.4万 来源:内容来自超级工程一览 , 谢谢. DRAM是动态随机存储器的意思,也就是 ...
- php 有意思的小题
/** * 你的是一个数字和一个字符串进行比较,PHP会把字符串转换成数字再进行比较.* PHP转换的规则的是:若字符串以数字开头,则取开头数字作为转换结果,若无则输出0.***/1)$a =”abc ...
- blog 社会化评论插件 多说for china, disqus for global range
1.disqus https://disqus.com/ https://publishers.disqus.com/engage https://disqus.com/admin/create/ h ...
- IE userData
前面的话 IE浏览器实现了它专属的客户端存储机制——“userData”.userData可以实现一定量的字符串数据存储,可以将其用做是Web存储的替代方案.本文将详细介绍IE userData 概述 ...