1001 即hdu 1003 Max Sum
状态转移方程 dp[i]=max(dp[i],dp[i-1]);没这么写
是相邻最大和的题,以前做过的题,数组开小了,wa了4次,最后才发现,弱暴了
View Code
#include<stdio.h>
int num[100005];
int main()
{
int T,cas=1;
scanf("%d",&T);
while(cas<=T)
{
if(cas!=1) printf("\n");
int n; scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
int sum=0,max=-99999,s=1,e=1,pos=1;
for(int i=1;i<=n;i++)
{
if(sum<0)
{sum=num[i];pos=i;}
else sum+=num[i];
if(max<sum)
{
s=pos;
max=sum;
e=i;
}
}
printf("Case %d:\n%d %d %d\n",cas++,max,s,e);
}
return 0;
}
1002 即hdu 1081 To The Max
1001 这道题的变形,变成2维的了,自己写了个还是wa,还是没学好啊,百度下发现,思考不全面
先是1--n行枚举+一维的dp
View Code
#include<stdio.h>
#include<string.h>
int num[105][105],ss[105];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
scanf("%d",&num[i][j]);
}
int max=-99999;
for(int i=0;i<n;i++)
{
memset(ss,0,sizeof(ss));
for(int j=i;j<n;j++)
{
int t=0;
for(int k=0;k<n;k++)
{
ss[k]+=num[j][k];
if(t<=0) t=ss[k];
else t+=ss[k];
if(t>max) max=t;
}
}
}
printf("%d\n",max);
}
return 0;
}
1003 hdu 1025 Constructing Roads In JGShining's Kingdom
不知怎么的总是有tle的思路代码,这题呢,经典的lis 题,
View Code
#include<stdio.h>
#define N 500005
int map[N],flag[N];
int main()
{
int n,cas=1;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
map[a]=b;
}
int low,hig,mid,top=1;
flag[1]=map[1];
for(int i=2;i<=n;i++)
{
low=1;hig=top;
while(low<=hig)
{
mid=(low+hig)/2;
if(flag[mid]<map[i])
low=mid+1;
else hig=mid-1;
}
flag[low]=map[i];
if(low>top) top++;
}
printf("Case %d:\n",cas++);
if(top==1)
printf("My king, at most %d road can be built.\n\n",top);
else
printf("My king, at most %d roads can be built.\n\n",top);
}
return 0;
}
1004 即hdu1058 Humble Numbers
做过的题了,自己做的思路 感觉定位在 枚举还是比较合适,不合主题~o_o
2 3 5 7 和 前 n-1 个数从大到小枚举
View Code
#include<stdio.h>
__int64 str[5845];
int main()
{
int n;
int b[4]={2,3,5,7};
str[1]=1;
for(int i=2;i<=5842;i++)
{
str[i]=2000000005;
for(int j=0;j<4;j++)
{
for(int k=i-1;k>=1;k--)
{
if(str[k]*b[j]<=str[i-1]) break;
if(str[i]>str[k]*b[j])
str[i]=str[k]*b[j];
}
}
}
while(scanf("%d",&n),n)
{
printf("The %d",n);
if(n%10==1&&n%100!=11) printf("st");
else if(n%10==2&&n%100!=12) printf("nd");
else if(n%10==3&&n%100!=13) printf("rd");
else printf("th");
printf(" humble number is %I64d.\n",str[n]);
}
return 0;
}
1005 即hdu 1080 Human Gene Functions
自己没想出来思路,百度的
先把字符处理成int ,然后就是 dp转移方程
grah[i][j]=max(grah[i-1][j-1]+score[ps[a[i-1]]][ps[b[j-1]]]
,max(grah[i][j-1]+score[4][ps[b[j-1]]],grah[i-1][j]+score[4][ps[a[i-1]]]));
经典啊,我的想法走歪了
View Code
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
map<char,int > ps;
int main()
{
ps['A']=0;ps['C']=1;ps['G']=2;ps['T']=3;ps['-']=4;
const int score[5][5]=
{{5,-1,-2,-1,-3},
{-1,5,-3,-2,-4},
{-2,-3,5,-2,-2},
{-1,-2,-2,5,-1},
{-3,-4,-2,-1,0}};
int n;cin>>n;
while(n--)
{
string a,b;int lena,lenb;
cin>>lena>>a>>lenb>>b;
int grah[105][105];
grah[0][0]=0;
for(int i=1;i<=lena;i++)
grah[i][0]=grah[i-1][0]+score[ps[a[i-1]]][4];
for(int i=1;i<=lenb;i++)
grah[0][i]=grah[0][i-1]+score[4][ps[b[i-1]]];
for(int i=1;i<=lena;i++)
for(int j=1;j<=lenb;j++)
{
grah[i][j]=max(grah[i-1][j-1]+score[ps[a[i-1]]][ps[b[j-1]]]
,max(grah[i][j-1]+score[4][ps[b[j-1]]],grah[i-1][j]+score[4][ps[a[i-1]]]));
}
cout<<grah[lena][lenb]<<endl;
}
return 0;
}