统计排序的代价
2013-09-24 23:08
274 查看
Google Code Jam上的第三题是“Moist”问题。
具体要求如下:
order, so that he can find the cards that he wants on short notice whenever it is necessary.
The problem is -- Moist can't actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate
the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.
Moist has figured out that the robot's sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card,
it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.
As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to
sort his deck of cards.
each contain the name of a figure skater, in order from the top of the deck to the bottom.
cards.
Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.
Lexicographically, the space character comes first, then come the upper case letters, then the lower case letters.
![](http://img.blog.csdn.net/20130924230258531?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRmFjaW5nVGhlU3VuQ04=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
这道题是要对trading cards进行排序,对排序的cost进行统计。一个trading card如果需要调换位置,则cost进行加1,而不是像算法书上那样,是需要对比较的cost进行累加或者移动后,对已排序的影响也需要考虑。
实现的时候利用了一个list来保存已排好序的trading cards,应该有更好的方法(空间、时间,求勿喷)。具体代码如下:
具体要求如下:
Problem
Moist has a hobby -- collecting figure skating trading cards. His card collection has been growing, and it is now too large to keep in one disorganized pile. Moist needs to sort the cards in alphabeticalorder, so that he can find the cards that he wants on short notice whenever it is necessary.
The problem is -- Moist can't actually pick up the cards because they keep sliding out his hands, and the sweat causes permanent damage. Some of the cards are rather expensive, mind you. To facilitate
the sorting, Moist has convinced Dr. Horrible to build him a sorting robot. However, in his rather horrible style, Dr. Horrible has decided to make the sorting robot charge Moist a fee of $1 whenever it has to move a trading card during the sorting process.
Moist has figured out that the robot's sorting mechanism is very primitive. It scans the deck of cards from top to bottom. Whenever it finds a card that is lexicographically smaller than the previous card,
it moves that card to its correct place in the stack above. This operation costs $1, and the robot resumes scanning down towards the bottom of the deck, moving cards one by one until the entire deck is sorted in lexicographical order from top to bottom.
As wet luck would have it, Moist is almost broke, but keeping his trading cards in order is the only remaining joy in his miserable life. He needs to know how much it would cost him to use the robot to
sort his deck of cards.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each one starts with a line containing a single integer, N. The next N lineseach contain the name of a figure skater, in order from the top of the deck to the bottom.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of dollars it would cost Moist to use the robot to sort his deck of tradingcards.
Limits
1 ≤ T ≤ 100.Each name will consist of only letters and the space character.
Each name will contain at most 100 characters.
No name with start or end with a space.
No name will appear more than once in the same test case.
Lexicographically, the space character comes first, then come the upper case letters, then the lower case letters.
Small dataset
1 ≤ N ≤ 10.Large dataset
1 ≤ N ≤ 100.Sample
这道题是要对trading cards进行排序,对排序的cost进行统计。一个trading card如果需要调换位置,则cost进行加1,而不是像算法书上那样,是需要对比较的cost进行累加或者移动后,对已排序的影响也需要考虑。
实现的时候利用了一个list来保存已排好序的trading cards,应该有更好的方法(空间、时间,求勿喷)。具体代码如下:
#include <iostream> #include <vector> #include <list> using namespace std; int cost_sort(vector<string> & trading_cards) { if(trading_cards.size() == 1) { return 0; } vector<string>::iterator itr_trading_cards; list<string> sorted_trading_cards; list<string>::iterator itr_sorted_trading_cards; int cost = 0; sorted_trading_cards.insert(sorted_trading_cards.begin(), trading_cards[0]); for(itr_trading_cards = trading_cards.begin() + 1; itr_trading_cards != trading_cards.end(); itr_trading_cards++) { for(itr_sorted_trading_cards = sorted_trading_cards.begin(); itr_sorted_trading_cards != sorted_trading_cards.end(); itr_sorted_trading_cards++) { if((*itr_sorted_trading_cards).compare(*itr_trading_cards) > 0) { cost++; sorted_trading_cards.insert(itr_sorted_trading_cards, *itr_trading_cards); break; } } if(itr_sorted_trading_cards == sorted_trading_cards.end()) { sorted_trading_cards.insert(itr_sorted_trading_cards, *itr_trading_cards); } } sorted_trading_cards.clear(); return cost; } int main() { int T = 0; cin >> T; int N = 0; string line = ""; vector<string> trading_cards; for(int i = 0; i < T; i++) { cin >> N; getline(cin, line, '\n'); for(int j = 0; j < N; j++) { line = ""; getline(cin, line, '\n'); // cout << j << "\t " << line << endl; trading_cards.push_back(line); } cout << "Case #" << i + 1 << ": " << cost_sort(trading_cards) << endl; trading_cards.clear(); } return 0; }
相关文章推荐
- MS SQL 统计信息浅析上篇
- opengl学习笔记2-2-矩形绘制
- Qt学习之路(3):组件布局
- JQuery Div scrollTop ScrollHeight
- android无线adb开发调试
- 无法将Ethernet0 连接到虚拟网络"VMnet8"
- 【ATT】Scramble String
- 音标学习
- 走进C++程序世界------继承和派生
- 未约定经济补偿的竞业禁止条款是否无效
- poj 1966 (最小割点集)
- poj 3308 (最大流)
- 音标学习
- HTML基础之<head>篇
- hdu1517 A Multiplication Game
- Chrome的进程体系
- 浅谈 数组 和指针
- CreateCompatibleDC 与 CreateCompatibleBitmap 小小结
- HTML基础之<head>篇
- 浅谈Android中的Serializable 接口