Pagini recente » Cod sursa (job #1535263) | Profil yourlove | Cod sursa (job #1757289) | Cod sursa (job #2826603) | Cod sursa (job #2868602)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data; // integer data
struct Node* next; // pointer to the next node
};
int nodesCount;
// Utility function to add an element `x` to the stack
void push(struct Node** top, int x) // insert at the beginning
{
// allocate a new node in a heap
struct Node* node = NULL;
node = (struct Node*)malloc(sizeof(struct Node));
// check if stack (heap) is full. Then inserting an element would
// lead to stack overflow
if (!node)
{
printf("Heap Overflow\n");
exit(-1);
}
//printf("Inserting %d\n", x);
// set data in the allocated node
node->data = x;
// set the .next pointer of the new node to point to the current
// top node of the list
node->next = *top;
// update top pointer
*top = node;
// increase stack's size by 1
nodesCount += 1;
}
// Utility function to check if the stack is empty or not
int isEmpty(struct Node* top) {
return top == NULL;
}
// Utility function to return the top element of the stack
int peek(struct Node* top)
{
// check for an empty stack
if (!isEmpty(top)) {
return top->data;
}
else {
printf("The stack is empty\n");
exit(EXIT_FAILURE);
}
}
// Utility function to pop a top element from the stack
int pop(struct Node** top) // remove at the beginning
{
struct Node* node;
// check for stack underflow
if (*top == NULL)
{
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
}
// take note of the top node's data
int x = peek(*top);
//printf("Removing %d\n", x);
node = *top;
// update the top pointer to point to the next node
*top = (*top)->next;
// decrease stack's size by 1
nodesCount -= 1;
// free allocated memory
free(node);
return x;
}
// Utility function to return the nodesCount of the stack
int size() {
return nodesCount;
}
int verificaT(int m,struct Node* top) {
int i, sum = 0, j = size(), o = 0, t = 0;
int a[100]; //tablou pentru memorarea volumelor saltelelor
//m - verificam daca m este Volumul cu care se pot face nr-ul bun de transportari
//k - numarul de drumuri
//t - numarul de drumuri facut cu valoarea m actuala
//o - cate saltele au fost scoase
for (i = 0; i < j + o; i++) {
if (!isEmpty(top)) {
if (sum + peek(top) <= m) {
sum += peek(top);
a[o] = peek(top);
pop(&top);
o++;
if (isEmpty(top))
t++;
}
else {
t++;
sum = 0;
}
}
else
continue;
}
for (i = o - 1; i >= 0; i--) {
push(&top, a[i]);
}
return t;
}
int cautareBinara(int s, int d, int k, struct Node* top) {
int i;
int t = d, m;
for (i = 0; i <= t; i++) {
m = (s + d) / 2;
if (verificaT(m, top) < k)
d = m;
else if (verificaT(m, top) > k)
s = m;
else
return m;
}
}
int main(void) {
int N, K, V, d = 0, s = 0;
struct Node* top = NULL;
do {
scanf_s("%d", &N); //nr de saltele
} while (N < 1 || N > 16000);
do {
scanf_s("%d", &K); //nr de transporturi
} while (K < 1 || K > 16000);
for (int i = 0; i < N; i++) {
do {
scanf_s("%d", &V); //volumele saltelelor
} while (V < 1 || V > 16000);
push(&top, V);
d += V;
}
printf("%d", cautareBinara(s, d, K, top));
return 1;
}