POJ 3265 DP

Source: Internet
Author: User

Cow can get m money at the end of each month, there are a total of n problems to solve

If you solve a problem, you must pay a deposit in the first month, and pay the end payment in i+1 month, need to solve the problem in order, the monthly money can only be used in the next month, cannot accumulate

F[i][j]=max (M-∑b[j]-∑b[k]) {where k should meet f[i-1][k]>=∑a[j]-∑a[k]}

F[I][J] Indicates the maximum amount of money that can be left for a J question before the first month of settlement

#include "stdio.h" #include "string.h" int dp[2010][310];int Max (int a,int b) {if (a<b) return B; else return A;}    int main () {int i,j,ans,m,n,a,b;    int suma[310],sumb[310];        while (scanf ("%d%d", &m,&n)!=eof) {memset (suma,0,sizeof (Suma));        memset (sumb,0,sizeof (sumb));            for (i=1;i<=n;i++) {scanf ("%d%d", &a,&b);            Suma[i]=suma[i-1]+a;        Sumb[i]=sumb[i-1]+b;        } ans=0;        Memset (Dp,-1,sizeof (DP));        Dp[1][0]=m;            while (1) {ans++; for (i=1;i<=n;i++) for (j=i;j>=0;j--) {if (dp[ans-1][j]>=suma[i                    ]-SUMA[J] && m>=sumb[i]-sumb[j]) Dp[ans][i]=max (Dp[ans][i],m-sumb[i]+sumb[j]);                if (dp[ans][i]==m) break;        } if (dp[ans][n]>=0) break;    } printf ("%d\n", ans+1); } return 0;}


POJ 3265 DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.