您的位置:首页 > 其它

UVA 111 History Grading(动态规划 LIS)

2013-04-29 20:25 477 查看
HistoryGrading

Background

ManyproblemsinComputerScienceinvolvemaximizingsomemeasureaccordingtoconstraints.

Considerahistoryexaminwhichstudentsareaskedtoputseveralhistoricaleventsintochronologicalorder.Studentswhoorderalltheeventscorrectlywillreceivefullcredit,buthowshouldpartialcreditbeawardedtostudentswhoincorrectlyrankoneormoreofthehistoricalevents?

Somepossibilitiesforpartialcreditinclude:

1pointforeacheventwhoserankmatchesitscorrectrank

1pointforeacheventinthelongest(notnecessarilycontiguous)sequenceofeventswhichareinthecorrectorderrelativetoeachother.

Forexample,iffoureventsarecorrectlyordered1234thentheorder1324wouldreceiveascoreof2usingthefirstmethod(events1and4arecorrectlyranked)andascoreof3usingthesecondmethod(eventsequences124and134arebothinthecorrectorderrelativetoeachother).

Inthisproblemyouareaskedtowriteaprogramtoscoresuchquestionsusingthesecondmethod.

TheProblem

GiventhecorrectchronologicalorderofneventsViewCode

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<map>
usingnamespacestd;
map<int,int>s;//map函数用来记录事件对应的时间顺序
inta[30];//用来表示学生答案的最终序列
intdp[30];
intmain(){
intn,x,i,j,max;
scanf("%d",&n);
s.clear();
for(i=1;i<=n;i++){
scanf("%d",&x);
s[i]=x;
}
//这里的输入很特别,如果用while(1)会超时,这样先输入一个x,再接下来的输入中少输入一个即可
while(scanf("%d",&x)!=EOF){
a[x]=s[1];
for(i=2;i<=n;i++){
scanf("%d",&x);
a[x]=s[i];
}
memset(dp,0,sizeof(dp));
dp[1]=1;
max=1;
for(i=2;i<=n;i++){
for(j=i-1;j>=1;j--)
if(a[i]>a[j]&&dp[j]>dp[i])
dp[i]=dp[j];

dp[i]++;
if(dp[i]>max)
max=dp[i];
}
printf("%d\n",max);
}
return0;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: