YT05-動態歸劃求解課後題目-1001—FatMouse's Speed-(6.21日-煙臺大學ACM預備隊解題報告)
FatMouse's Speed
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22 Accepted Submission(s) : 12
Special Judge
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
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.
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
Source
Zhejiang University Training Contest 2001
問題大意:
一隻胖老鼠認為越胖的老鼠跑的越快。當然這是不科學的,所以我們就要拿出證據來反駁他。
輸入:
我們將會輸入若干行每行2個整數第一個代表老鼠的體重,第二個代表老鼠的速度。最多輸入1000只老鼠的資訊,且體重和速度都在1~1000。
Ps:每隻老鼠可以有相同的體重,相同的速度,甚至完全一樣。
輸出:
輸出n在輸出n個數,每個數代表第幾只老鼠,如第一個從鍵盤輸入的老鼠引數就是1,第6個輸入的老鼠引數就是6,n代表這個子列的長度。
這n個數代表一個有規律的數列,規則是體重遞減而速度遞增,我們求的就是最長的這麼一個數列。
Ps:答案不唯一,因為最長的子列可以有多個。比如本題給的樣本資料你輸出4 4 5 9 8也是對的。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define MAXN 10050
using namespace std;
int dp[MAXN];
int out[MAXN];
struct mice
{
int weight;
int speed;
int num;
} m[MAXN];
int cmp(mice a,mice b)
{
if(a.weight!=b.weight)
return a.weight>b.weight;
return a.speed<b.speed;
}
int main()
{
int n,i,j,x,max,max2,end;
max=0;
memset(dp,0,sizeof(dp));
i=1;
while(scanf("%d%d",&m[i].weight,&m[i].speed)!=EOF)
{
m[i].num=i;
i++;
}
n=i;
sort(m+1,m+n,cmp);
for(i=1; i<=n-1; i++)
{
max2=0;
for(j=i-1; j>=1; j--)
{
if((m[j].weight>m[i].weight)&&(m[j].speed<m[i].speed))
{
if(max2<dp[j])
{
max2=dp[j];
x=j;
}
}
}
if(max2)out[m[i].num]=m[x].num;
dp[i]=max2+1;
if(dp[i]>max)
{
max=dp[i];
end=m[i].num;
}
}
printf("%d\n",max);
while(out[end]!=end)
{
printf("%d\n",end);
end=out[end];
}
printf("%d\n",end);
return 0;
}
相關文章
- YT05-動態歸劃求解課後題目-1004—Max Sum -(6.21日-煙臺大學ACM預備隊解題報告)ACM
- YT05-動態歸劃求解課堂題目-1003—數塔-(6.21日-煙臺大學ACM預備隊解題報告)ACM
- YT05-動態歸劃求解課後題目-1003—免費餡餅 -(6.21日-煙臺大學ACM預備隊解題報告)ACM
- YT05-動態歸劃求解課堂題目-1004—最少攔截系統-(6.21日-煙臺大學ACM預備隊解題報告)ACM
- YT05-動態歸劃求解課後題目-1002—Super Jumping! Jumping! Jumping! -(6.21日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課後題目-1001 母牛的故事-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課堂題目-1001 蟠桃記-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課堂題目-1005 Children’s Queue-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課後題目-1002 超級樓梯-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課堂題目-1002 折線分割平面-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT06-揹包-1001—Bone Collector -(6.27日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課堂題目-1004 不容易系列之一-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課後題目-1006 不容易系列之(4)——考新郎-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT03-遞推求解課後題目-1003 不容易系列之(3)—— LELE的RPG難題-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT04-貪心課堂練習-1001 今年暑假不AC-(6.14日-煙臺大學ACM預備隊解題報告)ACM
- YT04-貪心課後練習-1004—迷瘴-(6.14日-煙臺大學ACM預備隊解題報告)ACM
- YT04-貪心課後練習-1006—PAINTER(6.14日-煙臺大學ACM預備隊解題報告)AIACM
- YT03-遞推求解課堂題目-1003 獻給杭電五十週年校慶的禮物-(6.7日-煙臺大學ACM預備隊解題報告)ACM
- YT04-貪心課後練習-1002—Repair the Wall-(6.14日-煙臺大學ACM預備隊解題報告)AIACM
- YT04-貪心課堂練習-1004—Fire Net-(6.14日-煙臺大學ACM預備隊解題報告)ACM
- YT04-貪心課堂練習-1005—Wooden Sticks-(6.14日-煙臺大學ACM預備隊解題報告)ACM
- YT06-揹包-1002—Piggy-Bank -(6.27日-煙臺大學ACM預備隊解題報告)ACM
- YT04-貪心課後練習-1003—悼念512汶川大地震遇難同胞——老人是真餓了-(6.14日-煙臺大學ACM預備隊解題報告)ACM
- YT06-揹包-1003—悼念512汶川大地震遇難同胞——珍惜現在,感恩生活 -(6.27日-煙臺大學ACM預備隊解題報告)ACM
- 河南理工大學程式設計(ACM)大賽解題報告程式設計ACM
- 動態規劃求解最長上升子序列問題動態規劃
- 動態規劃解題方法動態規劃
- 動態規劃分類題目總結動態規劃
- 杭電ACM hdu 2079 選課時間 解題報告(母函式)ACM函式
- 大學畢業課題研究開題報告PPT分享-20套可下載
- NYNU ACM 藍橋杯選拔賽 解題報告ACM
- 山東省第四屆acm解題報告(部分)ACM
- 動態規劃9:變態跳臺問題動態規劃
- 動態規劃 擺花 題解動態規劃
- LeetCode:動態規劃+貪心題目整理LeetCode動態規劃
- leetcode題解(動態規劃)LeetCode動態規劃
- 青年大學習第十季第五期題目和答案(含課後習題)
- 一維動態規劃和二維動態規劃中兩道經典題目動態規劃