Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用DP做了,可是TLE? 本人DP写的不怎么好,请望指教。//1163 an easy dp problem #include <stdio.h> struct num { int val; int sum; }org[100][100]; void input(int t) { int tp1;int tp2; for(tp1=1;tp1<=t;tp1++) { for(tp2=0;tp2<tp1;tp2++) { scanf("%d",&org[tp1-1][tp2].val); } } } void clr() { int a; for(a=0;a<10000;a++) { org[a/100][a%100].val =-1; org[a/100][a%100].sum =-1; } } int max(int a,int b) { if(a>b) return a; else return b; } int cal(int c,int d,int t) { if(org[c][d].sum =-1) { if(c<t-1) org[c][d].sum =max(cal(c+1,d,t),cal(c+1,d+1,t))+org[c][d].val; if(c==t-1) org[c][d].sum =org[c][d].val; } return org[c][d].sum; } void output(int ans) { printf("%d\n",ans); } void work(int t) { int ans; input(t); ans=cal(0,0,t); output(ans); } int main() { int t; while(scanf("%d",&t)!=EOF) { clr(); work(t); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator