Free pie (HDU1176) and free pie hdu1176

Source: Internet
Author: User

Free pie (HDU1176) and free pie hdu1176

A simple DP, time is linear, and is a natural sequence. The factors that affect decision-making are only time and position. We already know that the initial position is 5, obtain the state equation dp [I] [j] = max {dp [I] [j] + a [I] [j], dp [I + 1] [j] + a [I] [j], dp [I + 1] [J-1] + a [I] [j], dp [I + 1] [j + 1] + a [I] [j]} must note that it has only 11 locations, so some restrictions must be added, in addition, the array a [t] [x] is used to represent the number of pies at the position x at the t moment. Since the maximum time is not provided, it is worth mentioning that T should be dynamically updated during input: Since he made three decisions in the first second, therefore, dp [I] [j] actually refers to the maximum number of pies obtained when I + 1 s is transferred to position j. Therefore, the final answer is dp [0] [5].

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<vector>using namespace std;const long long INF = 100000000000000000;int n,a[100005][15],x,t,T,dp[100005][15];int main(){    while(~scanf("%d",&n)&&n){        memset(a,0,sizeof(a));        memset(dp,0,sizeof(dp));        T = 0;        for(int i=1;i<=n;i++) {            scanf("%d%d",&x,&t);            a[t][x]++;            T = max(T,t);        }        for(int i=0;i<=10;i++) dp[T][i] = a[T][i];        for(int i=T-1;i>=0;i--){            for(int j=0;j<=10;j++){                dp[i][j] = max(dp[i][j],dp[i+1][j]+a[i][j]);                if(j>0) dp[i][j] = max(dp[i][j],dp[i+1][j-1]+a[i][j]);                if(j<10) dp[i][j] = max(dp[i][j],dp[i+1][j+1]+a[i][j]);            }        }            printf("%d\n",dp[0][5]);    }    return 0;}


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.