
贪心
acm_JL
这个作者很懒,什么都没留下…
展开
-
单源最短路径——Dijkstra算法
//Dijkstra算法!!! #include#includeusing namespace std;const int NUM=100; const int maxint= 10000;void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]){ int i,j; bool s[NUM]; for(i=1; i原创 2016-03-15 14:51:58 · 770 阅读 · 0 评论 -
删数字
删除一个整数中的k个数,得到的数最小。思想:确保前一个数比后一个数小,否则就把前一个数字删了#include using namespace std;string a;int k;int main() { cin>>a>>k; if(k >= a.size()) a.erase();//全部删除 else while(k > 0) { int i; for(原创 2016-03-15 16:09:04 · 1263 阅读 · 1 评论 -
背包问题求解
#include#include using namespace std;struct bag{ int w;//物品的重量 int v;//物品的价值 double c;//物品的性价比 double x;//每个物品装入背包中的量 int num;//物品的编号 };struct bag a[100];int cmp(bag a,bag b){ if(a.c>=b.原创 2016-03-15 14:35:57 · 425 阅读 · 0 评论 -
一般背包问题
#include#include using namespace std;struct bag{ int w;//物品的重量 int v;//物品的价值 double c;//物品的性价比 };struct bag a[100];int cmp(bag a,bag b){ if(a.c>=b.c) return 1; return 0;}//返回背包中物品的价值d原创 2016-03-15 14:35:25 · 1347 阅读 · 0 评论 -
活动安排
#include#include#includeusing namespace std;struct act{ int num;//活动的编号 int start;//活动开始时间 int finish;//活动结束时间 }; struct act map[100]; bool b[100];//记录活动是否被选中 int i,pre,n;//pre记录的是最后一次原创 2016-03-15 14:34:56 · 640 阅读 · 0 评论 -
最优装载方案
//贪心策略,采取最轻的集装箱先装的方法#include#includeusing namespace std;struct load{ int num;//箱子的编号 int w;//箱子的重量 };struct load a[100];int book[100];//标记箱子是否被选 int c,n,ans;int cmp(load a,load b){ if(a.w原创 2016-03-15 14:34:17 · 985 阅读 · 0 评论 -
最小生成树-kruskal算法
算法思想:设G=(V,E)是带权连通图,V={v1,v2,v3......vn}首先将G的n个顶点看成n个孤立的连通分量,将所有的边从小到大排序,从第一条边开始查看每一个边,只要不连成回路即可#include#includeusing namespace std;#define N 1000struct graph{ int u,v,cost; void set(int原创 2016-03-15 15:45:59 · 527 阅读 · 0 评论 -
最小生成树-prim算法
设G=(V,E)是无向连通带权图,对于图中的每一个边都有权重c[u][v]。如果G的子图T是一个包含G所有顶点的树,则称T是G的生成树。生成树上各权的总和称为生成树的耗费,在G的所有生成树中,耗费最小的生成树是最小生成树。prim算法:设置S={v1},只要S是V的真子集就做如下的贪心选择,选取满足条件的vi属于S,vj属于V-S,且c[i][j]是最小的边,将顶点vj添加到S中,这个过程原创 2016-03-15 15:38:22 · 2312 阅读 · 0 评论 -
poj1328 radar installation
//以各岛为圆心做半径为b的圆,记录下每个圆与x坐标轴的左右交点,然后进行贪心选择。 本质上每一个圆的左右交点连成的线段就是一个雷达放置的位置范围 #include#include#include using namespace std;struct point{ double left;//左交点 double right;//右交点 }a[100],temp; int cm原创 2016-03-17 16:27:05 · 371 阅读 · 0 评论 -
poj1477 box of bricks
//这题只是让你求给出的数的平均数即可!再通过续一比较,不够平均数的就从多于平均数中补上! 简单的题目! #include using namespace std;int num[55];int main(){ int n, i, tc = 0, sum, avg, ans; while (cin >> n) { if (n == 0) br原创 2016-03-17 16:29:37 · 481 阅读 · 0 评论 -
uva 1025 democrazy in danger
#include #include using namespace std; int main() { int i,k,a[100]; int ans=0; cin>>k; for(i=0;i<k;i++) cin>>a[i]; sort(a,a+k);//将组人数从小到大排序 for(i=0;i<=k/2;i++) ans+=a[i]/2+1; cou原创 2016-03-17 16:28:41 · 322 阅读 · 0 评论 -
ural 1014 the product of digits
//贪心策略:从9到2的顺序分解n的因子,如果最终n不是1,那么无解,否则则将存储的因子由小到大输出就是最小的整数 #includeusing namespace std;int a[100];int main(){ int n,i,j=-1; cin>>n; if(n<10) cout<<n<<endl; for(i=9;i>=2&&n!=1;i--)//将n进行因子分解,原创 2016-03-17 16:28:01 · 532 阅读 · 0 评论 -
uva 10954 add all
//维护一个堆,即优先权队列,每次取出最小的两个数 #includeusing namespace std;const int maxn=5010;int a[maxn],n;void sift(int i) //以i为根的子树调整为堆 { a[0]=a[i]; int k=i<<1;//计算左儿子指针k while(k<=n) { if(ka[k+1])//计算左右儿子原创 2016-03-17 16:26:16 · 416 阅读 · 0 评论 -
zoj2109fatmouse
#include#include#includeusing namespace std;struct trade{ int java;//库房的javabean int food;//换取库房的javabean需要的猫粮 double ratio;//用猫粮换取javabean的性价比 };struct trade a[100]; int cmp(trade a,trade原创 2016-03-17 16:24:22 · 510 阅读 · 0 评论 -
zoj 1171 sort the photo
/*算法分析:分析题目发现,从一堆照片中取照片是不计操作次数的,只有翻转的时候才统计次数,而且一堆照片可以一次翻转。所以决定从第一张照片开始遍历,遇到照片不同的就将前面的照片一次性翻转,接着按照翻转后的照片方向依次比较。 */ #include int main(){ int i; int n;//照片的张数 char p[100000]; //存储照片 int N; //原创 2016-03-17 16:23:48 · 481 阅读 · 0 评论 -
zoj1076 gene assembly
#include#includeusing namespace std;struct gene{ int begin;//开始位置 int end;//结束位置 int num;//编号 };struct gene a[100];int cmp(gene a,gene b){ return a.end<b.end?1:0;}int main(){ int i;原创 2016-03-17 16:23:00 · 525 阅读 · 0 评论 -
zoj 1092 moving tables
/*贪心策略:如果没有重叠那么一次过程10分钟就全部解决了,当发生重叠时,一个过程要等待另一个过程执行完才可以执行,因此,每段走廊的的最大经过次数就是总的过程的次数 。 */#include#includeusing namespace std;int main(){ int cases; cin>>cases; while(cases--)// { int i,j,f原创 2016-03-17 16:22:17 · 429 阅读 · 0 评论 -
多次最优服务次序问题
n个顾客等待一项服务,顾客i的等待时间是ti,共有s处提供此服务,应怎么安排能使得顾客的平均等待时间最短//将 按照顾客的服务时间从小到大排序,然后依次将顾客分配到s个窗口,如果窗口满了,在依次从第一个窗口开始统计的服务时间总数就是等待时间 #include #include using namespace std;double greedy(vector client,int s)原创 2016-03-15 16:36:58 · 1344 阅读 · 0 评论