【贪心】删数问题
2012-11-22 17:13
99 查看
【问题描述】
给定n位整数q,去掉其中任意k<=n个数字后,剩下的数字按原次序排列组成一个新的正整数。对于给定的n位正整数a和正整数k,设计一个算法找出剩下数字组成的新数最小的删数方案。
比如,178543删除四个数字后,最小的新数是13。
贪心策略:最近下降点优先。
给定n位整数q,去掉其中任意k<=n个数字后,剩下的数字按原次序排列组成一个新的正整数。对于给定的n位正整数a和正整数k,设计一个算法找出剩下数字组成的新数最小的删数方案。
比如,178543删除四个数字后,最小的新数是13。
贪心策略:最近下降点优先。
#include <iostream> #include <vector> #include <string> using namespace std; void delek(vector<int> &s, int k) { int n=s.size(); if(k>=n) ; else { while(k>0) { for(vector<int>::iterator it=s.begin(); it<s.end()-1 && *(it)<*(it+1); it++) ; s.erase(it); k--; } } } void main() { vector<int> s; s.push_back(1); s.push_back(7); s.push_back(8); s.push_back(5); s.push_back(4); s.push_back(3); delek(s, 4); for(vector<int>::iterator it=s.begin(); it!=s.end(); it++) cout<<*it; cout<<endl; }
相关文章推荐
- PAT 1033. To Fill or Not to Fill (25) 加油站贪心问题
- 51nod 贪心入门之四 独木舟问题
- 装船问题(贪心)
- 贪心算法之活动选择问题
- 最优装载问题(贪心基础)
- 01_传说中的车(Fabled Rooks UVa 11134 贪心问题)
- 贪心算法之删数问题
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
- 贪心算法之木棍问题
- POJ 1042 钓鱼问题 贪心枚举及动态规划
- 字典序最小问题(贪心)
- 小白书贪心之区间选点问题
- Radar Installation(poj1328,贪心,区间交集问题)
- 贪心-活动安排问题
- NYOJ 014会场安排问题(排序贪心)
- uva 10382 贪心 区间覆盖问题
- 贪心算法 解决 活动选择问题
- 4906 删数问题(另一种贪心思路)
- 51nod-1448 二染色问题-(贪心)
- 贪心算法专题小结——区间相关问题