- 博客(12)
- 资源 (3)
- 收藏
- 关注
原创 贪心算法——建雷达站
/* 建雷达站 海岸线上建雷达站 覆盖所有的岛屿 最少的雷达站数目 思路: 倒过来想,在岛屿上看距离d是否与x轴有交点,得到交点的区间 再得到最少的区间数目 */ #include<stdio.h> #include<algorithm> #include<cmath> using namespace std; const int MAXN = 1000+10; //覆盖范围与x轴的区间 struct Interval{ double left;
2021-01-25 10:30:45
520
原创 贪心算法——看电视
/* 看电视问题——区间贪心 在固定的时间段内尽可能看多的电视节目 思路:选择结束时间最早的 */ #include<stdio.h> #include<algorithm> using namespace std; const int MAXN = 100 + 10; struct program{ int startTime; int endTime; }; program arr[MAXN]; bool Compare(program a,prog
2021-01-25 09:47:56
750
原创 贪心算法——疯狂的牛牛
/* 疯狂的牛牛 n个隔间 c头牛 使每两头牛之间的最小距离最大化 思路: 转化为判定性问题 判断间距d是否可行;对间距d采取二分策略 */ #include<stdio.h> #include<algorithm> using namespace std; const int MAXN = 1e5 + 10; int arr[MAXN]; bool judge(int n, int m, int distance){ int current = arr
2021-01-25 09:13:28
327
原创 贪心算法——箱子打包问题
```cpp /* 箱子打包 每个箱子最多放两个物品 n个物品 求最小的箱子总数 思路: 每次只装一个箱子,选最大和最小的装箱,若溢出则只装最大的物品 */ #include<stdio.h> #include<algorithm> using namespace std; const int MAXN = 1e5+10; int length[MAXN]; int main() { int n,l; scanf("%d%d",&n,&l);.
2021-01-24 21:03:12
981
原创 贪心算法——烘干衣服问题
现有n件衣服需要烘干,每件衣服的含水量为ai,如果自然晾干,每分钟含水量减少1;如果使用烘干机烘干,每分钟含水量减少k(直至为0)。只有一台烘干机,每次只能烘干一件衣服,且一次至少使用1分钟。求使所有衣服含水量为0的最少时间是多少。 #include<stdio.h> #include<cmath> #include<algorithm> using namespace std; const int MAXN = 1e5+10; int water[MAXN]; //
2021-01-24 20:46:18
972
5
原创 数据结构—括号匹配
/* 栈的应用:括号配对 */ #include<iostream> #include<stack> #include<string.h> using namespace std; void MatchBracket(char* S) { stack<char> stack; int length = strlen(S); for (int i = 0; i < length; i++) { if (S[i] == '(' || S[i]
2020-05-22 11:19:44
556
原创 数据结构—判断回文数
/* 栈的基本应用:判断回文数 */ #include<iostream> #include<cstring> #include<malloc.h> #include<stdlib.h> #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACK_INCREASEMENT 10 using namespace std; typedef struct { char* base; char* top;
2020-05-22 08:28:16
1187
1
原创 数据结构—进制转换
/* 栈的基本应用:进制转换 */ #include<iostream> #include<malloc.h> #include<stdio.h> #define STACK_INIT_SIZE 100 //存储空间初始分配量 #define STACK_INCRESEMENT 10 using namespace std; typedef struct { int* base; int* top; int stacksize;//当前已经分配的存储空间 }
2020-05-21 20:34:59
5451
原创 数据结构—顺序栈
/* 顺序栈的基本操作 栈空条件:S.base == S.top 栈中元素个数:S.top - S.base */ #include<iostream> #include<malloc.h> #include<stdlib.h> #define STACK_INIT_SIZE 50 //栈的初始大小 #define STACK_INCREASEMENT 10 //栈的大小的增量 using namespace std; typedef int ElemType; /
2020-05-21 19:56:35
529
原创 链表操作—单链表的销毁与清空
/* 单链表的销毁与清空 销毁:连同头结点一起释放 清空:保留头结点;置头结点的指针域为NULL */ //销毁链表 void destroyList(Linklist &L) { Linklist p; p = L; while (p) { L = L->next; // free(p); delete(p); } } //清空链表 void clearList(Linklist &L) { Linklist p; while (L->next)
2020-05-17 13:44:53
15463
8
原创 链表操作—头插法尾插法
单链表的插入操作,包括头插和尾插,两种的时间复杂度都为O(n)。 /* 单链表插入操作(头插+尾插) */ #include<iostream> #include<malloc.h> #include<stdlib.h> using namespace std; //定义结点数据类型 typedef int Elemtype; //结点定义 typedef struct LNode { Elemtype data; LNode* next; }LNode, *
2020-05-17 13:03:21
1812
原创 链表操作—单链表的倒置
链表操作—单链表的倒置 代码: /* 单链表的倒置 */ #include<iostream> #include<malloc.h> #include<stdlib.h> using namespace std; //定义结点数据类型 typedef int Elemtype; //结点定义 typedef struct LNode { Elemtype data; LNode* next; }LNode, * Linklist; //创建单链表(头插法) voi
2020-05-17 12:09:58
1432
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅