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 当 ...
随机推荐
- 右键添加使用Sublime打开
网上教程大多是教你怎么改注册表,有点麻烦. 我根据教程改完之后导出来供大家使用,更方便快捷. Windows Registry Editor Version 5.00 [HKEY_CLASSES_RO ...
- JSP JSTL知识结构图
自行绘制,欢迎指正.
- 四则运算level2
package j; import java.util.Scanner; public class Main { public static void main(String[] args) { Sc ...
- “数学口袋精灵”App的第一个Sprint计划
一.现状 我们这个团队想制作一个关于运算的游戏类型手机软件,针对我们这个学期的Android软件开发的课程,制作出一个关于数学算术游戏软件. 二.任务认领 第一阶段先把静态网页制作出来,各自的任务: ...
- [OSChina]VirtualBox 6.0.0 发布,改进对高端显示器的 HiDPI 支持--尝试一下
VirtualBox 6.0.0 发布,改进对高端显示器的 HiDPI 支持 https://www.oschina.net/news/102838/virtualbox-6-0-0-released ...
- Session, Cookie区别
答: 1.Session由应用服务器维护的一个服务器端的存储空间:Cookie是客户端的存储空间,由浏览器维护. 2.用户可以通过浏览器设置决定是否保存Cookie,而不能决定是否保存Session, ...
- 如何在主Form出现之前,弹出密码验证From,Cancel就退出程序,Ok后密码正确才出现主Form
如何在主Form出现之前,弹出密码验证From,Cancel就退出程序,Ok后密码正确才出现主Form本文地址 :CodeGo.net/5175478/ ----------------------- ...
- Chrome Ajax 跨域设置
一.前言 web 开发中 Ajax 是十分常见的技术,但是在前后端使用接口对接的调试过程中不可避免会碰到跨域问题.今天我给大家介绍一个十分简单有效的方法. 跨域经典错误 二.Chrome 跨域设置 首 ...
- jQuery3 slim版本和普通版本区别,如何选择?
区别概述: slim即简化版,比普通版本缺少Ajax和特效模块模块. 官方发布地址:http://blog.jquery.com/2017/03/20/jquery-3-2-1-now-availab ...
- c++ int转string类型
std::string int2string(int input){ std::ostringstream ss; //clear string //ss.str(""); //s ...