solved 5 in reasonable runtime, could be optimized
This commit is contained in:
parent
d5d06cf18d
commit
7d967a7263
2 changed files with 231 additions and 0 deletions
13
5/Makefile
Normal file
13
5/Makefile
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
client: main.c
|
||||
gcc -o main main.c -O3 -Wall -Wextra -g -march=x86-64-v3
|
||||
|
||||
client-debug: main.c
|
||||
gcc -o main main.c -O1 -Wall -Wextra -g -fsanitize=address
|
||||
|
||||
client-debug-nosanitize: main.c
|
||||
gcc -o main main.c -O1 -Wall -Wextra -g
|
||||
|
||||
clean:
|
||||
rm main
|
||||
|
||||
.PHONY: clean
|
||||
218
5/main.c
Normal file
218
5/main.c
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
char input[] =
|
||||
{
|
||||
#embed "input.txt"
|
||||
,'\0' // null terminator
|
||||
};
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
uint64_t result = 0;
|
||||
|
||||
|
||||
uint64_t parseNum(char* str, int* len){
|
||||
int i = 0;
|
||||
uint64_t out = 0;
|
||||
while(str[i] != ',' && str[i] != '-' && str[i] != '\n'){
|
||||
out += str[i]-0x30;
|
||||
out *= 10;
|
||||
i++;
|
||||
}
|
||||
out /=10;
|
||||
*len = i;
|
||||
return out;
|
||||
}
|
||||
|
||||
int numPlaces (uint64_t n) {
|
||||
int r = 1;
|
||||
while (n > 9) {
|
||||
n /= 10;
|
||||
r++;
|
||||
}
|
||||
//printf("%d has %d places\n", n, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
uint64_t power(uint64_t base, uint64_t exp) {
|
||||
uint64_t i, result = 1;
|
||||
for (i = 0; i < exp; i++)
|
||||
result *= base;
|
||||
return result;
|
||||
}
|
||||
|
||||
void sort_ranges(uint64_t* starts, uint64_t* ends, int len){
|
||||
|
||||
for(int i = 0; i<len-1; i++){
|
||||
int curr_min = i;
|
||||
for(int o = i+1; o<len; o++){
|
||||
if(starts[curr_min]>starts[o]){
|
||||
//printf("%lu is bigger than %lu, so we have a new lowest\n", starts[curr_min], starts[o]);
|
||||
curr_min = o;
|
||||
}
|
||||
}
|
||||
uint64_t tmp_start = starts[curr_min];
|
||||
uint64_t tmp_end = ends[curr_min];
|
||||
|
||||
starts[curr_min] = starts[i];
|
||||
ends[curr_min] = ends[i];
|
||||
|
||||
starts[i] = tmp_start;
|
||||
ends[i] = tmp_end;
|
||||
}
|
||||
}
|
||||
|
||||
//bool check_fresh(uint64_t storage, uint64_t* starts, uint64_t* ends, int length){
|
||||
// int guess = length/2;
|
||||
// int diff = length/4;
|
||||
// while(true){
|
||||
// printf("guess is %d", guess);
|
||||
// diff = (diff/2)+1;
|
||||
// if(storage>starts[guess] && storage<starts[guess+1]){
|
||||
// break;
|
||||
// }else if(storage>starts[guess]){
|
||||
// guess += diff;
|
||||
// }else{
|
||||
// guess -= diff;
|
||||
// }
|
||||
// }
|
||||
// if(storage<ends[guess]){
|
||||
// return true;
|
||||
// }else{
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
|
||||
bool check_fresh(uint64_t storage, uint64_t* starts, uint64_t* ends, int length){
|
||||
//printf("checking %lu", storage);
|
||||
|
||||
for(int i = 0; i<length; i++){
|
||||
if(storage>=starts[i] && storage<=ends[i]){
|
||||
//printf(" .....fresh \t(is in range %lu - %lu)\n", starts[i], ends[i]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//printf(" .....spoiled\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
void join_ranges(uint64_t* starts, uint64_t* ends, int* range_count){
|
||||
int new_count = 0;
|
||||
for(int i = 0; i<*range_count; i++){
|
||||
int n=0;
|
||||
uint64_t end = ends[i];
|
||||
while(starts[i+n+1]<=end+1){
|
||||
n++;
|
||||
if(end<ends[i+n]){
|
||||
end = ends[i+n];
|
||||
}
|
||||
}
|
||||
starts[new_count]=starts[i];
|
||||
ends[new_count]=end;
|
||||
new_count++;
|
||||
i+=n;
|
||||
}
|
||||
starts[new_count+1] = ULLONG_MAX;
|
||||
*range_count = new_count;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
clock_t start, end;
|
||||
start = clock();
|
||||
|
||||
|
||||
//parse ranges first
|
||||
int i = 0;
|
||||
int range_count = 0;
|
||||
while(input[i] != '\n'){
|
||||
while(input[i] != '\n'){
|
||||
i++;
|
||||
}
|
||||
range_count++;
|
||||
i++;
|
||||
}
|
||||
//printf("range count: %d\n", range_count);
|
||||
|
||||
i++;
|
||||
int storage_count = 0;
|
||||
while(input[i] != 0){
|
||||
while(input[i] != '\n'){
|
||||
i++;
|
||||
}
|
||||
storage_count++;
|
||||
i++;
|
||||
}
|
||||
//printf("Storage count: %d\n", storage_count);
|
||||
|
||||
uint64_t starts [range_count+1];
|
||||
uint64_t ends [range_count+1];
|
||||
|
||||
i = 0;
|
||||
int range_num = 0;
|
||||
int len = 0;
|
||||
while(input[i] != '\n'){
|
||||
starts[range_num] = parseNum(&input[i], &len);
|
||||
i+=len+1;
|
||||
ends[range_num] = parseNum(&input[i], &len);
|
||||
i+=len+1;
|
||||
|
||||
//printf("%d-%d\n", starts[range_num], ends[range_num]);
|
||||
range_num++;
|
||||
}
|
||||
|
||||
//for(int i = 0; i<range_count; i++){
|
||||
// printf("%lu-%lu\n", starts[i], ends[i]);
|
||||
//}
|
||||
|
||||
range_num = 0;
|
||||
uint64_t* storage = malloc(sizeof(uint64_t)*(storage_count));
|
||||
i++;
|
||||
while(input[i] != 0){
|
||||
storage[range_num] = parseNum(&input[i], &len);
|
||||
i+=len+1;
|
||||
|
||||
|
||||
//printf("%d-%d\n", starts[range_num], ends[range_num]);
|
||||
range_num++;
|
||||
}
|
||||
|
||||
//printf("\n");
|
||||
//for(int i = 0; i<storage_count; i++){
|
||||
// printf("%lu\n", storage[i]);
|
||||
//}
|
||||
|
||||
sort_ranges(starts, ends, range_count);
|
||||
|
||||
//join_ranges(starts, ends, &range_count);
|
||||
|
||||
//for(int i = 0; i<range_count; i++){
|
||||
// printf("%lu-%lu\n", starts[i], ends[i]);
|
||||
//}
|
||||
|
||||
|
||||
for(int i = 0; i<storage_count; i++){
|
||||
if(check_fresh(storage[i], starts, ends, range_count)){
|
||||
result++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
end = clock();
|
||||
printf("count: %lu\n", result);
|
||||
clock_t ticks_taken = end - start;
|
||||
double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC;
|
||||
printf("time take: %f ticks\n", time_taken);
|
||||
printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC);
|
||||
|
||||
|
||||
exit(1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue