hdu 4960 Another OCD Patient dp
来源:互联网 发布:java的mvc框架 编辑:程序博客网 时间:2024/05/16 12:38
题目链接:hdu 4960
题意:一排橡皮泥排成一排,要求最后捏成对称的,捏过的不能再捏.
思路:书读的少啊!!!看到题就想暴力,发现不行才想dp,状态转移方程dp[j]=min(dp[j],dp[i]+a[cnt]),初始化dp值为从开始捏到I.
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <vector>using namespace std;#define MAXN 100010#define INF 1e9struct node{ int x,y; node(){} node(int xe,int ye):x(xe),y(ye) {}};int data[MAXN],a[MAXN];int n;int dp[MAXN];vector <node> layer;void init(){ layer.clear(); for(int i=0;i<n;i++) { scanf("%d",&data[i]); } for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } a[0]=0;}int be_layer(){ int l=0,r=n-1,ans; int x,y; long long l_value,r_value; x=1,y=1,l_value=data[0],r_value=data[n-1]; while(1) { if(l_value>r_value) { y++; r--; r_value+=data[r]; } else if(l_value<r_value){ x++; l++; l_value+=data[l]; } else { // cout<<l<<" x "<<r<<endl; if(l>=r) { // ans=y; break; } //cout<<l_value<<" "<<r_value<<endl; // printf("%d %d\n",x,y); layer.push_back(node(x,y)); if(l+1==r) { ans=0; break; } r--; l++; x=1,y=1,l_value=data[l],r_value=data[r]; } } return ans;}void slove(){ int midcnt=be_layer(); //cout<<midcnt<<endl; dp[0]=0; int sizee=layer.size(); //cout<<midcnt<<" "<<sizee<<endl; for(int i=0;i<sizee;i++) // cout<<layer[i].x<<" "<<layer[i].y<<endl; for(int i=1;i<=sizee;i++) { dp[i]=INF; } int sum_l=0,sum_r=0; for(int j=0;j<sizee;j++) { sum_l+=layer[j].x; sum_r+=layer[j].y; dp[j]=(a[sum_l]+a[sum_r]); } for(int i=0;i<sizee;i++) { sum_l=0,sum_r=0; for(int j=i+1;j<sizee;j++) { sum_l+=layer[j].x; sum_r+=layer[j].y; dp[j]=min(dp[j],dp[i]+a[sum_l]+a[sum_r]); } } int ans=INF; for(int i=sizee-1;i>=0;i--) { if(ans>dp[i]+a[midcnt]) { ans=dp[i]+a[midcnt]; } if(i) { midcnt+=(layer[i].x+layer[i].y); } } if(sizee==0) ans=a[n]; printf("%d\n",ans);}int main(){ //freopen("1001.in","r",stdin); //freopen("data.out","w",stdout); while(1){ scanf("%d",&n); if(n==0) break; init(); slove(); } return 0;}
0 0
- 【HDU】4960 Another OCD Patient 【DP】
- HDU 4960 Another OCD Patient 区间dp
- hdu 4960 Another OCD Patient dp
- HDU 4960 Another OCD Patient(DP)
- HDU 4960(Another OCD Patient-区间dp)
- HDU-4960 Another OCD Patient (DP)
- HDU 4960 Another OCD Patient (dp)
- hdu 4960 Another OCD Patient(DP)
- hdu 4960 Another OCD Patient
- HDU 4960 Another OCD Patient
- HDU 4960 Another OCD Patient
- hdu 4960 Another OCD Patient 多校九 区间DP
- hdu 4960 Another OCD Patient dp(记忆化搜索)
- HDU 4960 Another OCD Patient(区间dp记忆化搜索)
- hdu 4960 Another OCD Patient dp(记忆化搜索)
- Another OCD Patient HDU
- 【DP】 HDOJ 4960 Another OCD Patient
- hdu 4960 Another OCD Patient(记忆化)
- JAVA多线程变量的深入认识(二)
- Java分块传输的思路
- servlet filter listener interceptor 作用和区别
- POJ 1273 Drainage Ditches
- 去掉文件中^M的方法
- hdu 4960 Another OCD Patient dp
- 跨服务器操作数据库
- PHP盛宴——常用函数集锦
- 1408211934-hd-Crixalis's Equipment.cpp
- hdu4973
- POJ 1459 Power Network
- Ubuntu上编译安装Freetype/Freetype-py
- 韩语学习之第十一课
- svn代码回滚命令