UVa Problem Solution: 120 - Stacks of Flapjacks
来源:互联网 发布:什么编程软件好用 编辑:程序博客网 时间:2024/05/16 15:43
I just simulate the flipping. We are not required to find the optimal solution, so I choose a way easy to understand. I find the biggest cake in the unsorted stack, and then check its position. If it is at the bottom of the stack, then nothing need to be done. Otherwise, at most two steps are needed to flip it to the bottom of the unsorted stack: one flips it to the top, the other flips it to the bottom. Repeat until the unsorted stack shrinks to none.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 120 C++ "Stacks of Flapjacks" */
- #include <algorithm>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <iterator>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <sstream>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- int const cakecount = 31;
- int flip_sort(int *cakes, int ncakes, int flips[])
- {
- int sorted[cakecount];
- copy(cakes, cakes + ncakes, sorted);
- sort(sorted, sorted + ncakes);
- int nflips = 0;
- for (int i = ncakes - 1; i >= 0; --i) {
- int p = i;
- for (; p >= 0 && cakes[p] != sorted[i]; --p) {}
- if (p == i) continue;
- if (p != 0) {
- flips[nflips++] = ncakes - p;
- for (int j = 0, k = p; j < k; ++j, --k)
- cakes[j] ^= cakes[k], cakes[k] ^= cakes[j], cakes[j] ^= cakes[k];
- }
- flips[nflips++] = ncakes - i;
- for (int j = 0, k = i; j < k; ++j, --k)
- cakes[j] ^= cakes[k], cakes[k] ^= cakes[j], cakes[j] ^= cakes[k];
- }
- flips[nflips++] = 0;
- return nflips;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- filebuf in, out;
- cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
- cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
- #endif
- string line;
- while (getline(cin, line)) {
- stringstream ss(line);
- int cakes[cakecount];
- int ncakes = 0;
- for (; ss >> cakes[ncakes]; ++ncakes) {}
- for (int i = 0; i < ncakes; ++i)
- cout << (i > 0 ? " " : "") << cakes[i];
- cout << '/n';
- int flips[cakecount];
- int nflips = flip_sort(cakes, ncakes, flips);
- for (int i = 0; i < nflips; ++i)
- cout << (i > 0 ? " " : "") << flips[i];
- cout << '/n';
- }
- return 0;
- }
- UVa Problem Solution: 120 - Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- uva 120 Stacks of Flapjacks
- uva - 120 - Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA - 120 - Stacks of Flapjacks
- UVa:120 - Stacks of Flapjacks
- How to get local machine name and IP address?
- NSIS 资料二(常用的教本参数)
- 一个无缝滚动代码(兼容IE、FF)
- 一个伸缩效果,把innerHTML=="展开"中的...
- 在虚拟机下的ubuntu·
- UVa Problem Solution: 120 - Stacks of Flapjacks
- 关于我们
- web2.0精致导航下拉菜单代码
- GridView控件实现支持分页的自动编号代码
- VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结
- How to run a user control assembly that is hosted on Internet Information Services (IIS) in Internet Explorer
- 焦点图片轮换第三季
- ASP.NET网络上实现单点登录
- Windows CE API机制初探