CF 812B Sagheer, the Hausmeister
来源:互联网 发布:java人才培训班 编辑:程序博客网 时间:2024/06/10 17:44
The way I get this done is dynamic programming . Unlike the greedy method I do a little preliminary stuff to finished it.
the equation is pretty easy , just three way of get next floor.but we have to find the topmost floor that need to go .
#include <bits/stdc++.h>#include <cstring>using namespace std;const int maxn = 1005;const int INF = 1<<29;int n = 0,m;int dp[maxn][2];int lmost[maxn],rmost[maxn];int go(int from,int to,int i){ if(from == 1 && to == 1) { return lmost[i]*2 + 1; } if(from == 0 && to == 0) { return rmost[i]*2 + 1; } if( from != to) { return m +2; }}int main(){ cin>>n>>m; string str[20]; int ceilling = 0; for(int i=n; i>=1; i--) { cin>>str[i]; } int flag = 0; for(int i=n;i>=1;i--) { for(int j=1;j<=m;j++) { if(str[i][j] == '1') { flag = 1; break; } } if(flag == 0) n--; if(flag) break; }// cout<<n<<endl; for(int i = 1;i<=n;i++) { int f = 0; for(int j=1;j<=m;j++) { if(str[i][j] == '1') { f = 1; lmost[i] = m-j+1; break; } } for(int j = m;j>=1;j--) { if(str[i][j] == '1') { rmost[i] = j; break; } } if(!f) { lmost[i] = rmost[i] = 0; } } //cout<<n<<endl; if(n == 1) { cout<<rmost[1]<<endl; return 0; } //cout<<n<<endl;// for(int i=n;i>=1;i--)// {// cout<<lmost[i]<<" "<<rmost[i]<<endl;// } dp[1][0] = 0; dp[1][1] = m; for(int i=2; i<=n; i++) { dp[i][0] = min(dp[i-1][0] + go(0,0,i-1),dp[i-1][1]+go(1,0,i-1)); dp[i][1] = min(dp[i-1][1] + go(1,1,i-1),dp[i-1][0]+go(0,1,i-1)); }// cout<<endl;// for(int i=n;i>=1;i--)// {// for(int j =0 ;j<2;j++)// {// cout<<dp[i][j]<<" ";// }// cout<<endl;// } cout<<min(dp[n][0] + rmost[n],dp[n][1] + lmost[n])<<endl; return 0;}
阅读全文
0 0
- CF 812B Sagheer, the Hausmeister
- B. Sagheer, the Hausmeister
- Codeforecs 812B Sagheer, the Hausmeister DP
- Codeforces 812B-Sagheer, the Hausmeister
- Codeforces 812B Sagheer, the Hausmeister[dp]
- codeforces 812 B Sagheer, the Hausmeister(枚举)
- Codeforces 812 B Sagheer, the Hausmeister
- codeforces 812B Sagheer, the Hausmeister dp
- codeforces 812b Sagheer, the Hausmeister
- codeforces Sagheer, the Hausmeister 812B
- Codeforces 812B-Sagheer, the Hausmeister
- CodeFroces 812B Sagheer, the Hausmeister(BFS)
- codeforces812BB. Sagheer, the Hausmeister
- Codeforces812B Sagheer, the Hausmeister
- Sagheer, the Hausmeister CodeForces
- Sagheer, the Hausmeister CodeForces
- B. Sagheer, the Hausmeister(codeforce 417 div2 B, dfs)
- codefoces 812B——Sagheer, the Hausmeister(动态规划)
- linux下将一个文件mount为一个文件系统
- Unity3D Linear和Gamma渲染
- ListView的适配(使用自定义ArrayAdapter)
- 【A
- 文件名置换
- CF 812B Sagheer, the Hausmeister
- ReactiveX/RxJava文档中文版
- UVA548 Tree(DFS,二叉树)
- js中的cookie操作
- 数据库水平切分的实现原理解析——分库,分表,主从,集群,负载均衡器(转)
- 注解
- sqlserver字符串分割
- java线程池(很好的文章)
- 算法题目---实现atoi函数