NC25041. [USACO 2007 Jan G]Problem Solving
描述
输入描述
Line 1: Two space-separated integers: M and P.
Lines 2..P+1: Line i+1 describes problem i with two space-separated integers: Bi and Ai. Bi is the payment to the consult BEFORE the problem is solved; Ai is the payment to the consult AFTER the problem is solved.
输出描述
Line 1: The number of months it takes to solve and pay for all the cows' problems.
示例1
输入:
100 5 40 20 60 20 30 50 30 50 40 40
输出:
6
C++ 解法, 执行用时: 5ms, 内存消耗: 812K, 提交时间: 2022-02-11 14:10:14
#include<bits/stdc++.h> using namespace std; const int N=300; const int M=200; const int mod=1e9+7; const int INF=0x3f3f3f3f; int m,n,a[N+10],b[N+10],dp[N+10][N+10]; int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>m>>n; for(int i=1;i<=n;i++){ int x,y;cin>>x>>y; a[i]=a[i-1]+x,b[i]=b[i-1]+y; } memset(dp,0x3f,sizeof(dp)); dp[0][0]=0; for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ if(a[i]-a[i-j]>m || b[i]-b[i-j]>m)continue; for(int k=0;k<=i-j;k++){ if(a[i]-a[i-j]+b[i-j]-b[i-j-k]>m)continue; dp[i][j]=min(dp[i][j],dp[i-j][k]+1); } } for(int j=1;j<=i;j++)dp[i][0]=min(dp[i][j]+1,dp[i][0]); } int ans=INF; for(int i=1;i<=n;i++)ans=min(ans,dp[n][i]+2); ans=min(ans,dp[n][0]+1); cout<<ans; return 0; }