[HDU1160]FatMouse's Speed
题目大意:读入一些数(每行读入$w[i],s[i]$为一组数),要求找到一个最长的序列,使得符合$w[m[1]] < w[m[2]] < ... < w[m[n]]$且$s[m[1]] > s[m[2]] > ... > s[m[n]]$,并输出每组数在读入时的顺序(具体见原题目)。
思路:先根据w从小到大排序,再求最长下降子序列,DP时保存路径,最后递归输出路径。
C++ Code:
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
int n=1;
struct sz{
int w,s,num;
bool operator <(const sz& rhs)const{
if(w!=rhs.w)return w<rhs.w;
return s>rhs.s;
}
}a[1020];
int d[1020],p[1020];
void dg(int n){
if(n==0)return;
dg(d[n]);
printf("%d\n",a[n].num);
}
int main(){
while(scanf("%d%d",&a[n].w,&a[n].s)!=EOF)a[n].num=n,n++;
n--;
sort(a+1,a+n+1);
memset(d,0,sizeof(d));//d[i]表示i的前一个数
memset(p,0,sizeof(p));//p[i]表示以s[i]结尾的最长下降子序列的长度
for(int i=1;i<=n;i++)p[i]=1;
for(int i=2;i<=n;i++){
for(int j=1;j<i;j++)
if(a[i].w>a[j].w&&a[i].s<a[j].s){//a[i].w>a[j].w不能去掉,因为可能出现两个w相等的情况
if(p[i]<p[j]+1){
p[i]=p[j]+1;
d[i]=j;
}
}
}
int t,max=0;
for(int i=1;i<=n;i++)
if(max<p[i]){
t=i;
max=p[i];
}
printf("%d\n",max);
dg(t);
return 0;
}
[HDU1160]FatMouse's Speed的更多相关文章
- HDU1160 FatMouse's Speed —— DP
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1160 FatMouse's Speed Time Limit: 2000/1000 MS ...
- HDU1160:FatMouse's Speed(最长上升子序列,不错的题)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1160 学的东西还是不深入啊,明明会最长上升子序列,可是还是没有A出这题,反而做的一点思路没有,题意就不多说 ...
- FatMouse's Speed——J
J. FatMouse's Speed FatMouse believes that the fatter a mouse is, the faster it runs. To disprove th ...
- HDU 1160 FatMouse's Speed(要记录路径的二维LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1160 FatMouse's Speed (DP)
FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- FatMouse's Speed(HDU LIS)
FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- J - FatMouse's Speed
p的思路不一定要到最后去找到ans:也可以设置成在中间找到ans:比如J - FatMouse's Speed 这个题,如果要是让dp[n]成为最终答案的话,即到了i,最差的情况也是dp[i-1],就 ...
随机推荐
- 【Codeforces 276C】Little Girl and Maximum Sum
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 最后的和. 其实可以看成是 ∑bi*ai 的形式. 这里的bi这个系数表示的是有多少个区间覆盖了ai这个元素. 既然这样的话. 那么 ...
- Java 嵌套类和内部类演示样例<三>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-s ...
- nyoj19(排列组合next_permutation(s.begin(),s.end()))
题目意思: 从n个数中选择m个数,按字典序输出其排列. pid=19">http://acm.nyist.net/JudgeOnline/problem.php?pid=19 例: 输 ...
- 腾讯云 ubuntuservermysql安装和外网訪问
1 腾讯云 购买ubuntu 默认账户是ubuntu(因为winscp 使用ubuntu没有权限写文件) 因为腾讯云主机ubuntu系统默认username为ubuntu,不喜欢每次做什么都要sudo ...
- 单机 & 弱联网手游 防破解、金币改动 简单措施
手游经常使用破解方法 对于一个弱联网或者单机游戏,能够从下面方面去破解: 1.找得到存档文件的,直接破解改动存档文件. 2.找不到存档文件,就在游戏执行时借助一些软件来改动数值,比方用各种改动器手游助 ...
- memcached—向memcached中保存Java实体需注意的问题
今天以代码实例的形式总结一下向memcached中保存Java实体需注意的问题: memcached工具类代码: package com.ghj.packageoftool; import java. ...
- node generator 模仿co
exports.run = function(fn ){ return function(onDone){ function thunk(tfn , ctx){ return function(sql ...
- ShareREC for iOS v1.0.4 已经公布
ShareREC for iOS v1.0.4 已经公布 版本号:v1.0.4 2015-3-13 1.新增视频列表的筛选排序功能 2.修复在開始录制后,没有调用结束录制直接进入社区崩溃问题 3.优化 ...
- remote debug
https://docs.microsoft.com/en-us/visualstudio/debugger/remote-debugging https://docs.microsoft.com/e ...
- 机器学习案例学习【每周一例】之 Titanic: Machine Learning from Disaster
下面一文章就总结几点关键: 1.要学会观察,尤其是输入数据的特征提取时,看各输入数据和输出的关系,用绘图看! 2.训练后,看测试数据和训练数据误差,确定是否过拟合还是欠拟合: 3.欠拟合的话,说明模 ...