📄 usaco 3_1_2 score inflation 题解_leokan的blog.mht
字号:
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
size=3D1>http://hi.baidu.com/leokan/</FONT></A></STRONG></P>
<P><STRONG>=C4=A3=D0=CD=B5=C4=C7=F3=BD=E2:<FONT color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>=B7=BD=B7=A8=D2=BB:=D6=
=B1=BD=D3=BD=A8=C1=A2=C4=A3=D0=CD<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>score:array[1..n]=A1=FB=
S<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>timearray[1..n]=A1=FBT=
<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>f:array[1..m]=A1=FB0=20
{f[x]=3Df(x)}<FONT color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>for i=A1=FB1 to =
m do<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>for j=A1=FB1 to =
n do=20
{f(x)=3Dmax{f(x-Ti)+Si} <FONT color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR> if =
f[i-time[j]]+score[j]>f[i] and i-time[j]>0<FONT =
color=3D#ffffff=20
=
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A =
href=3D"http://hi.baidu.com/leokan/"><FONT=20
color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR> &nbs=
p; =20
then f[i]=A1=FBf[i-time[j]]+score[j]<FONT color=3D#ffffff=20
=
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A =
href=3D"http://hi.baidu.com/leokan/"><FONT=20
color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>=D5=E2=D6=D6=B7=BD=B7=A8=
=CA=C7=BF=C9=D0=D0=B5=C4=B7=BD=B7=A8=D6=AE=D2=BB,=B5=AB=CA=C7=D0=A7=C2=CA=
=B2=BB=B8=DF,=BB=B9=D3=D0=D2=BB=D6=D6=B7=BD=B7=A8<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>=B7=BD=B7=A8=B6=FE:=B3=
=E4=B7=D6=C0=FB=D3=C3=D2=D1=BE=AD=B5=C3=B5=BD=B5=C4=D7=B4=CC=AC<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>f:array[1..m]=A1=FB(f[=
i]=3DSi)<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>for i=A1=FB1 to =
m do<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>for j=A1=FB1 to =
i div 2=20
do<FONT color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR> if =
f[i-j]+f[j]>f[i] then f[i]:=3Df[i-j]+f[j];<FONT color=3D#ffffff =
=
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A =
href=3D"http://hi.baidu.com/leokan/"><FONT=20
color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A><BR>=D5=E2=D6=D6=B7=BD=B7=A8=
=D6=B1=BD=D3=C0=FB=D3=C3=D2=D4=C7=B0=B5=C3=B5=BD=B5=C4=C3=BF=D2=BB=D6=D6=D7=
=B4=CC=AC=A1=C6Si,=A1=C6Ti=C7=F3=BD=E2,=BD=AB=D7=B4=CC=AC=CF=E0=BC=D3,=B6=
=F8=B2=BB=CA=C7=BD=ABSi,Ti=CF=E0=BC=D3,=BF=C9=D2=D4=CB=B5=D0=A7=C2=CA=BB=E1=
=B8=FC=B8=DF.<FONT=20
color=3D#ffffff =
size=3D1>=B1=BE=CC=FB=D4=AD=B4=B4,=C7=EB=CE=F0=D7=AA=D4=D8</FONT><A=20
href=3D"http://hi.baidu.com/leokan/"><FONT color=3D#ffffff=20
=
size=3D1>http://hi.baidu.com/leokan/</FONT></A></STRONG></P><STRONG>
<HR>
</STRONG>
=
<P><STRONG>=CE=D2=B5=C4=B1=C8USACO=B5=C4=CF=EA=CF=B8=B6=E0=C1=CB</STRONG>=
</P>
<P><STRONG>We use dynamic programming to calculate the best way to =
use t=20
minutes for all t from 0 to tmax. </STRONG></P>
<P><STRONG>When we find out about a new category of problem with =
points p=20
and length t, we update the best for j minutes by taking the =
better of=20
what was there already and what we can do by using a p point =
problem with=20
the best for time j - t. </STRONG></P><PRE><STRONG>#include =
<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAXCAT 10000
#define MAXTIME 10000
int best[MAXTIME+1];
void
main(void)
{
FILE *fin, *fout;
int tmax, ncat;
int i, j, m, p, t;
fin =3D fopen("inflate.in", "r");
fout =3D fopen("inflate.out", "w");
assert(fin !=3D NULL && fout !=3D NULL);
fscanf(fin, "%d %d", &tmax, &ncat);
for(i=3D0; i<ncat; i++) {
fscanf(fin, "%d %d", &p, &t);
for(j=3D0; j+t <=3D tmax; j++)
if(best[j]+p > best[j+t])
best[j+t] =3D best[j]+p;
}
m =3D 0;
for(i=3D0; i<=3Dtmax; i++)
if(m < best[i])
m =3D best[i];
fprintf(fout, "%d\n", m);
exit(0);
}
</STRONG></PRE>
<P><STRONG>Greg Price writes: After the main `for' loop that does =
the=20
actual DP work, we don't need to look at the entire array of best =
point=20
totals to find the highest one. The array is always nondecreasing, =
so we=20
simply output the last element of the array. =
</STRONG></P><PRE><STRONG>#include <fstream.h>
ifstream fin("inflate.in");
ofstream fout("inflate.out");
const short maxm =3D 10010;
long best[maxm], m, n;
void
main()
{
short i, j, len, pts;
fin >> m >> n;
for (j =3D 0; j <=3D m; j++)
best[j] =3D 0;
for (i =3D 0; i < n; i++) {
fin >> pts >> len;
for (j =3D len; j <=3D m; j++)
if (best[j-len] + pts > best[j])
best[j] =3D best[j-len] + pts;
}
fout << best[m] << endl; // This is always the highest =
total.
}</STRONG></PRE></DIV></TD></TR></TBODY></TABLE><BR>
<DIV class=3Dopt><A =
title=3D=B2=E9=BF=B4=B8=C3=B7=D6=C0=E0=D6=D0=CB=F9=D3=D0=CE=C4=D5=C2=20
href=3D"http://hi.baidu.com/leokan/blog/category/Oi">=C0=E0=B1=F0=A3=BAOi=
</A> | <A=20
href=3D"http://hi.baidu.com/leokan/modify/blog/ec65ea1188a010c2a6ef3f6f">=
=B1=E0=BC=AD</A> |=20
<A onclick=3D"return blogdel('blogdelform')"=20
href=3D"http://hi.baidu.com/leokan/blog/item/ec65ea1188a010c2a6ef3f6f.htm=
l#">=C9=BE=B3=FD</A>=20
<FORM id=3Dblogdelform style=3D"DISPLAY: none" name=3Dblogdelform=20
action=3D/leokan/commit method=3Dpost><INPUT type=3Dhidden value=3D1 =
name=3Dct><INPUT=20
type=3Dhidden value=3D3 name=3Dcm><INPUT type=3Dhidden =
value=3Dec65ea1188a010c2a6ef3f6f=20
name=3DspBlogID><INPUT type=3Dhidden =
value=3Dhttp://hi.baidu.com/leokan/blog=20
name=3DspRefURL></FORM>
<SCRIPT language=3Djavascript>
<!--
function blogdel(str)
{
var pop=3Dnew Popup({ =
contentType:3,isReloadOnClose:false,width:340,height:80});
pop.setContent("title","=C9=BE=B3=FD=CE=C4=D5=C2");
=
pop.setContent("confirmCon","=C4=FA=C8=B7=B6=A8=D2=AA=B3=B9=B5=D7=C9=BE=B3=
=FD=D5=E2=C6=AA=CE=C4=D5=C2=BC=B0=C6=E4=CB=F9=D3=D0=C6=C0=C2=DB=C2=F0=A3=BF=
");
pop.setContent("callBack",delCallback);
pop.setContent("parameter",{fid:str,popup:pop});
pop.build();
pop.show();
return false;
}
function delCallback(para)
{
var o_pop=3Dpara["popup"];
o_pop.config.contentType=3D1;
o_pop.setContent("contentUrl","");
o_pop.reBuild();
G(para["fid"]).target=3Do_pop.iframeIdName;
eval("document."+para["fid"]).submit();
}
//-->
</SCRIPT>
| <A =
title=3D=BD=AB=B4=CB=CE=C4=D5=C2=CC=ED=BC=D3=B5=BD=B0=D9=B6=C8=CB=D1=B2=D8=
onclick=3D"return addToFavor();"=20
href=3D"http://cang.baidu.com/do/add" =
target=3D_blank>=CC=ED=BC=D3=B5=BD=CB=D1=B2=D8</A> | =E4=AF=C0=C0(<SPAN=20
id=3Dresult></SPAN>) | <A=20
href=3D"http://hi.baidu.com/leokan/blog/item/ec65ea1188a010c2a6ef3f6f.htm=
l#send">=C6=C0=C2=DB</A> (0)
<SCRIPT language=3Djavascript>
/*<![CDATA[*/
var pre =3D [true,'USACO 3.1.1 Agri-Net =CC=E2=BD=E2', 'USACO 3.1.1 =
Agri-Net =
=CC=E2=BD=E2','/leokan/blog/item/9be0dc09860ef986d0581be8.html'];
var post =3D [true,'USACO 3.1.3 Humble Numbers =CC=E2=BD=E2','USACO =
3.1.3 Humble Numbers =CC=E2...', =
'/leokan/blog/item/1867a4ec32d4052363d09fe0.html'];
if(pre[0] || post[0]){
document.write('<div =
style=3D"height:5px;line-height:5px;"> </div><div id=3D"in_nav">');
if(pre[0]){
document.write('=C9=CF=D2=BB=C6=AA=A3=BA<a href=3D"' + pre[3] + '" =
title=3D"' + pre[1] + '">' + pre[2] + '</a> ');
}
if(post[0]){
document.write('=CF=C2=D2=BB=C6=AA=A3=BA<a href=3D"' + post[3] + '" =
title=3D"' + post[1] + '">' + post[2] + '</a>');
}
document.write('</div>');
}
/*]]>*/
</SCRIPT>
</DIV>
<DIV class=3Dline></DIV>
<STYLE type=3Dtext/css>#in_related_doc A {
TEXT-DECORATION: none
}
</STYLE>
<DIV id=3Din_related_tmp></DIV>
<SCRIPT language=3Djavascript type=3Dtext/javascript>
/*<![CDATA[*/
function HI_MOD_IN_RELATED_DOC_CALLBACK(arg){
if(arg.length <=3D 1) return false;
var hasMore =3D arg[0];
var D=3Dfunction(A,B){A[A.length]=3DB;}
if(arg.length % 2 =3D=3D 0) D(arg, ["","","",""]);
var html =3D ['<div id=3D"in_related_doc"><div =
class=3D"tit">=CF=E0=B9=D8=CE=C4=D5=C2=A3=BA</div>'];
D(html, '<table cellpadding=3D"0" cellspacing=3D"3" border=3D"0">');
for(var i =3D 1, j =3D arg.length; i < j; i +=3D 2){
D(html, '<tr>');
D(html, '<td width=3D"15px"><a style=3D"font-size:25px" =
>•</a></td><td><a href=3D"http://hi.baidu.com/' + arg[i][3] + =
'/blog/item/' + arg[i][2] + '.html" target=3D"_blank" title=3D"' + =
arg[i][0] + '">' + arg[i][1] + '</a>');
D(html, new Array(10).join('\u3000'));
D(html, '</td>');
if(arg[i + 1][0] !=3D "")
D(html, '<td width=3D"15px"><a style=3D"font-size:25px" =
>•</a></td><td><a href=3D"http://hi.baidu.com/' + arg[i + 1][3] + =
'/blog/item/' + arg[i + 1][2] + '.html" target=3D"_blank" title=3D"' + =
arg[i + 1][0] + '">' + arg[i + 1][1] + '</a></td>');
else
D(html, '<td> </td><td> </td>');
D(html, '</tr>');
}
if(hasMore) D(html, '<tr><td colspan=3D"4"><a target=3D"_blank" =
href=3D"/sys/search?pageno=3D1&type=3D7&sort=3D1&word=3DUSACO%203%2E1%2E2=
%20Score%20Inflation%20%CC%E2%BD%E2&item=3Dec65ea1188a010c2a6ef3f6f">=B8=FC=
=B6=E0>></a></td></tr>');
D(html, '</table></div><div class=3D"line"> </div>');
var div =3D document.getElementById('in_related_tmp');
if(div){
div.innerHTML =3D html.join('');
while(div.firstChild){
div.parentNode.insertBefore(div.firstChild, div);
}
div.parentNode.removeChild(div);
}
}
if(RelatedDocData =3D=3D -1){ // not supported xhr
var script =3D document.createElement('script');
script.type =3D 'text/javascript';
script.src =3D =
'/sys/search?type=3D8&word=3DUSACO%203%2E1%2E2%20Score%20Inflation%20%CC%=
E2%BD%E2&item=3Dec65ea1188a010c2a6ef3f6f&t=3D' + new Date().getTime();
document.getElementsByTagName('HEAD')[0].appendChild(script);
}else if(RelatedDocData =3D=3D null){
GetAndEval =3D true;
}else{
eval(RelatedDocData);
}
/*]]>*/
</SCRIPT>
<DIV id=3Din_reader>
<DIV class=3Dtit>=D7=EE=BD=FC=B6=C1=D5=DF=A3=BA</DIV>
<SCRIPT>
var g_spAnnony=3Dfalse;
var g_read=3D[
{}
];
g_read.length=3Dg_read.length-1;
var _rh1=3D"";
var _rh2=3D"";
function wrreader(){
_rh1 +=3D '<table width=3D"100%" ><tr>';
_rh2+=3D'<tr>';
if(g_spAnnony){
_rh1+=3D'<td align=3D"center" width=3D"10%" ><img border=3D"0" =
width=3D"55" height=3D"55" =
src=3D"http://img.baidu.com/hi/img/portraitn.jpg"></td>';
_rh2+=3D'<td> </td>';
if(g_read.length>0){
_rh1+=3D'<td align=3D"left" width=3D"12%">';
}else{
_rh1+=3D'<td align=3D"left" width=3D"100%">';
}
_rh1+=3D"<a =
href=3D'http://passport.baidu.com/?login&tpl=3Dsp&tpl_reg=3Dsp&u=3D"+myre=
f+"' =
target=3D'_self'>=B5=C7=C2=BC</a>=BA=F3=A3=AC=C4=FA=BE=CD=B3=F6=CF=D6=D4=DA=
=D5=E2=C0=EF=A1=A3</td>";
_rh2+=3D'<td> </td>'
}
if(g_read.length=3D=3D0){
if(!g_spAnnony){
_rh1+=3D'<td align=3Dleft =
width=3D"100%">=D7=EE=BD=FC=BB=B9=C3=BB=D3=D0=B5=C7=C2=BC=D3=C3=BB=A7=BF=B4=
=B9=FD=D5=E2=C6=AA=CE=C4=D5=C2=A1=AD=A1=AD</td>';
_rh2+=3D'<td> </td>';
}
}else{
for(i=3D0,len=3Dg_read.length;i<len;i++){
_rh1+=3D'<td align=3D"center" valign=3D"bottom" width=3D"10%" =
class=3D"user"><a href=3D"/'+g_read[i][0]+'" target=3D"_blank"><img =
border=3D"0" =
src=3D"http://himg.baidu.com/sys/portraitn/item/'+g_read[i][1]+'.jpg"></a=
></td>';
_rh2+=3D'<td align=3D"center" valign=3D"top" class=3D"user"><a =
href=3D"/'+g_read[i][0]+'" target=3D"_blank">'+g_read[i][2]+'</a></td>';
}
}
_rh1+=3D'<td width=3D"100%"></td></tr>';
_rh2+=3D'<td></td></tr></table>';
document.write(_rh1+_rh2);
}
wrreader();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -