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,贴代码,飘过~~#include <iostream> using namespace std; int cow[30001]; int dp[30001]; int main() { int n,i,cmin; while(scanf("%d",&n) != EOF) { for(i = 0;i < n;i++) scanf("%d",&cow[i]); dp[0] = 0; dp[n] = 0; for(i = 0;i < n;i++) if(cow[i] == 1) dp[0] ++; for(i = 1;i < n;i++) { if(cow[i - 1] == 1) dp[i] = dp[i - 1] - 1; if(cow[i - 1] == 2) dp[i] = dp[i - 1] + 1; } for(i = 0;i < n;i++) if(cow[i] == 2) dp[n] ++; cmin = dp[0]; for(i = 1;i <= n;i++) cmin = cmin > dp[i] ? dp[i] : cmin; for(i = n - 1;i >= 0;i--) if(cow[i] == 1) dp[n-1]++; for(i = n - 2;i >= 0;i--) { if(cow[i + 1] == 1) dp[i] = dp[i + 1] - 1; if(cow[i + 1] == 2) dp[i] = dp[i + 1] + 1; } for(i = 1;i < n;i++) cmin = cmin > dp[i] ? dp[i] : cmin; printf("%d\n",cmin); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator