HDU1160 FatMouse's Speed LIS变种+路径
来源:互联网 发布:linux命令 cp r 编辑:程序博客网 时间:2024/04/30 16:19
J - FatMouse’s Speed
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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]] < W[m[2]] < … < W[m[n]]
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
题解
题目的意思是输入每只老鼠的体重和速度,输出在体重递增的情况下,速度的最长不上升序列的个数和路径,输入完后先按体重递增排序,然后LIS的时候顺便记录路径,最后逆序输出。不难,但很考验细心和耐心…调试了好久快奔溃了..突然冒出来的AC真是太感人了!
代码
#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;struct mice{ int val; //最长序列的个数 int weight; int speed; int number; //一开始的编号 int p; //路径}m[1005];bool cmp(mice a,mice b){ return a.weight<b.weight;}int main(){ int cnt = 0; while(scanf("%d%d",&m[cnt].weight,&m[cnt].speed)!=EOF) { m[cnt].number = cnt+1; m[cnt].p = -1; cnt++; } int a[1005]; sort(m,m+cnt,cmp); for(int i = 0;i<cnt;i++) for (int j = 0;j<i;j++) { if (m[i].speed < m[j].speed && m[i].val<m[j].val+1 && m[i].weight!=m[j].weight) { m[i].val = m[j].val+1; m[i].p = j; } } int maxn = 0;int markmax = 0; for (int i = 0;i<cnt;i++) if (m[i].val >= maxn) { maxn = m[i].val; markmax = i; } printf("%d\n",maxn+1); a[0] = m[markmax].number; int temp = m[markmax].p; int i = 1; int flag = 1; while (flag) { if (temp == -1) { flag = 0; break; } a[i] = m[temp].number; temp = m[temp].p; i++; } for (i = maxn;i>=0;i--) printf("%d\n",a[i]);}
- HDU1160 FatMouse's Speed LIS变种+路径
- hdu1160 FatMouse's Speed(LIS普通法)
- hdu1160 FatMouse's Speed--DP&记录路径
- HDU1160--FatMouse's Speed
- HDU1160 FatMouse's Speed
- HDU1160 FatMouse's Speed
- hdu1160,FatMouse's Speed
- HDU1160--FatMouse's Speed
- hdu1160 FatMouse's Speed
- HDU1160 FatMouse's Speed
- hdu1160 FatMouse's Speed
- HDU1160 FatMouse's Speed
- hdu1160 FatMouse's Speed
- [HDU1160]-FatMouse's Speed
- hdu1160 FatMouse's Speed
- hdu1160 FatMouse's Speed 最长上升子序列 LIS
- HDU1160 - FatMouse's Speed(最长下降子序列+打印路径)
- hdu1160 FatMouse's Speed (dp,记录路径)
- 保障执行计划正确
- CSS3阴影 box-shadow的使用和技巧总结
- VS条件调试
- powerdesign使用心得
- 国内镜像网站更新AndroidSDK 的操作
- HDU1160 FatMouse's Speed LIS变种+路径
- 定时关机的小软件
- zoj 3612 (multiset)
- Oracle BBED模拟坏块&验证坏块
- Redis 缓存 + Spring 的集成示例
- jquery-easyui简单使用
- 安卓开发学习笔记(一)
- java nio多线程引起的full gc问题
- leetcode happy number