HDU 1160
来源:互联网 发布:淘宝上什么是禁售商品 编辑:程序博客网 时间:2024/06/06 10:59
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 m11, m22,…, mnn then it must be the case that
Wm[1m[1] < Wm[2m[2] < … < Wm[nm[n]
and
Sm[1m[1] > Sm[2m[2] > … > Sm[nm[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
按体重降序,求最长上升子序列,并打印序列(答案不唯一)
dp[j]:以f[i]结尾的最长上升子序列
dp[j] = max(dp[x]&&f[x] <= f[j])+1 x∈(1,j)
耗时间的做法:
#include<iostream>#include<cstdio>#include<algorithm>#include<queue>using namespace std;#define INF 0x3f3f3f3f#define maxn 10005struct data{ int weight, speed, th, order[1001];};data mouse[maxn];int dp[maxn];bool cmp(data a, data b){ return a.weight > b.weight ;}int main(){ int i = 0, j = 0, k, index = 0, max = 0, ans = 0, ans_index; while(~scanf("%d %d", &mouse[i].weight, &mouse[i].speed)){ mouse[i].th = i+1; ++i; } int num = i; sort(mouse, mouse+i, cmp); //for(i = 0; i < num; ++i){ //printf("%d %d %d\n",mouse[i].th ,mouse[i].weight , mouse[i].speed ); //} mouse[0].order[0] = mouse[0].th ; dp[0] = 1; ans_index = 0; ans = -INF; for(i = 1; i < num; ++i){ index = i; max = -INF; int flag = 1; for(j = 0; j < i; ++j){ if(mouse[i].speed > mouse[j].speed && max < dp[j]){ max = dp[j]; index = j; flag = 0; } } if(flag){ dp[i] = 1; mouse[i].order[0] = mouse[i].th; } else{ dp[i] = max + 1; for(k = 0; k < dp[index]; ++k){ mouse[i].order[k] = mouse[index].order[k]; } mouse[i].order[k] = mouse[i].th; } if(ans < dp[i]){ ans = dp[i]; ans_index = i; } } printf("%d\n", ans); for(i = ans-1; i >= 0; --i){ printf("%d\n", mouse[ans_index].order[i]); }}
用并查集优化
#include<iostream>#include<cstdio>#include<algorithm>#include<stack>using namespace std;#define INF 0x3f3f3f3f#define maxn 10005struct data{ int weight, speed, th, last;};data mouse[maxn];int dp[maxn];bool cmp(data a, data b){ return a.weight > b.weight ;}int main(){ int i = 0, j = 0, k, index = 0, max = 0, ans = 0, ans_index; while(~scanf("%d %d", &mouse[i].weight, &mouse[i].speed)){ mouse[i].th = i+1; ++i; } int num = i; sort(mouse, mouse+i, cmp); //for(i = 0; i < num; ++i){ //printf("%d %d %d\n",mouse[i].th ,mouse[i].weight , mouse[i].speed ); //} mouse[0].last = 0; dp[0] = 1; ans_index = 0; ans = -INF; for(i = 1; i < num; ++i){ index = i; max = -INF; int flag = 1; for(j = 0; j < i; ++j){ if(mouse[i].speed > mouse[j].speed && max < dp[j]){ max = dp[j]; index = j; flag = 0; } } if(flag){ dp[i] = 1; mouse[i].last = i; } else{ dp[i] = max + 1; mouse[i].last = index; } if(ans < dp[i]){ ans = dp[i]; ans_index = i; } } printf("%d\n", ans); while(mouse[ans_index].last != ans_index){ printf("%d\n",mouse[ans_index].th); ans_index = mouse[ans_index].last; } printf("%d\n",mouse[ans_index].th);}
- hdu 1160
- hdu 1160
- HDU 1160
- HDU 1160
- hdu 1160
- HDU 1160
- HDU 1160
- HDU 1160
- hdu 1160
- hdu 1160
- HDU-1160
- HDU 1160
- hdu 1160
- HDU 1160
- HDU 1160 动态规划
- hdu 1160 排序
- hdu 1160 dp
- (1160)HDU-类LIS
- 质数(两种方法)
- Q130:PBRT-V3,非均匀介质的采样(11.3.3章节、15.2.2章节)
- 2017.07.16【NOIP提高组】模拟赛B组小结
- Android 新建Activity并运行的步骤
- 安装好cuda和cudnn后,安装opencv2.4.9出现错误
- HDU 1160
- 腾讯分布式数据库DCCB
- javax.media错误解决 播放器代码
- Android Context 上下文 你必须知道的一切
- leetcode学习 1.two sum
- MFC/VS2013创建MFC项目(一)
- C/C++学习笔记一(整型/浮点型)
- socket选项
- 基于讯飞开放平台的安卓语音开发——语音合成(文本→语音)