热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU1160FatMouse'sSpeed简单DP

FatMouse'sSpeedProblemDescriptionFatMousebelievesthatthefatteramouseis,thefasteritruns
                 FatMouse‘s Speed
Problem Description
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
 
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed. 
 
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],..., m[n] then it must be the case that 

W[m[1]]

and 

S[m[1]] > S[m[2]] > ... > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one. 
 
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
 
 
Sample Output
4
4
5
9
7
 
一组数据,处理到文件结束
给你一些牛的体重,和速度
现在从这些牛中挑出尽量多的牛组成一个序列,使得满足:
体重从小到大严格递减,速度从大到小严格递增
输出最多能找出多少只牛,还要输出牛的序号
 
其实类似最大上升子序列一样,只不过有2个条件,一个增大,一个减少而已。
再另外记录下原来输入的牛的编号,
这样排序后照样可以知道他原来的序号。
 
按原来的序号输出
 
技术分享技术分享
 1 #include
 2 #include
 3 #include
 4 #include
 5 
 6 using namespace std;
 7 
 8 const int maxn=1005;
 9 
10 int dp[maxn];                    //以第i个为结束元素的序列的最大值
11 int pre[maxn];                   //记录序列
12 
13 struct Node
14 {
15     int w,s,num;                 //num记录输入的顺序
16 }node[maxn];
17 
18 bool cmp(Node a,Node b)
19 {
20     if(a.w==b.w)
21         return a.s>b.s;
22     return a.w<b.w;
23 }
24 
25 //输出路径
26 void output(int cur)
27 {
28     if(pre[cur]!=-1)
29         output(pre[cur]);
30     cout<endl;
31 }
32 
33 int main()
34 {
35     //freopen("in.txt","r",stdin);          //记住要注释掉
36 
37     int u,v;
38     int tot=1;
39 
40     while(cin>>u>>v)
41     {
42         node[tot].w=u;
43         node[tot].s=v;
44         node[tot].num=tot++;
45     }
46 
47     sort(node+1,node+tot,cmp);
48 
49     memset(pre,-1,sizeof(pre));
50 
51     for(int i=1;i)
52         dp[i]=1;
53 
54     for(int i=1;i)
55     {
56         for(int j=1;j)
57             if(node[j].wnode[i].s)
58                 if(dp[j]+1>dp[i])
59                 {
60                     dp[i]=dp[j]+1;
61                     pre[i]=j;
62                 }
63     }
64 
65     int ans=0;
66     int cur;
67     for(int i=1;i)
68         if(dp[i]>ans)
69         {
70             ans=dp[i];
71             cur=i;
72         }
73 
74     cout<endl;
75 
76     output(cur);
77 
78     return 0;
79 }
View Code
 
 
 
 
 
 
 
 
 
 
 

 

HDU 1160 FatMouse&#39;s Speed 简单DP


推荐阅读
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文讲述了作者通过点火测试男友的性格和承受能力,以考验婚姻问题。作者故意不安慰男友并再次点火,观察他的反应。这个行为是善意的玩人,旨在了解男友的性格和避免婚姻问题。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
author-avatar
手机用户2702938100
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有