数据结构零碎笔记
作者:
张指导
,
2021-11-24 20:54:33
,
所有人可见
,
阅读 327
多机调度问题
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 10010, M = 110;
int n, m, q[N], res;
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ ) cin >> q[i];
sort(q, q + n);
priority_queue<int, vector<int>, greater<int>> heap; // 小根堆
for (int i = n - 1; i > n - 1 - m; i --)
heap.push(q[i]);
for (int i = n - 1 - m; i >= 0; i --)
{
int x = heap.top();
heap.pop();
heap.push(x + q[i]);
}
while (heap.size())
{
res = max(res, heap.top());
heap.pop();
}
cout << res << endl;
return 0;
}
循环队列
#include <iostream>
#include <cstring>
#include <algorithm>
#define MaxSize 50
using namespace std;
// 顺序循环队列
typedef int Elemtype;
typedef struct
{
Elemtype data[MaxSize];
int front, rear;
} SqQueue; // 循环队列
void initQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
bool isEmpty(SqQueue Q)
{
return Q.front == Q.rear;
}
bool enQueue(SqQueue &Q, int x)
{
if ((Q.rear + 1) % MaxSize == Q.front) return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool deQueue(SqQueue &Q, int &x)
{
if (Q.front == Q.rear) return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
// 链式循环队列
struct Node
{
int val;
Node *next;
Node() : val(0), next(NULL) {}
Node(int x) : val(x), next(NULL) {}
} *front, *rear;
void InitQueue()
{
front = rear = new Node();
}
void EnQueue(int x) // 入队
{
Node *p = new Node(x);
rear->next = p;
rear = p;
}
bool DeQueue(int &x)
{
if (front == rear) return false;
Node *p = front->next;
x = p->val;
front->next = p->next;
if (rear == p) rear = front; // 如果队列中只有一个结点,删除后变空
delete p;
return true;
}
void show()
{
auto p = front->next;
while (p)
{
cout << p->val << ' ';
p = p->next;
}
cout << endl;
}
int main()
{
InitQueue();
for (int i = 1; i <= 5; i ++ ) EnQueue(i);
int x;
DeQueue(x);
cout << x << endl;
show();
return 0;
}
# 太菜了