SRM 222 Div II Level Two: GroceryBagger,STL map 用法
2013-07-17 11:43
429 查看
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3450
这个题目其实很简单,只要得到每种物品的数量就可以了,用一般的方法也可以做,不过如果使用STL中map
容器的话,那真是轻松加惬意啊!主要是map重载了[]运算符,所以操作起来特别方便。
代码如下:
这个题目其实很简单,只要得到每种物品的数量就可以了,用一般的方法也可以做,不过如果使用STL中map
容器的话,那真是轻松加惬意啊!主要是map重载了[]运算符,所以操作起来特别方便。
代码如下:
#include <string> #include <vector> #include <map> using namespace std; class GroceryBagger { public: int minimumBags(int strength, vector <string> itemType) { int res = 0; map <string, int> msi; for (int i = 0; i < itemType.size(); i++) { msi[ itemType[i] ] = 0; } for (int i = 0; i < itemType.size(); i++) { ++msi[ itemType[i] ]; } for ( map<string, int>::iterator it = msi.begin(); it != msi.end(); it++) { res += (it->second + strength - 1) / strength; } return res; } };
相关文章推荐
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序( 比较函数 )
- SRM 222 Div II Level One: TextCompressor,求最大重复子串问题
- SRM 219 Div II Level Two: HealthFood
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 233 Div II Level Two: PipeCuts
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 299 Div II Level Two: Projections
- SRM 223 Div II Level Two: BlackAndRed,O(N)复杂度
- SRM 483 Div II Level Two: MovieSeating, TopCoder上正确率不到%10的题
- SRM 578 Div II Level Two: GooseInZooDivTwo, DFS
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 585 Div II Level Two: TrafficCongestionDivTwo
- SRM 579 Div II Level Two: UndoHistory
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 207 Div II Level Two: RegularSeason,字符串操作(sstream),多关键字排序(操作符重载)
- SRM 577 Div II Level Two: EllysRoomAssignmentsDiv2
- SRM 578 Div II Level Three: WolfInZooDivTwo,Dynamic Programming,求教!
- SRM 207 Div II Level Three: CaptureThemAll,BFS算法
- SRM 219 Div II Level One: WaiterTipping,小心约分